gpt4 book ai didi

jquery - Spring安全中的X-Frame DENY

转载 作者:行者123 更新时间:2023-12-01 00:26:46 24 4
gpt4 key购买 nike

我正在使用jquery download plugin在我的 spring 项目中,但浏览器给我以下错误:

Refused to display 'http://localhost:8086/DART/fleetAndCar/download/5' in a frame because it set 'X-Frame-Options' to 'DENY'.

我读到了 Spring Security 中关于 Xframe 的问题,所以我添加了

http
.headers()
.addHeaderWriter(new XFrameOptionsHeaderWriter(XFrameOptionsHeaderWriter.XFrameOptionsMode.SAMEORIGIN))

但它不会更改 DENY,而是添加 SAMEORIGIN,所以我遇到以下错误:

Multiple 'X-Frame-Options' headers with conflicting values ('DENY, SAMEORIGIN') encountered when loading 'http://localhost:8086/DART/fleetAndCar/download/5'. Falling back to 'DENY'.

这是 http 请求:

enter image description here

这是我的 Spring 配置:

@Configuration
@Order(1)
public static class ApiWebSecurityConfig extends WebSecurityConfigurerAdapter{
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.antMatcher("/client/**")
.authorizeRequests()
//Exclude send file from authentication because it doesn't work with spring authentication
.antMatchers(HttpMethod.POST, "/client/file").permitAll()
.anyRequest().authenticated()
.and()
.httpBasic();
}
}

@Configuration
@Order(2)
public static class FormWebSecurityConfig extends WebSecurityConfigurerAdapter{

@Autowired
RoleServices roleServices;

@Override
public void configure(WebSecurity web) throws Exception {
web
//Spring Security ignores request to static resources such as CSS or JS files.
.ignoring()
.antMatchers("/static/**");
}

@Override
protected void configure(HttpSecurity http) throws Exception {
List<Role> roles=roleServices.getRoles();
//Retrieve array of roles(only string field without id)
String[] rolesArray = new String[roles.size()];
int i=0;
for (Role role:roles){
rolesArray[i++] = role.getRole();
}

http
.headers()
.addHeaderWriter(new XFrameOptionsHeaderWriter(XFrameOptionsHeaderWriter.XFrameOptionsMode.SAMEORIGIN))
.and()
.authorizeRequests() //Authorize Request Configuration
.anyRequest().hasAnyRole(rolesArray)//.authenticated()
.and() //Login Form configuration for all others
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.exceptionHandling().accessDeniedPage("/403")
.and()
.logout()
.logoutSuccessUrl("/login?logout")
.permitAll();

}
}

如何解决此问题?谢谢(尽管出现错误,但下载工作正常)

最佳答案

您可以在 Spring Security 配置文件中执行此操作,如下所示:

<http>    
<headers>
<frame-options policy="SAMEORIGIN"/>
</headers>
</http>

你也可以用java配置来做到这一点:

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends
WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http.headers().frameOptions().sameOrigin();
}
}

对于较旧的 Spring 版本,请使用:

http
.headers()
.addHeaderWriter(new XFrameOptionsHeaderWriter(XFrameOptionsHeaderWriter.XFrameOptionsMode.SAMEORIGIN))

而不是:

http.headers().frameOptions().sameOrigin();

最后,这些是可用的选项:

拒绝:不允许任何域在框架内显示此页面。

SAMEORIGIN:允许当前页面显示在另一个页面的框架中,但仅限于当前域内。

ALLOW-FROM:允许当前页面显示在框架中,但仅限于特定的 URI。例如www.example.com/frame-page

关于jquery - Spring安全中的X-Frame DENY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36603174/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com