java @PassToken注解使用详细

详细

@PassToken作用就是为了不需要token也能访问,方便好用
简单易懂!!

一、使用步骤

1.注解

代码如下(示例):

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface PassToken {
    boolean required() default true;
}

2.逻辑处理

需要在filter拦截时候判断token,shiro拦截或者网关gateway拦截时候都行,自己写的拦截器也一样。拿到method就能到注解,判断就行
代码如下(示例):

    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        //通过ApplicationContext上下文(spring)找到RequestMappingHandlerMapping这个bean
        RequestMappingHandlerMapping handlerMapping = ApplicationContextUtil.getBean(RequestMappingHandlerMapping.class);
        //RequestMappingHandlerMapping是对应url和处理类方法的一个类
        HandlerExecutionChain handlerChain = handlerMapping.getHandler(httpServletRequest);
        //通过处理链找到对应的HandlerMethod类
        HandlerMethod handler = (HandlerMethod) handlerChain.getHandler();
        //HandlerMethod中有bean和method
        // Object bean = handler.getBean();//处理请求的类
        Method method = handler.getMethod();//处理请求的方法
        // boolean annotationPresent = method.isAnnotationPresent(PostMapping.class);
        if (method.isAnnotationPresent(PassToken.class)) {
            PassToken passToken = method.getAnnotation(PassToken.class);
            if (passToken.required()) {
                return true;
            }
        }
        }

3.使用

    @ApiOperation("登录")
    @PostMapping("/login")
    @PassToken
    public Response login(@RequestBody UserParam loginParam) {
        LoginVO userVO = userService.login(loginParam);
        return Response.ok(userVO);
    }

总结

提示:这里对文章进行总结:

用@PassToken就行了,方便好用!!!
因为我用的Oauth2+shiro,主要在于获取 注解 这里,
有两种:
方法一:

    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        WebApplicationContext ctx = RequestContextUtils.findWebApplicationContext(httpServletRequest);
        RequestMappingHandlerMapping mapping = ctx.getBean("requestMappingHandlerMapping", RequestMappingHandlerMapping.class);
        HandlerExecutionChain handler = null;
        handler = mapping.getHandler(httpServletRequest);
        Annotation[] declaredAnnotations = ((HandlerMethod) handler.getHandler()).
                getMethod().getDeclaredAnnotations();
        for(Annotation annotation:declaredAnnotations) {
            if (PassToken.class.equals(annotation.annotationType())) {
                return true;
            }
        }

方法二:

    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        WebApplicationContext ctx = RequestContextUtils.findWebApplicationContext(httpServletRequest);
        RequestMappingHandlerMapping mapping = ctx.getBean("requestMappingHandlerMapping", RequestMappingHandlerMapping.class);
        HandlerExecutionChain handler = null;
        handler = mapping.getHandler(httpServletRequest);
        Annotation[] declaredAnnotations = ((HandlerMethod) handler.getHandler()).
                getMethod().getDeclaredAnnotations();
        for(Annotation annotation:declaredAnnotations) {
            if (PassToken.class.equals(annotation.annotationType())) {
                return true;
            }
        }

总结:
1.自己写一个@PassToken注解
2.使用aop拦截器时候,获取request接口上的注解,如果有@PassToken则返回true,不需要验证token

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值