话不多说,先看拦截器
public class LoginInterceptor extends HandlerInterceptorAdapter {
/**
*
* @param request
* @param response
* @param handler
* @return boolean
* @author shy
* @date 2021/1/13 17:34
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
boolean continueHandling = true;
if (handler instanceof HandlerMethod) {
HandlerMethod method = (HandlerMethod) handler;
//获取方法上的注解
WithoutLogin withoutLoginAnnotation = method.getMethodAnnotation(WithoutLogin.class);
//获取类上的注解
CustomRole customRoleAnnotation = method.getMethod().getDeclaringClass().getAnnotation(CustomRole.class);
if (!sessionService.validUserType(customRoleAnnotation,request.getSession())){
throw new ErrorCodeException(ErrorCode.USER_AUTH_NOT_EXIST);
}
} else {
log.error("This handler object is not HandlerMethod instance! handler class: {}", handler.getClass());
}
return continueHandling;
}
}
获取类上的注解:
HandlerMethod.getMethod().getDeclaringClass().getAnnotation(注解名称.class);
获取方法上的注解:
HandlerMethod.getMethodAnnotation(注解名称.class);