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

專注Java教育14年 全國(guó)咨詢/投訴熱線:400-8080-105
動(dòng)力節(jié)點(diǎn)LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁(yè) hot資訊 SpringBoot配置攔截器的方法

SpringBoot配置攔截器的方法

更新時(shí)間:2022-07-06 11:05:13 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽4071次

Java教程中大家會(huì)學(xué)到spring boot攔截器的配置,其實(shí)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;
        }
    }
}

關(guān)鍵代碼: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)創(chuàng)建注解:

/**
 * 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)創(chuàng)建攔截器:

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)在配置中添加攔截器,相當(dāng)于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注解,它會(huì)在啟動(dòng)時(shí)加載。

其實(shí)spring MVC的很多東西都可以用在spring boot中。只需將Configuration文件的模式改為對(duì)應(yīng)的@Configuration類即可。如果大家想了解更多相關(guān)知識(shí),不妨來(lái)關(guān)注一下動(dòng)力節(jié)點(diǎn)的SpringBoot教程,里面的課程內(nèi)容更加豐富,相信對(duì)大家的學(xué)習(xí)一定會(huì)有所幫助的。

提交申請(qǐng)后,顧問(wèn)老師會(huì)電話與您溝通安排學(xué)習(xí)

免費(fèi)課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 久久一本一区二区三区 | 久久久日韩精品国产成人 | 操操碰| 欧美在线综合 | 精品日韩在线视频一区二区三区 | 四虎影院国产 | 国产一区二区三区在线免费观看 | 国产成人啪一区二区 | 国产永久免费爽视频在线 | 亚洲欧美网 | 亚洲精品乱码久久久久久中文字幕 | 亚洲伦理网站 | 深夜福利免费 | 日韩中文字幕在线免费观看 | 99在线观看国产 | 精品国产一二三区 | 麻豆精品久久精品色综合 | 日本午夜免费理论片 | 咪咪色在线视频 | 亚洲一级在线 | 精品91精品91精品国产片 | 久久香蕉国产线 | 国产成人爱片免费观看视频 | 日本欧美久久久久免费播放网 | 一区精品视频 | 成人欧美一区二区三区视频 | 亚洲欧美精品一中文字幕 | 欧美一级永久免费毛片在线 | 俄罗斯老妇性欧美毛茸茸孕交 | 狠狠色丁香婷婷综合最新地址 | 一级毛片免费视频网站 | 亚洲欧美综合久久 | 亚洲 欧美 另类 天天更新影院 | 国产精品久久久久久免费 | 欧美午夜伦y4480私人影院 | 欧美一级毛片免费网站 | 久久精品片 | 特级一级毛片视频免费观看 | 欧美一级午夜免费视频你懂的 | 亚洲日本一区二区三区 | 亚洲欧美日韩高清 |