@Component
@Order(3) //类似于责任链,下一步、下一步这种
@Slf4j
public class AuthInterceptor implements HandlerInterceptor {
//往作用域中设置用户信息
//我的理解是 用户每次登录请求进入preHandle方法.每个用户对应不同的作用域。 也就是一个浏览器对应一个HttpServletRequest
//下面方法可以直接获取用户信息
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
log.info("AuthInterceptor#preHandle.preHandle ; 请求地址: {},登录认证拦截器开始校验", request.getRequestURI());
if (!(handler instanceof HandlerMethod)) {
return Boolean.TRUE;
}
HandlerMethod handlerMethod = (HandlerMethod) handler;
//1. 判断是否需要校验登录
boolean isCheckLogin = isNeedCheckLogin(handlerMethod);
if (!isCheckLogin) {
log.info("AuthInterceptor#preHandle.preHandle ; 请求地址: {},当前地址不需要校验登录,已放行", request.getRequestURI());
return Boolean.TRUE;
}
// 校验登录
LoginInfoDTO loginInfoDTO = checkLogin(request);
// 存入请求中
request.setAttribute(BasicConstants.LOGIN_INFO, loginInfoDTO);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
/**
* 获取登录用户信息
*
* @return
*/
public static LoginInfoDTO getLoginInfo() {
HttpServletRequest request = RequestContextUtils.getRequest();
return (LoginInfoDTO) request.getAttribute(BasicConstants.LOGIN_INFO);
}