springboot 拦截器验证token

springboot 配置拦截器,验证token并返回json给前端。代码如下:

/**
 * 验证token,是否登录
 */
@Component
public class TokenInterceptor implements HandlerInterceptor {

    private Logger logger = LoggerFactory.getLogger(TokenInterceptor.class);
    /**
     * 忽略拦截的url
     */
    private String urls[] = {
            "/login",
            "/register"
    };
    @Autowired
    private UserService userService;
    /**
     * 进入controller层之前拦截请求
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        String url = httpServletRequest.getRequestURI();
        String token = httpServletRequest.getHeader("token");
        String method = httpServletRequest.getMethod();
        if (!method.equals("OPTIONS")){
            logger.info(token);
            logger.info(url);
            logger.info(method);
            // 遍历需要忽略拦截的路径
            for (String item : this.urls){
                if (item.equals(url)){
                    return true;
                }
            }
            // 查询验证token
            User userByToken = userService.getUserByToken(token);
            if (userByToken == null){
                httpServletResponse.setCharacterEncoding("UTF-8");
                httpServletResponse.setContentType("application/json; charset=utf-8");
                PrintWriter out = null ;
                try{
                    Result res = new Result(10001,"登录失效重新登录");
                    String json = JSON.toJSONString(res);
                    httpServletResponse.setContentType("application/json");
                    out = httpServletResponse.getWriter();
                    // 返回json信息给前端
                    out.append(json);
                    out.flush();
                    return false;
                } catch (Exception e){
                    e.printStackTrace();
                    httpServletResponse.sendError(500);
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        // System.out.println("处理请求完成后视图渲染之前的处理操作");
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        // System.out.println("视图渲染之后的操作");
    }
}

注册拦截器

/**
 * 注册拦截器
 */
@Configuration
public class WebAppConfig implements WebMvcConfigurer {
    @Autowired
    private CROSInterceptor crosInterceptor;

    @Autowired
    private TokenInterceptor tokenInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry){
        registry.addInterceptor(crosInterceptor).addPathPatterns("/**");   // 跨域拦截器
        registry.addInterceptor(tokenInterceptor).addPathPatterns("/**");  // token 验证拦截器
    }
}

用到的包

<!-- json 转换工具 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.29</version>
</dependency>

你可能感兴趣的:(Java)