定义注解类
@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<String> test() {
return "";
}
|