網站首頁 編程語言 正文
package com.example.config.request.handler;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.annotation.RetentionPolicy;
//Authorization.java,授權注解,作用于controller類及其方法上,方法優先
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Authorization {
}
package com.example.config.request.mvcConfigurer;
import com.example.config.request.handler.LogHandlerInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
/**
* 注冊攔截器
* **/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LogHandlerInterceptor()).addPathPatterns("/**");
}
}
package com.example.config.request.handler;
import com.example.config.exception.handler.BusinessException;
import com.example.config.response.enums.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
@Slf4j
/***
* 實現自定義的攔截器需要實現HandlerInterceptor接口
* **/
public class LogHandlerInterceptor implements HandlerInterceptor {
/**
* controller 執行之前調用
* 邏輯為 先去查看方法上是否有注解
* 沒有注解的話查看類上是否存在注解
* 都沒有的話可以通過
*
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
if(handler instanceof HandlerMethod){
// 方法上是否有授權注解
HandlerMethod handlerMethod = (HandlerMethod) handler;
Authorization authorization = handlerMethod.getMethodAnnotation(Authorization.class);
if (authorization == null) {
// 方法所屬類上是否有授權注解
authorization = handlerMethod.getMethod().getDeclaringClass().getAnnotation(Authorization.class);
if (authorization == null) {
return true;
}
}
// 方法或類上面有需要登錄權限的話就需要去校驗該token是否存在
// log.info(request.getHeader("sessionId").toString());
// if("匹配錯誤"){
// throw new BusinessException(ResultCode.USER_NOT_FIND);
// }else{
// return true;
// }
}
return true;
}
/**
* controller 執行之后,且頁面渲染之前調用
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("------postHandle-----");
}
/**
* 頁面渲染之后調用,一般用于資源清理操作
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
System.out.println("------afterCompletion-----");
}
}
原文鏈接:https://blog.csdn.net/qq_37061571/article/details/122056024
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-12-07 進程狀態ps?-ef中的e、f含義講解_linux shell
- 2023-02-12 python中使用docx模塊處理word文檔_python
- 2022-07-31 pycharm終端解釋器與Python解釋器配置_python
- 2022-11-17 Python中的優先隊列(priority?queue)和堆(heap)_python
- 2022-12-21 使用redis如何生成自增序列號碼_Redis
- 2022-08-06 python實現去除空格及tab換行符的方法_python
- 2022-07-26 golang中slice切片使用的誤區
- 2022-08-17 解決Git推送錯誤non-fast-forward的方法_相關技巧
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支