通过 @ModelAttribute 获取token

通过 @ModelAttribute 获取token

@Controller
public class AuthenticatorController extends BaseController {

    @ModelAttribute
    public void getUserId(HttpServletRequest request, HttpServletResponse response, Model model) throws NoSuchAlgorithmException {
        log.info("进入到AuthenticatorController");
        //验证签名
        // 设备ID
//		Map<String, Object> param = ServletHolderFilter.getContext().getParamMap();
//		String deviceId = param.get("deviceId").toString();
        String deviceId = request.getParameter("deviceId");
        // 通道
//    	String channel = param.get("channel").toString();
        String channel = request.getParameter("channel");
        // 版本
//    	String version = param.get("version").toString();
        String version = request.getParameter("version");
        // 签名
//    	String signature = param.get("signature").toString();
        String signature = request.getParameter("signature");

//		String token = param.get("token").toString();
        String token = request.getParameter("token");
        String temp;


        String type = "";
        //获取签名规则
        try {
            type = signature.substring(signature.length() - 1, signature.length());
        } catch (Exception e) {
            throw new BusinessException(ErrorCodeEnum.TOKEN_ERROR);
        }

        if ("1".equals(channel)) {
            //IOS
            if ("1".equals(type)) {
                temp = PasswordUtil.md5(deviceId + channel + version + deviceId.substring(deviceId.length() - 5, deviceId.length())) + "1";
            } else if ("2".equals(type)) {
                temp = PasswordUtil.md5(deviceId.substring(deviceId.length() - 5, deviceId.length()) + deviceId + channel + version) + "2";
            } else {
                temp = PasswordUtil.md5(deviceId + deviceId.substring(deviceId.length() - 5, deviceId.length()) + channel + version) + "3";
            }
        } else {
            //android
            if ("1".equals(type)) {
                temp = PasswordUtil.md5(deviceId.substring(0, 5) + deviceId + channel + version) + "1";
            } else if ("2".equals(type)) {
                temp = PasswordUtil.md5(deviceId + channel + version + deviceId.substring(0, 5)) + "2";
            } else {
                temp = PasswordUtil.md5(deviceId + channel + deviceId.substring(0, 5) + version) + "3";
            }
        }
        if (!temp.equals(signature)) {
            throw new BusinessException(ErrorCodeEnum.TOKEN_ERROR);
        }

        String url = request.getRequestURI();
        if (checkNeedAuthority(url)) {
            try {
//				String token = param.get("token").toString();
                if (StrUtil.isNotBlank(token)) {
                    //从redis中获取信息
                    Long userId = RedisUtil.getCacheObject(VisionTimeConstant.REDIS_USER_TICKET + ":" + token);
                    if (Objects.isNull(userId) || userId == 0L) {
                        throw new BusinessException(ErrorCodeEnum.TOKEN_IS_NULL);
                    } else {
                        //往model里面添加用户ID
                        model.addAttribute("userId", userId);
                    }
                } else {
                    throw new BusinessException(ErrorCodeEnum.TOKEN_IS_NULL);
                }
            } catch (Exception e) {
                //部分方法的token非必传
                if (checkNeedToken(url)) {
                    model.addAttribute("userId", null);
                } else {
                    throw new BusinessException(ErrorCodeEnum.TOKEN_IS_NULL);
                }
            }
        } else {
            try {
//				String token = param.get("token").toString();
                if (StrUtil.isNotBlank(token)) {
                    //从redis中获取信息
                    Long userId = RedisUtil.getCacheObject(VisionTimeConstant.REDIS_USER_TICKET + ":" + token);
                    if (userId == null) {
                        model.addAttribute("userId", null);
                    } else {
                        model.addAttribute("userId", userId);
                    }
                } else {
                    model.addAttribute("userId", null);
                }
            } catch (Exception e) {
                model.addAttribute("userId", null);
            }
        }
    }

    public boolean checkNeedAuthority(String url) {
        log.info("checkNeedAuthority : {}", url);
        if (url.contains("user_bannerList")) {
            return false;
        } else if (url.contains("user")) {
            return true;
        } else {
            return true;
        }
    }

    private static final String[] urls = {
            "user_cityList",
            "forgetPassword",
            "sendMobileVerify",
            "login",
            "bindPhone",
            "queryAllArticleIndex",
            "queryArticleDetailById",

            "queryDetailById",
            "queryAllSpecByItemId",
            "queryAllParamByItemId",

            "queryAllDict",
            "queryAllByMerchantId",
            "queryAllMerchant",
            "queryMerchantDetail",
            "queryAllMerchantComment",
            "area"
    };

    public boolean checkNeedToken(String url) {
        log.info("checkNeedToken : {}", url);
        for (int i = 1; i <= urls.length; i++) {
            if (url.contains(urls[i - 1])) {
                return true;
            }
        }
        return false;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值