Java Spring安全页面不';t在铬合金上的Iframe中打开
我正在使用SpringBoot、springsecurity和JDK1.8。当我试图在Chrome上的iframe中打开任何安全页面时,它会再次指示我登录页面。它在firefox和IE上运行良好。当我尝试在没有iframe的情况下打开相同的URL时,它运行良好。我已经花了很多时间来解决这个问题,但我能解决它。下面是我的spring安全配置文件代码。还有一件事两个领域是不同的Java Spring安全页面不';t在铬合金上的Iframe中打开,java,spring-boot,spring-security,thymeleaf,Java,Spring Boot,Spring Security,Thymeleaf,我正在使用SpringBoot、springsecurity和JDK1.8。当我试图在Chrome上的iframe中打开任何安全页面时,它会再次指示我登录页面。它在firefox和IE上运行良好。当我尝试在没有iframe的情况下打开相同的URL时,它运行良好。我已经花了很多时间来解决这个问题,但我能解决它。下面是我的spring安全配置文件代码。还有一件事两个领域是不同的 @Override protected void configure(HttpSecurity http) thr
@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”
标题并在iframe中使用您的应用程序。这会带来安全风险,您可以在中了解更多 现在来解释您看到的行为。
Spring Security使用
会话
cookie存储用户的会话。cookie与域关联,因此,例如,如果存在与域
stackoverflow.com
关联的cookie,则该cookie将包含在对stackoverlow.com
的任何请求中
为了控制这种行为,cookie还有一个名为SameSite
可以有3个值,
None
、Lax
、Strict
,也可以不设置且没有值。当值为
None
时,其行为如上所述(包括在所有请求中)。当该值为
Lax
时,cookie将仅包括在顶级导航GET
请求中
Spring Security使用的会话
cookie没有设置SameSite
属性。此时(2020年3月),一些浏览器,如Firefox和Edge,将unset属性视为与
None
然而,Chrome正在尝试将unset属性与
Lax
处理相同
你可以阅读更多关于这方面的文章
总之,使用Chrome时,会话
cookie被视为将SameSite
设置为Lax
由于在iframe中呈现应用程序不是顶级导航,因此来自iframe的请求中不包括
会话
cookie,并且应用程序无法知道用户已登录
您可以使用Spring会话将SameSite
属性显式设置为None
。我再次提醒您不要这样做,因为这会使您的应用程序容易受到CSRF和点击劫持攻击。
如果考虑了安全性的影响,你认为有必要将<代码> SameSite <代码>属性设置为<代码> NON/COMPE >,你可以通过在你的依赖项中包含Spring会话并创建一个.< /P>来做同样的事情。你也使用Spring会话吗?不,我没有使用Spring会话。谢谢,施泰因,我会遵循这个。