java - Spring Security 页面无法在 Chrome 上的 Iframe 中打开

标签 java spring-boot spring-security thymeleaf

我正在使用 Spring Boot、Spring Security 和 jdk 1.8。当我尝试在 Chrome 上的 iframe 中打开任何安全的 Thymleaf 页面时,它每次都会将我重定向到登录页面。它在 Firefox 和 IE 上运行良好。

当我尝试在没有 iframe 的情况下打开相同的 URL 时,它工作正常。下面是我的 Spring Security conf 文件代码。还有一件事:两个域是不同的。

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .headers()
                .frameOptions().disable()
                .and()
                .csrf().disable()/*disbaling csrf here*/
                .authorizeRequests()
                .antMatchers("/","/login","/css/**", "/js/**", "/fonts/**","/img/**").permitAll()/*do not use spring security on this path*/
                .and()
                .formLogin()
                .successHandler(successHandler) /*after success login on web we are handling the success event*/
                .permitAll()
                .and()
                .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/login/?logout") /*defining logout and login url here*/
                .permitAll()
                 /*
                 * This is for authentication failure handling
                 * */
                 http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint)
                 /*Token based authentication we are handling here*/
                 http.addFilterBefore(new StatelessAuthenticationFilter(tokenAuthenticationService), BasicAuthenticationFilter.class);
                 http.addFilterAfter(new SameSiteFilter(), BasicAuthenticationFilter.class)
    }

如何修复它?

最佳答案

首先,我建议您不要禁用 "X-Frame-Options" header 并在 iframe 中使用您的应用程序。
这会带来安全风险,您可以在 this answer 中了解更多信息。

现在解释一下您所看到的行为。
Spring Security 使用 Session cookie 来存储用户的 session 。
Cookie 与域关联,因此,例如,如果有一个与域 stackoverflow.com 关联的 Cookie,则该 Cookie 将包含在对 stackoverlow.com 的任何请求中.

为了控制该行为,cookie 还有一个名为 SameSite 的属性。
SameSite attribute 可以有 3 个值:NoneLaxStrict,或者可以取消设置并且没有值。
当值为 None 时,其行为如上所述(包含在所有请求中)。
当值为 Lax 时,Cookie 将仅包含在顶级导航 GET 请求中。

Spring Security 使用的 Session cookie 不会设置 SameSite 属性。
目前(2020 年 3 月),某些浏览器(例如 Firefox 和 Edge)将 unset 属性视为 None
不过,Chrome 正在尝试以与 Lax 相同的方式处理未设置的属性。
您可以在 Chrome Platform Status 中阅读更多相关信息。

总而言之,使用 Chrome 时,Session Cookie 会被视为将 SameSite 设置为 Lax
由于在 iframe 中呈现应用程序不是顶级导航,因此 iframe 的请求中不包含 Session cookie,并且应用程序无法知道用户是否已登录。

您可以使用 Spring Session 将 SameSite 属性显式设置为 None
我再次提醒您不要这样做,因为它会使您的应用程序容易受到 CSRF 和点击劫持攻击。
如果在考虑安全影响后,您认为有必要将 SameSite 属性设置为 None,则可以通过在依赖项中包含 Spring Session 并创建 custom CookieSerializer 来实现。

关于java - Spring Security 页面无法在 Chrome 上的 Iframe 中打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60827129/

相关文章:

java - 如何使用 spring webflux 上传和读取文本文件?

spring - 获取 Spring SpEL 表达式上的 java 注释属性值

spring - 如何提供来自文件的 spring boot application.yml 属性值

java - 使用 Spring Security 进行 JUnit 测试

java - JSF:空嵌套数据表

java - EJB:自定义身份验证和授权

Java 数字回文

Spring 启动 mongodb java.lang.NoClassDefFoundError : com/mongodb/MongoException$DuplicateKey

java - Spring boot 集成测试观察服务的困境

java - WebLogic 的默认调试端口是什么?