SpringBoot配置拦截器对静态资源部分接口不实行拦截

SpringBoot配置拦截器对静态资源部分接口不实行拦截

/**

  • @ClassName WebSecurityConfig
  • @Description
  • @Author louk
  • @Date 2020/1/8 23:10
    */

import java.io.IOException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
/**

  • Created by fanclys on 2018/2/23 16:36:16

  • 拦截器配置
    */
    @Configuration
    public class WebSecurityConfig implements WebMvcConfigurer{
    @Bean
    public SecurityInterceptor getSecurityInterceptor() {
    return new SecurityInterceptor();
    }
    private class SecurityInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws IOException{
    HttpSession session = request.getSession();

         Cookie[] cookies = request.getCookies();
         String userToken = request.getHeader("");
         //判断是否已有该用户登录的session
         if(session.getAttribute("username") !=null){
             return  true;
         }else {
             System.out.println("没有session");
             response.sendRedirect("http://localhost:8080/login.html");
             return false;
         }
     }
    

    }
    @Override
    public void addInterceptors(InterceptorRegistry registry){
    InterceptorRegistration addInterceptor = registry.addInterceptor(getSecurityInterceptor());
    //排除配置
    addInterceptor.excludePathPatterns("/company/layui","/layui/","/static/","/css/","/image/","/js/","/login.html","/login/");
    //拦截配置
    addInterceptor.addPathPatterns("/**");
    }
    }


版权声明:本文为qq_41244181原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。