【发布时间】:2020-11-04 17:14:50
【问题描述】:
Spring Boot 版本:2.3.0.RELEASE
使用 spring boot 应用程序作为 iframe 的 src 和 classpath 中的 spring security,webflux 和 servlet 应用程序的行为不同。
响应式 webapp 的安全配置:
@Configuration
@EnableWebFluxSecurity
public class SecurityConfig {
@Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
return http
.headers().frameOptions().disable()
.and()
.authorizeExchange()
.anyExchange().authenticated()
.and()
.formLogin()
.and()
.build();
}
}
servlet webapp 的安全配置:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.headers().frameOptions().disable()
.and()
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin();
}
}
使用上面给出的 tomcat 服务器和安全配置,用户登录并成功发回 200 OK 响应。 带有 netty 服务器的 webflux 中的类似配置响应 403 Forbidden 和响应为
CSRF Token 已与此客户端关联。
可能与 Set-cookie 相关,SameSite=Lax;在 webflux 中。
在任何外部域中使用iframe,并将src 设置为spring boot 应用程序。如果您无法重新生成问题,请尝试删除 cookie。
<iframe src="http://localhost:8080/">
</iframe>
示例应用 - spring-iframe.zip
用例:许多客户关系管理服务集成需要应用程序在其 iframe 中打开,在这种特殊情况下,此应用程序用于 Salesforce 集成。
问题: 有没有办法使用spring webflux作为依赖登录外部iframe?
【问题讨论】:
-
浏览器控制台中是否有任何错误/警告? Chrome 和 Firefox 可能会记录他们阻止的任何内容以及原因。
-
@OrangeDog 是的,如果登录页面中的 webflux 响应 cookie 默认设置为 Samesite=Lax。 Chrome 在网络面板中显示此警告,并在登录后抛出 403。
This Set-Cookie was blocked because it had the "Samesite=Lax" attribute but came from a cross-site response which was not the response to a top-level navigation -
这就是你的答案。你可以在下面写出来接受。
-
@OrangeDog 但这并不能解决登录问题,我需要一种解决方法来禁用 Spring Security 会话 cookie 中的 Samesite 属性。
标签: java spring spring-boot spring-security spring-webflux