![](/img/trans.png)
[英]Loading External Application Configuration In Spring Web Application
[英]Loading Spring Webflux application in external iframe
Spring 引导版本:2.3.0.RELEASE
使用 spring 启动应用程序作为 iframe 的src
与iframe
安全类路径,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 令牌已与此客户端关联。
也许它与 Set-cookie with SameSite=Lax; 在 webflux 中。
在src
设置为 spring 引导应用程序的任何外部域中使用iframe
。 如果您无法重新生成问题,请尝试删除 cookies。
<iframe src="http://localhost:8080/">
</iframe>
示例应用程序 - spring-iframe.zip
用例:许多客户关系管理服务集成需要在其 iframe 中打开应用程序,在这种特殊情况下,此应用程序用于 Salesforce 集成。
问题:有没有办法使用 spring webflux 作为依赖项登录外部 iframe?
CookieWebSessionIdResolver
bean 可以自定义为使用 session cookie 的不同选项。 sameSite(attribute)
可用于将 SameSite 值设置为“None”、“Lax”或“Strict”。
@Bean
public WebSessionIdResolver webSessionIdResolver() {
CookieWebSessionIdResolver resolver = new CookieWebSessionIdResolver();
resolver.setCookieName("SESSION");
resolver.addCookieInitializer((builder) -> {
builder.path("/")
.httpOnly(true)
.secure(true)
.sameSite("None");
});
return resolver;
}
Session配置:
@Configuration
@EnableSpringWebSession
public class SessionConfig {
@Bean
public ReactiveSessionRepository<MapSession> sessionRepository() {
return new ReactiveMapSessionRepository(new ConcurrentHashMap<>());
}
@Bean
public WebSessionIdResolver webSessionIdResolver() {
CookieWebSessionIdResolver resolver = new CookieWebSessionIdResolver();
resolver.setCookieName("SESSION");
resolver.addCookieInitializer((builder) -> {
builder.path("/")
.httpOnly(true)
.secure(true)
.sameSite("None");
});
return resolver;
}
}
Spring 会话核心依赖:
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
<version>2.3.0.RELEASE</version>
</dependency>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.