增加 @NoSensitive 注解

- 在 SysUser 实体类中为手机号码字段添加 @Sensitive 注解
- 新增 @NoSensitive 注解和相关切面、拦截器,用于 “关闭” 数据脱敏
- 在用户信息相关接口中添加 @NoSensitive 注解,以 “关闭” 数据脱敏
- 新增 WebMvcConfig 配置类,注册 NoSensitiveInterceptor 拦截器
This commit is contained in:
ayi
2025-07-07 11:10:52 +08:00
parent e549210ad6
commit 2622d9147e
13 changed files with 176 additions and 2 deletions

View File

@@ -0,0 +1,43 @@
package com.ruoyi.common.security.interceptor;
import com.ruoyi.common.core.annotation.NoSensitive;
import com.ruoyi.common.core.context.SensitiveContextHolder;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @Description @NoSensitive 注解的请求拦截器,主要用于对请求的注解,在请求的整个生命周期内有效
* @Author AhYi
* @Date 2025-07-07 10:35
*/
public class NoSensitiveInterceptor implements HandlerInterceptor {
private static final String SENSITIVE_INTERCEPTOR_APPLIED = "SENSITIVE_INTERCEPTOR_APPLIED";
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
NoSensitive noSensitive = handlerMethod.getMethodAnnotation(NoSensitive.class);
if (noSensitive == null) {
noSensitive = handlerMethod.getBeanType().getAnnotation(NoSensitive.class);
}
if (noSensitive != null) {
SensitiveContextHolder.enterNoSensitiveScope();
request.setAttribute(SENSITIVE_INTERCEPTOR_APPLIED, Boolean.TRUE);
}
}
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
Object applied = request.getAttribute(SENSITIVE_INTERCEPTOR_APPLIED);
if (applied != null && (Boolean) applied) {
SensitiveContextHolder.exitNoSensitiveScope();
}
}
}