【问题标题】:Loading Spring Webflux application in external iframe在外部 iframe 中加载 Spring Webflux 应用程序
【发布时间】:2020-11-04 17:14:50
【问题描述】:

Spring Boot 版本:2.3.0.RELEASE

使用 spring boot 应用程序作为 iframesrc 和 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


【解决方案1】:

CookieWebSessionIdResolver bean 可以自定义为会话 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;
}

会话配置:

@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>

Spring Session - WebFlux with Custom Cookie

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-06
    • 2019-01-29
    • 2015-01-23
    • 2021-10-26
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    相关资源
    最近更新 更多