X-Frame-Options: DENY只在后端端口端点上工作



因此,由于我正在研究spring安全,我已将headers.frameOptions设置为DENY,当我尝试将后端端点放在iframe中localhost:8080时这里,一切都工作得很好,问题是,当我把前端localhost:3000在iframe中,什么都不发生,应用程序显示在iframe中。我认为我正在做的头配置只适用于api,而不是在应用程序的开始

在应用程序的开始,你可以看到没有配置:X-Frame-Options: DENY在我发送一个API

之后下面是函数

@Override
protected void configure(HttpSecurity http) throws Exception {
http.headers()
.httpStrictTransportSecurity()
.maxAgeInSeconds(31536000)
.includeSubDomains(true);
http.headers()
.contentTypeOptions();
http.cors().and()
.headers()
.xssProtection()
.and()
.contentSecurityPolicy("script-src 'self'")
.and()
.httpStrictTransportSecurity().includeSubDomains(true).maxAgeInSeconds(31536000)
.and()
.contentSecurityPolicy("frame-ancestors 'none'")
.and()
.frameOptions()
.deny()
.and()
.csrf()
.disable()
.formLogin().defaultSuccessUrl("/swagger-ui.html", true).and()
.authorizeRequests().antMatchers(AUTH_LIST).authenticated()
.antMatchers("/actuator/**").access("hasAnyRole('ADMIN') and hasIpAddress('127.0.0.1')")
.anyRequest().permitAll()
.and().httpBasic();
}

我最终通过配置前端web.xml文件解决了这个问题如果你使用nginx作为服务器,你可以添加:

报头总是设置X-Frame-Options "SAMEORIGIN">

如果你使用其他服务器,你可以检查mdn,

总而言之,如果你在一个全栈应用程序上工作,你必须配置服务器配置文件,否则spring安全性不会阻止你的点击劫持。

最新更新