通过自定义注解拿到请示路径等参数

通过自定义注解拿到请示路径等参数
通过自定义注解拿到请示路径等参数


@Component
@Slf4j
public class LoginHandlerResolver implements HandlerMethodArgumentResolver {

    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {

        //如果有注解 AuthUser 才会封装请求参数到业务方法
        return methodParameter.hasParameterAnnotation(Login.class);
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        String token = nativeWebRequest.getHeader(SysConstants.TOKEN_KEY);
        if (StrUtil.isEmpty(token)) {
            throw new BizException(AUTHENTICATION_FAILED);
        }

        JWTObject jwtToken = JWTUtil.parseToken(token);

        //验证token是否有效
        if (jwtToken == null || !JWTUtil.validTokenSign(token)) {
            //sendCode(response, 403);
            log.warn("token 不合法:{}", token);
            throw new BizException(TOKEN_ILLEGAL);
        }

        if (StrUtil.isEmpty(token)) {
            throw new BizException(USER_NOT_LOGIN);
        }

        JWTObject jwtObject = JWTUtil.parseToken(token);

        if(ObjectUtil.isEmpty(jwtObject)){
            throw new BizException(USER_NOT_LOGIN);
        }
        JWTObject.JWTPayload payload = jwtObject.getPayload();
        //取出登陆时封的参数
        String userId = payload.get("accountId");
        UserCache userCache = new UserCache();
        userCache.setUserId(Long.parseLong(userId));

        return userCache;
    }

}

通过自定义注解拿到请示路径等参数

上一篇:c – `void func()throw(type)`有什么意义?


下一篇:异常处理关键字