自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

SpringBoot優(yōu)雅的配置攔截器方式

開(kāi)發(fā) 后端
其實(shí)spring boot攔截器的配置方式和springMVC差不多,只有一些小的改變需要注意下就ok了。下面主要介紹兩種常用的攔截器。

其實(shí)spring boot攔截器的配置方式和springMVC差不多,只有一些小的改變需要注意下就ok了。

下面主要介紹兩種常用的攔截器:

一、基于URL實(shí)現(xiàn)的攔截器: 

  1. public class LoginInterceptor extends HandlerInterceptorAdapter{  
  2.     /**  
  3.      * 在請(qǐng)求處理之前進(jìn)行調(diào)用(Controller方法調(diào)用之前)  
  4.      * 基于URL實(shí)現(xiàn)的攔截器  
  5.      * @param request  
  6.      * @param response  
  7.      * @param handler  
  8.      * @return  
  9.      * @throws Exception  
  10.      */  
  11.     @Override  
  12.     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {  
  13.         String path = request.getServletPath();  
  14.         if (path.matches(Const.NO_INTERCEPTOR_PATH)) {  
  15.             //不需要的攔截直接過(guò)  
  16.             return true;  
  17.         } else {  
  18.             // 這寫你攔截需要干的事兒,比如取緩存,SESSION,權(quán)限判斷等  
  19.             System.out.println("====================================");  
  20.             return true;  
  21.         }  
  22.     }  

關(guān)鍵代碼:path.matches(Const.NO_INTERCEPTOR_PATH 就是基于正則匹配的url。 

  1. /**  
  2.  * @author     BianP  
  3.  * @explain 常量類  
  4.  */  
  5. public class Const {  
  6.     public static final String SUCCESS = "SUCCESS" 
  7.     public static final String ERROR = "ERROR" 
  8.     public static final String FIALL = "FIALL" 
  9.     /**********************對(duì)象和個(gè)體****************************/  
  10.     public static final String SESSION_USER = "loginedAgent"; // 用戶對(duì)象  
  11.     public static final String SESSION_LOGINID = "sessionLoginID"; // 登錄ID  
  12.     public static final String SESSION_USERID = "sessionUserID"; // 當(dāng)前用戶對(duì)象ID編號(hào)  
  13.     public static final String SESSION_USERNAME = "sessionUserName"; // 當(dāng)前用戶對(duì)象ID編號(hào)  
  14.     public static final Integer PAGE = 10; // 默認(rèn)分頁(yè)數(shù)  
  15.     public static final String SESSION_URL = "sessionUrl"; // 被記錄的url  
  16.     public static final String SESSION_SECURITY_CODE = "sessionVerifyCode"; // 登錄頁(yè)驗(yàn)證碼 
  17.     // 時(shí)間 緩存時(shí)間  
  18.     public static final int TIMEOUT = 1800;// 秒  
  19.     public static final String ON_LOGIN = "/logout.htm" 
  20.     public static final String LOGIN_OUT = "/toLogout" 
  21.     // 不驗(yàn)證URL anon:不驗(yàn)證/authc:受控制的  
  22.     public static final String NO_INTERCEPTOR_PATH =".*/((.css)|(.js)|(images)|(login)|(anon)).*" 

二、基于注解的攔截器

①創(chuàng)建注解: 

  1. /**  
  2.  * 在需要登錄驗(yàn)證的Controller的方法上使用此注解  
  3.  */  
  4. @Target({ElementType.METHOD})// 可用在方法名上  
  5. @Retention(RetentionPolicy.RUNTIME)// 運(yùn)行時(shí)有效  
  6. public @interface LoginRequired {   

②創(chuàng)建攔截器: 

  1. public class AuthorityInterceptor extends HandlerInterceptorAdapter{  
  2.      @Override  
  3.      public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {  
  4.         // 如果不是映射到方法直接通過(guò)  
  5.         if (!(handler instanceof HandlerMethod)) {  
  6.             return true;  
  7.         }  
  8.         // ①:START 方法注解級(jí)攔截器  
  9.         HandlerMethod handlerMethod = (HandlerMethod) handler;  
  10.         Method method = handlerMethod.getMethod();  
  11.         // 判斷接口是否需要登錄  
  12.         LoginRequired methodmethodAnnotation = method.getAnnotation(LoginRequired.class);  
  13.         // 有 @LoginRequired 注解,需要認(rèn)證  
  14.         if (methodAnnotation != null) {  
  15.             // 這寫你攔截需要干的事兒,比如取緩存,SESSION,權(quán)限判斷等  
  16.             System.out.println("====================================");  
  17.             return true;  
  18.         }  
  19.         return true;  
  20.     }  

三、把攔截器添加到配置中,相當(dāng)于SpringMVC時(shí)的配置文件干的事兒: 

  1. /**  
  2.  * 和springmvc的webmvc攔截配置一樣  
  3.  * @author BIANP  
  4.  */  
  5. @Configuration  
  6. public class WebConfigurer implements WebMvcConfigurer {  
  7.      @Override  
  8.      public void addInterceptors(InterceptorRegistry registry) {  
  9.         // 攔截所有請(qǐng)求,通過(guò)判斷是否有 @LoginRequired 注解 決定是否需要登錄  
  10.         registry.addInterceptor(LoginInterceptor()).addPathPatterns("/**");  
  11.         registry.addInterceptor(AuthorityInterceptor()).addPathPatterns("/**");  
  12.      }  
  13.      @Bean  
  14.      public LoginInterceptor LoginInterceptor() {  
  15.          return new LoginInterceptor();  
  16.      }  
  17.      @Bean  
  18.      public AuthorityInterceptor AuthorityInterceptor() {  
  19.          return new AuthorityInterceptor();  
  20.      }  

1、一定要加@Configuration 這個(gè)注解,在啟動(dòng)的時(shí)候在會(huì)被加載。

2、有一些教程是用的“WebMvcConfigurerAdapter”,不過(guò)在spring5.0版本后這個(gè)類被丟棄了 WebMvcConfigurerAdapter ,雖然還可以用,但是看起來(lái)不好。

3、也有一些教程使用的WebMvcConfigurationSupport,我使用后發(fā)現(xiàn),classpath:/META/resources/,classpath:/resources/,classpath:/static/,classpath:/public/不生效。具體可以原因,大家可以看下源碼因?yàn)椋篧ebMvcAutoConfiguration上有個(gè)條件注解: 

  1. @ConditionalOnMissingBean(WebMvcConfigurationSupport.class) 

所以還是建議使用WebMvcConfigurer, 其實(shí)springMVC很多東西,都可以搬到springboot中來(lái)使用,只需要把配置文件的模式,改成 對(duì)應(yīng)@Configuration 類就好了。 

 

責(zé)任編輯:龐桂玉 來(lái)源: Java知音
相關(guān)推薦

2021-07-19 05:48:30

springboot 攔截器項(xiàng)目

2025-01-02 10:10:51

2009-06-24 16:00:00

2023-09-15 11:26:16

2009-09-27 17:37:32

Hibernate攔截

2025-02-28 08:14:53

2023-09-05 08:58:07

2011-05-16 10:14:11

Hibernate

2009-07-08 17:02:11

JDK實(shí)現(xiàn)調(diào)用攔截器

2011-11-21 14:21:26

SpringMVCJava框架

2009-06-25 15:54:42

Struts2教程攔截器

2009-06-25 15:59:21

Struts2教程攔截器

2021-11-03 17:04:11

攔截器操作Servlet

2020-10-22 10:43:55

HTTP框架AOP

2012-02-03 13:27:16

2009-06-19 18:26:38

Spring事務(wù)配置

2009-02-04 14:19:38

2009-06-04 08:01:25

Struts2攔截器原理

2024-05-06 00:00:00

C#工具代碼

2009-02-04 14:45:06

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)