一、概述

扶風(fēng)網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站開發(fā)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營(yíng)維護(hù)。成都創(chuàng)新互聯(lián)于2013年創(chuàng)立到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)。
攔截器的使用場(chǎng)景越來(lái)越多,尤其是面向切片編程流行之后。那通常攔截器可以做什么呢?
之前我們?cè)贏gent介紹中,提到過(guò)統(tǒng)計(jì)函數(shù)的調(diào)用耗時(shí)。這個(gè)思路其實(shí)和AOP的環(huán)繞增強(qiáng)如出一轍。
那一般來(lái)說(shuō),場(chǎng)景如下:
以及其他等等。
二、Spring的攔截器
無(wú)論是SpringMVC或者SpringBoot中,關(guān)于攔截器不得不提:
org.springframework.web.servlet.handler.HandlerInterceptorAdapter
public abstract class HandlerInterceptorAdapter implements AsyncHandlerInterceptor {
// 在目標(biāo)方法執(zhí)行前執(zhí)行
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
return true;
}
// 在目標(biāo)方法執(zhí)行后執(zhí)行,但在請(qǐng)求返回前,我們?nèi)匀豢梢詫?duì) ModelAndView進(jìn)行修改
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
throws Exception {}
// 在請(qǐng)求已經(jīng)返回之后執(zhí)行
@Override
public void afterCompletion(
HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {}
// 用來(lái)處理異步請(qǐng)求, 當(dāng)Controller中有異步請(qǐng)求方法的時(shí)候會(huì)觸發(fā)該方法
@Override
public void afterConcurrentHandlingStarted(
HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {}
}
三、實(shí)現(xiàn)一個(gè)用于驗(yàn)證簡(jiǎn)單權(quán)限的攔截器
1、自定義一個(gè)權(quán)限注解 @Auth
@Inherited
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Auth {
String user() default "";
}2、在Controller的方法上添加注解
上一步添加完注解后,之后要在你所使用的方法上添加相關(guān)注解,如下。
@RestController
@EnableAutoConfiguration
public class DemoController {
@Auth(user = "admin")
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String sayHello() {
return "hello world.";
}
}
3、實(shí)現(xiàn)攔截器功能
需求:我們?cè)谟脩敉ㄟ^(guò)/hello這個(gè)URI訪問(wèn)時(shí),對(duì)其進(jìn)行驗(yàn)證,如果為admin則放行,否則拒絕,假設(shè)用戶的身份在URL參數(shù)中。
思路:因此我們要在執(zhí)行sayHello()之前,對(duì)用戶做出驗(yàn)證。如果其身份與注解中的身份相同,則放行。因此我們要在preHandle()中做文章。
難點(diǎn):我們?cè)趺茨玫紺ontroller 方法上的@Auth這個(gè)注解呢?看PreHandle()的三個(gè)參數(shù),貌似也沒(méi)有哪個(gè)可以提供Controller類中的注解。
其實(shí),第三個(gè)參數(shù)handler,一般情況下其類型為:org.springframework.web.method.HandlerMethod類型,而這里面含有注解的相關(guān)信息。
為什么這么說(shuō)呢?
在SpringBoot中,注解的默認(rèn)類型為函數(shù)級(jí),而在SpringMVC其默認(rèn)類型為Controller對(duì)象級(jí)別。
因此,如果在SpringMVC中需要在dispatcher-servlet.xml中配置:
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>,這樣其類型才為HandlerMethod。
我們看下具體實(shí)現(xiàn)邏輯:
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle");
if (!handler.getClass().isAssignableFrom(HandlerMethod.class)) {
System.out.println("cat cast handler to HandlerMethod.class");
return true;
}
// 獲取注解
Auth auth = ((HandlerMethod) handler).getMethod().getAnnotation(Auth.class);
if (auth == null) {
System.out.println("cant find @Auth in this uri:" + request.getRequestURI());
return true;
}
// 從參數(shù)中取出用戶身份并驗(yàn)證
String admin = auth.user();
if (!admin.equals(request.getParameter("user"))) {
System.out.println("permission denied");
response.setStatus(403);
return false;
}
return true;
}
其實(shí)實(shí)現(xiàn)邏輯就兩點(diǎn):從參數(shù)中取出身份,和注解中的進(jìn)行比對(duì)。
4、配置攔截器
那怎么讓剛才的這個(gè)攔截器生效呢?
這個(gè)時(shí)候,需要我們配置:WebMvcConfigurerAdapter
具體實(shí)現(xiàn)如下:
@Configuration
public class ConfigAdapter extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/hello");
}
}
注意:這里有兩點(diǎn)需要注意,一個(gè)是@Configuration這個(gè)注解,這樣才能讓SpringBoot服務(wù)發(fā)現(xiàn)這個(gè)配置;另一個(gè)是配置匹配項(xiàng),這里是對(duì)"/hello"這個(gè)進(jìn)行攔截。("/**"是對(duì)所有的訪問(wèn)攔截)
四、運(yùn)行
訪問(wèn) http://127.0.0.1:8080/hello?user=admin就可以看到結(jié)果啦。
本文中的代碼詳見(jiàn):https://github.com/hawkingfoo/springboot-interceptor
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
新聞名稱:SpringBoot快速設(shè)置攔截器并實(shí)現(xiàn)權(quán)限驗(yàn)證的方法
當(dāng)前URL:http://www.chinadenli.net/article28/gpchjp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、網(wǎng)站排名、網(wǎng)站改版、網(wǎng)站導(dǎo)航、商城網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
網(wǎng)頁(yè)設(shè)計(jì)公司知識(shí)