登录拦截器 HandlerInterceptor 针对web提供的接口
为了统一区分所有请求,将所有的请求加上.do
登录,如果用户登录成功后,则生成一个随机的字符串,将字符串作为key存储在redis中。
登录拦截时,也只拦截.do请求,如果通过字符串查询到信息,则视为登录。否则未登录
增加配置 LoginInterceptorConfig, 这里设定我们要拦截的是什么,哪些拦截,哪些不拦截,特殊例外
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import com.ccjr.business.aop.LoginInterceptor; import lombok.extern.slf4j.Slf4j; /** * @Description : 登录拦截器 * @author : Chenweixian * @Date : 2020年11月3日 下午6:49:09 */ @Slf4j @Configuration public class LoginInterceptorConfig implements WebMvcConfigurer{ @Value("${login.uriIgnore:/login/*}") private String LOGIN_URIIGNORE; @Autowired private LoginInterceptor interceptor; public static final String PATH_PATTERNS = "/*/*.do,*.do"; @Override public void addInterceptors(InterceptorRegistry registry) { //addPathPatterns 用于添加拦截规则 //excludePathPatterns 用于排除拦截 registry.addInterceptor(interceptor) .addPathPatterns(PATH_PATTERNS.split(",")) .excludePathPatterns(LOGIN_URIIGNORE.split(",")); log.info("登录拦截器注册成功!"); } }
增加拦截器 LoginInterceptor ,拦截所有的请求
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import com.ccjr.base.BaseApiService; import com.ccjr.base.BaseResponse; import com.ccjr.business.web.controller.BaseController; import com.ccjr.business.web.services.LoginServices; import com.ccjr.commons.constants.ApiErrorEnum; import lombok.extern.slf4j.Slf4j; /** * @Description : d登录拦截器 * @Company : 晨创科技 * @author : Chenweixian * @Date : 2020年11月5日 下午1:24:54 */ @Slf4j @Component public class LoginInterceptor implements HandlerInterceptor { @Autowired private LoginServices loginServices; @Autowired protected StringRedisTemplate stringRedisTemplate; @Autowired protected BaseApiService baseApiService; /** * 在业务处理器处理请求之前被调用。预处理,可以进行编码、安全控制、权限校验等处理; */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String token = request.getHeader(BaseController.LOGIN_USER_TOKEN); if (StringUtils.isEmpty(token)) { returnJson(response); return false; } String userInfoString = stringRedisTemplate.boundValueOps(token).get(); if (StringUtils.isEmpty(userInfoString)) { returnJson(response); return false; } if (!StringUtils.isEmpty(token)) { loginServices.refreshLoginToken(token); } return true; } /** * 在业务处理器处理请求执行完成后,生成视图之前执行。后处理(调用了Service并返回ModelAndView,但未进行页面渲染),有机会修改ModelAndView; */ @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // log.info("postHandle..."); } /** * 在DispatcherServlet完全处理完请求后被调用,可用于清理资源等。返回处理(已经渲染了页面); */ @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // log.info("afterCompletion..."); } private void returnJson(HttpServletResponse response){ PrintWriter writer = null; response.setCharacterEncoding("UTF-8"); response.setContentType("application/json; charset=utf-8"); try { BaseResponse baseResponse = baseApiService.setResultError(ApiErrorEnum.USER_LOGIN_20103); writer = response.getWriter(); writer.print(baseResponse); } catch (IOException e){ log.error("拦截器输出流异常",e); } finally { if(writer != null){ writer.close(); } } } }
完。