zoukankan      html  css  js  c++  java
  • spring boot拦截器配置

    1.在spring boot配置文件application.properties中添加要拦截的链接

    com.url.interceptor=/user/test

    2.编写拦截器代码 ,创建UrlInterceptorUtil 类

     1 import org.springframework.core.io.ClassPathResource;
     2 import org.springframework.core.io.Resource;
     3 import org.springframework.core.io.support.PropertiesLoaderUtils;
     4 import org.springframework.stereotype.Component;
     5 import org.springframework.web.servlet.ModelAndView;
     6 import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
     7 
     8 
     9 import javax.servlet.http.HttpServletRequest;
    10 import javax.servlet.http.HttpServletResponse;
    11 import javax.servlet.http.HttpSession;
    12 import java.util.Properties;
    13 
    14 @Component
    15 public class UrlInterceptorUtil extends HandlerInterceptorAdapter {
    16 
    17     private String noCheckUrls;
    18     private static Properties props ;
    19 
    20     @Override
    21     public boolean preHandle(HttpServletRequest request,
    22                              HttpServletResponse response, Object handler) throws Exception {
    23         String url=request.getRequestURI();
    24         Resource resource = new ClassPathResource("/application.properties");//
    25         props = PropertiesLoaderUtils.loadProperties(resource);
    26         noCheckUrls=props.getProperty("com.url.interceptor","");
    27         if(!noCheckUrls.contains(url)){
    28                 return true;
    29         }
    30         HttpSession session=request.getSession();
    31      //业务逻辑处理
    32         if (session.getAttribute("User")==null){
    33             
    34             response.sendRedirect("/index");
    35             return false;
    36         }
    37         return true;
    38     }
    39 
    40         @Override
    41         public void postHandle(HttpServletRequest request,
    42                                HttpServletResponse response, Object o, ModelAndView mav)
    43             throws Exception {
    44 //        System.out.println("postHandle");
    45     }
    46 
    47     @Override
    48     public void afterCompletion(HttpServletRequest request,
    49                                 HttpServletResponse response, Object o, Exception excptn)
    50             throws Exception {
    51 //        System.out.println("afterCompletion");
    52     }
    53 }

    3.配置spring boot拦截

    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    //标注此文件为一个配置项,spring boot才会扫描到该配置。该注解类似于之前使用xml进行配置
    @Configuration
    public class CustomWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter {
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            //对所有的请求进行拦截
            registry.addInterceptor(new UrlInterceptorUtil()).addPathPatterns("/**");
        }
    }
    
    
    
    
  • 相关阅读:
    Java 的垃圾回收机制
    Java 变参函数的实现
    对已知有限集合中缺失或重复元素的查找
    Java 旋转数组查找旋转点和任意元素(元素可重复)
    第三章 磁盘分区
    第二章 一切都是对象
    发布 AutoRssReceiver Console 机器人! 兼讲: .Net 中的 Attribute (特性)
    .Net/C#/VB/TSQL/Java 实现: 将天文数字转换成中文大写 (2000 年前的思路,打劫的,一点儿技术含量都没有)
    .Net/C#/VB/TSQL/Java/Script 实现: 将天文数字转换成中文大写 (2000 年前的思路,打劫的,一点儿技术含量都没有)
    利用"委托"实现类的对象实例按"多字段嵌套"排序
  • 原文地址:https://www.cnblogs.com/kui-technology/p/8940665.html
Copyright © 2011-2022 走看看