springboot 拦截器

拦截器

bash 复制代码
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 内部微服务调用密钥校验拦截器
 */
@Component
public class InnerServiceAuthInterceptor implements HandlerInterceptor {

    // 建议配置在 application.yml 中,通过 @Value("${inner.service.secret}") 注入
    private static final String INNER_SECRET = "your-pre-shared-key-2026";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String secret = request.getHeader("X-Internal-Secret");

        // 校验 Header 中是否包含合法密钥
        if (StringUtils.hasText(secret) && INNER_SECRET.equals(secret)) {
            return true; // 校验通过,放行
        }

        // 校验失败,直接拒绝访问
        response.setStatus(HttpStatus.FORBIDDEN.value());
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().write("{\"code\": 403, \"msg\": \"非法访问:缺少或无效的内部服务调用凭证\"}");
        return false;
    }
}

拦截器配置

bash 复制代码
import cn.dev33.satoken.stp.StpUtil;
import com.huayi.iepms.common.interceptor.FileSecurityInterceptor;
import com.huayi.iepms.common.config.IepmsConfig;
import com.huayi.iepms.common.constant.GenConstants;
import com.huayi.iepms.common.interceptor.InnerServiceAuthInterceptor;
import com.huayi.security.authentication.AuthorizationInterceptor;
import com.huayi.security.authentication.SecurityUserContext;
import com.huayi.web.utils.utils.CommonUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * @Author:zrf
 * @Date:2023/09/04 15:00
 * @description:配置 Spring MVC 拦截器
 */
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Resource
    private InnerServiceAuthInterceptor innerServiceAuthInterceptor;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(GenConstants.RESOURCE_PREFIX + "/**")
                .addResourceLocations("file:" + IepmsConfig.getProfile() + "/");
    }

    /**
     * 注册权限认证拦截器
     *
     * @param registry 拦截器注册器
     **/
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(innerServiceAuthInterceptor)
                // 只拦截指定路径下的接口
                .addPathPatterns("/file/**");

        WebMvcConfigurer.super.addInterceptors(registry);
    }

}
相关推荐
裕晟资质规划5 分钟前
三类涉密资质延续时限差异分析:军工60日与集成印制3个月的边界
java·服务器·前端·网络·安全
念念清晰6 分钟前
000-OpenJDK17 源码构建:MacOS环境(M芯片)
java·后端
Wang's Blog30 分钟前
Java 项目实战: 交友项目 - 百度人脸识别入门案例
java·百度·交友
来福猿35 分钟前
SpringCloud Config 源码分析:配置拉取、刷新与多环境管理
后端·spring·spring cloud
wuminyu1 小时前
JMM学习总结:深入理解happens-before关系
java·linux·c语言·jvm·c++
电商API_180079052471 小时前
反向海淘多币种结算:汇率该在哪个时点被冻结
java·开发语言·数据库·跨境电商·1688 api
古法老登瞎折腾2 小时前
大数据智能运维实战(一):机器和任务数据都有了,如何用 MCP 搭建运维助手?
大数据·运维·spring boot·spring ai·mcp
开开心心就好2 小时前
多份文档怎么批量打印?支持多种格式
java·服务器·开发语言·pdf·ocr·散列表·启发式算法
Pniubi2 小时前
模拟一个充值送token怎么保证幂等?一致性?原子性?
java
企业数字化笔记2 小时前
多租户配额怎么控制?用量口径、并发预留与超额处理的实现方法
java·数据库·spring boot