大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 hot資訊 SpringBoot配置攔截器的方法

SpringBoot配置攔截器的方法

更新時間:2022-07-06 11:05:13 來源:動力節點 瀏覽3783次

Java教程中大家會學到spring boot攔截器的配置,其實spring boot攔截器的配置方式和spring MVC類似。只需要注意一些小的變化。下面介紹兩種常用的攔截器:

1.基于URL的攔截器:

public class LoginInterceptor extends HandlerInterceptorAdapter{
	/**
     * Call before request processing (before Controller method call)
     * Interceptor Based on 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)) {
        	//No need to intercept directly
            return true;
        } else {
        	// This describes the things you need to do to intercept, such as fetching cache, SESSION, permission judgment, etc
            System.out.println("====================================");
            return true;
        }
    }
}

關鍵代碼:path.matches(const.no'intersector'path是基于正則匹配的url。

/**
 * @author 	BianP
 * @explain Constant class
 */
public class Const {
    public static final String SUCCESS = "SUCCESS";
    public static final String ERROR = "ERROR";
    public static final String FIALL = "FIALL";
    /**********************Objects and individuals****************************/
    public static final String SESSION_USER = "loginedAgent"; // User object
    public static final String SESSION_LOGINID = "sessionLoginID"; // Login ID
    public static final String SESSION_USERID = "sessionUserID"; // Current user object ID number
    public static final String SESSION_USERNAME = "sessionUserName"; // Current user object ID number
    public static final Integer PAGE = 10; // Default page number
    public static final String SESSION_URL = "sessionUrl"; // Recorded url
    public static final String SESSION_SECURITY_CODE = "sessionVerifyCode"; // Login page verification code
    // Time cache time
    public static final int TIMEOUT = 1800;// second
	public static final String ON_LOGIN = "/logout.htm";
	public static final String LOGIN_OUT = "/toLogout";
    // Do not validate URL anon: do not validate / authc: controlled
    public static final String NO_INTERCEPTOR_PATH =".*/((.css)|(.js)|(images)|(login)|(anon)).*";
}

2.基于注解的攔截器

(1)創建注解:

/**
 * Use this annotation on the method of the Controller that requires login authentication
 */
@Target({ElementType.METHOD})// Available on method name
@Retention(RetentionPolicy.RUNTIME)// Valid at run time
public @interface LoginRequired {	
}

(2)創建攔截器:

public class AuthorityInterceptor extends HandlerInterceptorAdapter{	
	 @Override
	 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
	 	// If it is not mapped to a method directly through
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }
        // ①: START method annotation level interceptor
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();
        // Determine whether the interface needs to be logged in
        LoginRequired methodAnnotation = method.getAnnotation(LoginRequired.class);
        // With @ LoginRequired annotation, authentication is required
        if (methodAnnotation != null) {
            // This describes the things you need to do to intercept, such as fetching cache, SESSION, permission judgment, etc
            System.out.println("====================================");
            return true;
        }
        return true;
	}
}

(3)在配置中添加攔截器,相當于spring MVC中配置文件的作用:

/**
 * The same webmvc interception configuration as spring MVC
 * @author BIANP
 */
@Configuration
public class WebMvcConfigurer extends WebMvcConfigurationSupport {
	 @Override
	 public void addInterceptors(InterceptorRegistry registry) {
        // Block all requests, and determine whether login is required by judging whether there is @ LoginRequired annotation
        registry.addInterceptor(LoginInterceptor()).addPathPatterns("/**");
        registry.addInterceptor(AuthorityInterceptor()).addPathPatterns("/**");
        super.addInterceptors(registry);
	 }	 
	 @Bean
	 public LoginInterceptor LoginInterceptor() {
		 return new LoginInterceptor();
	 }	 
	 @Bean
	 public AuthorityInterceptor AuthorityInterceptor() {
		 return new AuthorityInterceptor();
	 }
}

一定要添加@Configuration注解,它會在啟動時加載。

其實spring MVC的很多東西都可以用在spring boot中。只需將Configuration文件的模式改為對應的@Configuration類即可。如果大家想了解更多相關知識,不妨來關注一下動力節點的SpringBoot教程,里面的課程內容更加豐富,相信對大家的學習一定會有所幫助的。

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 国产精品情侣久久婷婷文字 | 欧美一级毛片不卡免费观看 | 四虎成人精品国产一区a | 婷婷综合国产激情在线 | 久久精品综合网 | 国产精品tv | 91久久国产精品视频 | 亚洲伦理久久 | 99视频在线观看高清 | 久久婷婷综合中文字幕 | 日韩黄色网址 | 国产亚洲精品激情一区二区三区 | 国产人成午夜免视频网站 | 亚洲精品高清国产一久久 | 国产亚洲精品久久久久久小说 | 超级碰碰青草免费视频92 | 91尤物在线 | 久热视线观看免费视频 | 久久综合九色综合97婷婷女人 | 青青操在线观看 | 中国性孕妇xxxxxxxx孕交 | 美国一级毛片aa | 99热最新网站 | 可以直接看的毛片 | 国产一级精品毛片 | 狠狠狠很橹影院 | 国产成人综合一区人人 | 男人的天堂免费视频 | 国产臀控福利视频在线 | 日韩欧美一区二区三区久久 | 久久久成人啪啪免费网站 | 在线观看欧美国产 | 成年人一级黄色片 | 亚洲人人爱 | 欧美成人免费视频a | 成人激情视频在线 | 日本一极毛片兔费看 | 美女精品久久久久久国产潘金莲 | 四虎黄色影视库 | 五月色婷婷琪琪综合伊人 | 女人精69xxxxx免费无毒 |