



下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第詳解Java攔截器以及自定義注解的使用目錄1,設(shè)置預(yù)處理,設(shè)置不需要攔截的請(qǐng)求2.UserTokenInterceptor,securityInterceptor分別處理不同的請(qǐng)求攔截,執(zhí)行不同的攔截邏輯。3.關(guān)于注解的使用總結(jié)
1,設(shè)置預(yù)處理,設(shè)置不需要攔截的請(qǐng)求
@Component
publicclassMyWebConfigimplementsWebMvcConfigurer{
privatefinalUserTokenInterceptoruserTokenInterceptor;
privatefinalSecurityInterceptorsecurityInterceptor;
publicMyWebConfig(
UserTokenInterceptoruserTokenInterceptor,SecurityInterceptorsecurityInterceptor){
this.userTokenInterceptor=userTokenInterceptor;
this.securityInterceptor=securityInterceptor;
@Override
publicvoidaddInterceptors(InterceptorRegistryregistry){
//定義排除swagger訪問的路徑配置
String[]swaggerExcludes=
newString[]{"/swagger-ui.html","/swagger-resources/**","/webjars/**"};
registry
.addInterceptor(userTokenInterceptor)
.addPathPatterns("/**")
.excludePathPatterns(
"/user/login","/static/**","/*.html","/*.ico","/*.json","/*.png","/heartbeat/**")
.excludePathPatterns(swaggerExcludes);
registry
.addInterceptor(securityInterceptor)
.addPathPatterns("/maintain/**","/user/**")
.excludePathPatterns("/user/login");
}
2.UserTokenInterceptor,securityInterceptor分別處理不同的請(qǐng)求攔截,執(zhí)行不同的攔截邏輯。
2個(gè)處理的類請(qǐng)求上可以有交集,2個(gè)處理類都執(zhí)行。
@Component
publicclassUserTokenInterceptorimplementsHandlerInterceptor{
privatefinalEmpInfoServiceempInfoService;
publicUserTokenInterceptor(EmpInfoServiceempInfoService){
this.empInfoService=empInfoService;
@Override
publicbooleanpreHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler)
throwsException{
//校驗(yàn)handler是否是HandlerMethod
if(!(handlerinstanceofHandlerMethod)){
returntrue;
//從請(qǐng)求頭中獲取token
Stringtoken=request.getHeader("Authorization");
*update:2025/11/30ShengJieLi
*增加邏輯:Authorization的值不為本系統(tǒng)生成的token時(shí),解密Authorization,獲取token并驗(yàn)證
if(StrUtil.isNotEmpty(token)){
EmpInfosecurityEmployee=empInfoService.queryToken(token);
if(securityEmployee!=null){
//token有效
Stringref=empInfoService.isRef(token);
if(StrUtil.isNotBlank(ref)){
response.setHeader("Access-Control-Expose-Headers","token");
response.addHeader("token",ref);
}else{
//Authorization為PBE加密數(shù)據(jù)
securityEmployee=empInfoService.analyticQueryToken(token,response);
if(securityEmployee!=null){
//token有效
//將User對(duì)象放入到ThreadLocal中
UserLocal.set(securityEmployee);
returntrue;
returnfalse;
//Strings=JSONUtil.toJsonStr(ResponseResult.error(ErrorCode.TOKEN_ERROR));
//response.setContentType("text/html;charset=UTF-8");
//JSONUtil.toJsonStr(s,response.getWriter());
//response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
InterceptorExceptionRerceptorError(response,ErrorCode.TOKEN_ERROR);
//update結(jié)束
returnfalse;
@Override
publicvoidafterCompletion(
Htt
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 中級(jí)會(huì)計(jì)常見考點(diǎn)的試題及答案
- 定目標(biāo)2025年入團(tuán)考試試題及答案
- 中級(jí)會(huì)計(jì)考生分享平臺(tái)試題及答案
- 2024碩士外語水平考試內(nèi)容解讀與試題及答案
- 慢性疾病患者的自我管理指導(dǎo)試題及答案
- 成功秘訣無人機(jī)駕駛員考試試題及答案
- 中級(jí)審計(jì)師學(xué)術(shù)研究試題及答案
- 車位使用協(xié)議和合同
- 后疫情時(shí)代的審計(jì)新挑戰(zhàn)試題及答案
- 心理健康護(hù)理干預(yù)試題及答案
- DL-T 5850-2021 電氣裝置安裝工程 高壓電器施工及驗(yàn)收規(guī)范
- 多層螺旋CT原理及臨床應(yīng)用
- 年產(chǎn)3.0萬噸二甲醚裝置分離精餾工段的設(shè)計(jì)
- 驗(yàn)房項(xiàng)目詳細(xì)表格
- 小學(xué)二年級(jí)下冊(cè)第19課-大象的耳朵教案(部編版)
- 三年級(jí)培智生活數(shù)學(xué)暑假作業(yè)
- 公路隧道建設(shè)施工技術(shù)規(guī)范學(xué)習(xí)考試題庫(400道)
- 康復(fù)醫(yī)學(xué)質(zhì)控標(biāo)準(zhǔn)
- 《石壕吏》優(yōu)質(zhì)課一等獎(jiǎng)?wù)n件
- 天津東疆綜合保稅區(qū)管理委員會(huì)招考聘用沖刺題(二)
- 第三篇 太陽和恒星世界(3)恒星形成和演化
評(píng)論
0/150
提交評(píng)論