通过spring注解实现接口免登录校验

通过spring注解实现接口免登录校验,第1张

定义注解类

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface NotLogin {

}

2.登录拦截放行

 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();
        // 判断接口是否需要登录
        NotLogin methodAnnotation = method.getAnnotation(NotLogin.class);
        if (methodAnnotation != null) {
             //放行
             return true;
        }
       
        return true;
    }

3.

在不需要登录的接口增加不需要登录的注解

    @PostMapping(value = "/test")
    @ResponseBody
    @NotLogin
    public RestResponse test() {
        return "";
    }

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/738215.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-28
下一篇 2022-04-28

发表评论

登录后才能评论

评论列表(0条)

保存