



下載本文檔
版權(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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 福建漳州2024~2025學(xué)年高一下冊期末數(shù)學(xué)試題學(xué)生卷
- 個性化營養(yǎng)方案制定考核試卷
- 獸用抗生素批發(fā)政策考核試卷
- 可持續(xù)發(fā)展與企業(yè)綠色物流實踐考核試卷
- 變電站自然災(zāi)害應(yīng)對措施考核試卷
- 化纖漿粕高溫過濾材料的生物降解性探討考核試卷
- 一次性護目鏡的防沖擊與防紫外線性能的國際標(biāo)準(zhǔn)對比考核試卷
- 2025年中國PE不飽和聚酯漆數(shù)據(jù)監(jiān)測報告
- 2025年中國L-酒石酸數(shù)據(jù)監(jiān)測報告
- 2025年中國E-cares面部雕塑系統(tǒng)數(shù)據(jù)監(jiān)測研究報告
- 電廠安規(guī)考試題庫及答案
- 2021-2022學(xué)年浙江省杭州市拱墅區(qū)英語小升初新生分班考試卷 附解析
- 2024-2025學(xué)年人教版(2024)初中英語七年級下冊教學(xué)工作總結(jié)(共4套)
- Unit 1 Happy Holiday 第5課時(Section B 2a-3c) 2025-2026學(xué)年人教版英語八年級下冊
- 2025年中國三元乙丙橡膠市場調(diào)查研究報告
- 常見耐藥菌感染診療與防控
- 征兵體檢外科標(biāo)準(zhǔn)
- 小學(xué)生預(yù)防拐騙教育課件
- 2025-2030年中國基于細(xì)胞的人源化小鼠模型行業(yè)市場現(xiàn)狀供需分析及投資評估規(guī)劃分析研究報告
- 2025至2030中國無線通訊檢測行業(yè)市場發(fā)展分析及競爭格局與投資機會報告
- 2025年上海徐匯區(qū)高一(下)信息技術(shù)合格考試題及答案
評論
0/150
提交評論