public class LoginInterceptor extends HandlerInterceptorAdapter{
/**
* 在請求處理之前進行調用(Controller方法調用之前)
* 基於URL實現的攔截器
* @param request
* @param response
* @param handler
* @return
* @throws Exception
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String path = request.getServletPath();
if (path.matches(Const.NO_INTERCEPTOR_PATH)) {
//不需要的攔截直接過
return true;
} else {
// 這寫你攔截需要干的事兒,比如取緩存,SESSION,權限判斷等
System.out.println("====================================");
return true;
}
}
}
關鍵代碼:path.matches(Const.NO_INTERCEPTOR_PATH 就是基於正則匹配的url。
/**
* @author BianP
* @explain 常量類
*/
public class Const {
public static final String SUCCESS = "SUCCESS";
public static final String ERROR = "ERROR";
public static final String FIALL = "FIALL";
/**********************對象和個體****************************/
public static final String SESSION_USER = "loginedAgent"; // 用戶對象
public static final String SESSION_LOGINID = "sessionLoginID"; // 登錄ID
public static final String SESSION_USERID = "sessionUserID"; // 當前用戶對象ID編號
public static final String SESSION_USERNAME = "sessionUserName"; // 當前用戶對象ID編號
public static final Integer PAGE = 10; // 默認分頁數
public static final String SESSION_URL = "sessionUrl"; // 被記錄的url
public static final String SESSION_SECURITY_CODE = "sessionVerifyCode"; // 登錄頁驗證碼
// 時間 緩存時間
public static final int TIMEOUT = 1800;// 秒
public static final String ON_LOGIN = "/logout.htm";
public static final String LOGIN_OUT = "/toLogout";
// 不驗證URL anon:不驗證/authc:受控制的
public static final String NO_INTERCEPTOR_PATH =".*/((.css)|(.js)|(images)|(login)|(anon)).*";
}
public class AuthorityInterceptor extends HandlerInterceptorAdapter{
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 如果不是映射到方法直接通過
if (!(handler instanceof HandlerMethod)) {
return true;
}
// ①:START 方法注解級攔截器
HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod();
// 判斷接口是否需要登錄
LoginRequired methodAnnotation = method.getAnnotation(LoginRequired.class);
// 有 @LoginRequired 注解,需要認證
if (methodAnnotation != null) {
// 這寫你攔截需要干的事兒,比如取緩存,SESSION,權限判斷等
System.out.println("====================================");
return true;
}
return true;
}
}
把攔截器添加到配置中,相當於SpringMVC時的配置文件干的事兒:
/**
* 和springmvc的webmvc攔截配置一樣
* @author BIANP
*/
@Configuration
public class WebConfigurer implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 攔截所有請求,通過判斷是否有 @LoginRequired 注解 決定是否需要登錄
registry.addInterceptor(LoginInterceptor()).addPathPatterns("/**");
registry.addInterceptor(AuthorityInterceptor()).addPathPatterns("/**");
}
@Bean
public LoginInterceptor LoginInterceptor() {
return new LoginInterceptor();
}
@Bean
public AuthorityInterceptor AuthorityInterceptor() {
return new AuthorityInterceptor();
}
}
1、一定要加@Configuration 這個注解,在啟動的時候在會被加載。
2、有一些教程是用的“WebMvcConfigurerAdapter”,不過在spring5.0版本后這個類被丟棄了 WebMvcConfigurerAdapter ,雖然還可以用,但是看起來不好。
3、也有一些教程使用的WebMvcConfigurationSupport,我使用后發現,classpath:/META/resources/,classpath:/resources/,classpath:/static/,classpath:/public/)不生效。具體可以原因,大家 可以看下源碼因為:WebMvcAutoConfiguration上有個條件注解:
所以還是建議使用WebMvcConfigurer, 其實springMVC很多東西,都可以搬到springboot中來使用,只需要把配置文件的模式,改成 對應@Configuration 類就好了