首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SpringSecurity - RequestHeaderRequestMatcher

SpringSecurity - RequestHeaderRequestMatcher
EN

Stack Overflow用户
提问于 2017-10-02 21:02:42
回答 2查看 977关注 0票数 1

我想配置我的webapp,拒绝所有没有正确"Content-Type“的请求。例如:除了"application/json“之外的任何内容类型都应该被拒绝。

目前我是通过一个自定义筛选器来实现的,但是我想知道如何通过安全配置中的"RequestHeaderRequestMatcher“直接实现?

让我们以下面的安全配置为例:

代码语言:javascript
复制
EnableWebSecurity
class SecurityConfig : WebSecurityConfigurerAdapter() {

    override fun configure(http: HttpSecurity) {
        http.authorizeRequests()
                .requestMatchers(matcher)
                .denyAll()
                .antMatchers("/css/**", "/index")
                .permitAll()
                .antMatchers("/user/**")
                .hasRole("USER")

        http.formLogin()
                .loginPage("/login")
                .failureUrl("/login-error")
    }
}

我应该如何添加新的请求匹配器来检查所有请求的有效内容类型?

EN

回答 2

Stack Overflow用户

发布于 2017-10-03 02:34:47

可以使用RequestHeaderRequestMatcher来验证请求是否包含某个标头。此外,还可以进行更精确的测试,以针对特定的值进行测试。

在下面的例子中,所有请求都必须包含一个值为application/jsonAccept-Header。否则返回一个HTTP状态的406

代码语言:javascript
复制
@Configuration
class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.requestMatcher(new RequestHeaderRequestMatcher("Accept", "application/json"));
    }
}
票数 1
EN

Stack Overflow用户

发布于 2017-10-02 22:21:49

您可以查看本文档CORS,此答案可以帮助您https://stackoverflow.com/a/43559288/5429215

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46526030

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档