



下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
第詳解Java攔截器以及自定義注解的使用目錄1,設(shè)置預(yù)處理,設(shè)置不需要攔截的請求2.UserTokenInterceptor,securityInterceptor分別處理不同的請求攔截,執(zhí)行不同的攔截邏輯。3.關(guān)于注解的使用總結(jié)
1,設(shè)置預(yù)處理,設(shè)置不需要攔截的請求
@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分別處理不同的請求攔截,執(zhí)行不同的攔截邏輯。
2個處理的類請求上可以有交集,2個處理類都執(zhí)行。
@Component
publicclassUserTokenInterceptorimplementsHandlerInterceptor{
privatefinalEmpInfoServiceempInfoService;
publicUserTokenInterceptor(EmpInfoServiceempInfoService){
this.empInfoService=empInfoService;
@Override
publicbooleanpreHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler)
throwsException{
//校驗handler是否是HandlerMethod
if(!(handlerinstanceofHandlerMethod)){
returntrue;
//從請求頭中獲取token
Stringtoken=request.getHeader("Authorization");
*update:2025/11/30ShengJieLi
*增加邏輯:Authorization的值不為本系統(tǒng)生成的token時,解密Authorization,獲取token并驗證
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對象放入到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等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 運(yùn)動員培訓(xùn)合同協(xié)議書
- 簽單合同協(xié)議書怎么寫
- 餐飲眾籌合同協(xié)議書
- 解除工程檢測合同協(xié)議書
- 2025貸款合同協(xié)議范本
- 風(fēng)車制造項目合同協(xié)議書
- 裝修貸款裝修合同協(xié)議書
- 聘用合同就業(yè)協(xié)議書范本
- 2025餐館租賃合同
- 2025存量房買賣合同及相關(guān)工程
- 綠色施工管理辦法
- 2024年安徽省中考物理試卷真題(含答案解析)+2023年中考物理試卷及答案
- 青年興則國家興青年強(qiáng)則國家強(qiáng)
- 藥物分析智慧樹知到答案2024年中國藥科大學(xué)
- 2023年海南省中考物理試題(解析版)
- 2024年北京中考地理試卷
- 入藏安全承諾書怎么寫
- 2024年安徽省初中地理會考卷真題含參考答案
- 《月亮與六便士》讀書分享課件
- 車輛超載超限培訓(xùn)
- 燃?xì)庑袠I(yè)數(shù)字化轉(zhuǎn)型與智能化升級
評論
0/150
提交評論