当前位置:   article > 正文

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

addinterceptor 根目录不拦截

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;
         }
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    }
    @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("/**");
    }
    }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/972932
推荐阅读
相关标签
  

闽ICP备14008679号