我想配置我的webapp,拒绝所有没有正确"Content-Type“的请求。例如:除了"application/json“之外的任何内容类型都应该被拒绝。
目前我是通过一个自定义筛选器来实现的,但是我想知道如何通过安全配置中的"RequestHeaderRequestMatcher“直接实现?
让我们以下面的安全配置为例:
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")
}
}我应该如何添加新的请求匹配器来检查所有请求的有效内容类型?
发布于 2017-10-03 02:34:47
可以使用RequestHeaderRequestMatcher来验证请求是否包含某个标头。此外,还可以进行更精确的测试,以针对特定的值进行测试。
在下面的例子中,所有请求都必须包含一个值为application/json的Accept-Header。否则返回一个HTTP状态的406。
@Configuration
class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.requestMatcher(new RequestHeaderRequestMatcher("Accept", "application/json"));
}
}发布于 2017-10-02 22:21:49
您可以查看本文档CORS,此答案可以帮助您https://stackoverflow.com/a/43559288/5429215
https://stackoverflow.com/questions/46526030
复制相似问题