gpt4 book ai didi

java - Spring 安全检查的 header 存在一些固定值

转载 作者:行者123 更新时间:2023-12-02 01:50:59 25 4
gpt4 key购买 nike

假设我有 Spring Boot Rest 服务,我想用这个简单的逻辑来保护它,如果存在值为“123456”的 header “token”,则请求没问题,我应该处理它,如果没有,那么我应该发回未经授权的 401 http 错误。

如何在 Spring Boot 1.5 中实现此逻辑。

最佳答案

通过实现GenericFilterBean

public class CustomAuthenticationFilter extends GenericFilterBean {

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
// get header and validate from request object
filterChain.doFilter(request, response);
}
}

并将过滤器挂接到您的安全配置中。

@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
// other security config
.addFilterBefore(new CustomAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
}

关于java - Spring 安全检查的 header 存在一些固定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52990451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com