这个想法是在webflux中复制http://blog.netgloo.com/2014/09/28/spring-boot-enable-the-csrf-check-selectively-only-for-some-requests/。
这就是我到目前为止的地方:
@Configuration
@EnableWebFluxSecurity
@EnableReactiveMethodSecurity
public class SecurityConfig {
@Bean
SecurityWebFilterChain springSecurityFilterChain(final ServerHttpSecurity http) {
http
.csrf().requireCsrfProtectionMatcher(
new ServerWebExchangeMatcher() {
@Override
public Mono<MatchResult> matches(ServerWebExchange serverWebExchange) {
// here check if the url should have csrf or not and then return MatchResult.match() or notMatch(), however I find that if I return match then I get 'Invalid CSRF Token' error.
// return MatchResult.match();
// return MatchResult.notMatch();
}
}
).and()
.anyExchange().authenticated()
.and()
.httpBasic()
.and()
.formLogin().loginPage("/login")
.and().logout()
return http.build();
}
}
字符串
4条答案
按热度按时间gywdnpxw1#
这个应该够了
字符串
hec6srdp2#
我来晚了,但经过大量的研究,我得到了一个很好的解决方案。
假设你想对一个特定的URL禁用CSRF检查。在我的例子中,我想对匹配这个模式
/token/**
的URL禁用CSRF检查。所以,首先,你需要创建一个
NegatedServerWebExchangeMatcher
示例,在其中你将添加所有你想要禁用CSRF检查的URL模式。在我的例子中,我将创建一个方法,只为/token/**
模式返回NegatedServerWebExchangeMatcher
。所以,这是我的方法。字符串
现在在
requireCsrfProtectionMatcher
方法中委托getURLsForDisabledCSRF()
,如下所示:型
这是我的Spring Cloud API Gateway的Security Config Class,它实际上使用了web flux。
型
注:Spring Cloud版本:2020.0.2
k4emjkb13#
我认为你只是从这些post/put端点中删除了所有的安全协议(可能会更改数据)。最好禁用CSRF并仍然受益于Spring身份验证链。只需使用许可证就可以消除用户登录的任何需求。不是一个好答案。
toiithl64#
配置允许来源:
字符串