springBoot springSecurty导致的x-frame-options值为deny问题及跨域问题处理方法

1.问题解释说明:

X-Frame-Options HTTP 响应头是用来给浏览器 指示允许一个页面 可否在 <frame>, <iframe>, <embed> 或者 <object> 中展现的标记。站点可以通过确保网站没有被嵌入到别人的站点里面,从而避免被攻击。

X-Frame-Options 有三个可能的值:

X-Frame-Options: deny

X-Frame-Options: sameorigin

X-Frame-Options: allow-from url

换一句话说,如果设置为 deny,不光在别人的网站 frame 嵌入时会无法加载,在同域名页面中同样会无法加载。另一方面,如果设置为sameorigin,那么页面就可以在同域名页面的 frame 中嵌套。

deny

表示该页面不允许在 frame 中展示,即便是在相同域名的页面中嵌套也不允许。

sameorigin

表示该页面可以在相同域名页面的 frame 中展示。

allow-from uri

表示该页面可以在指定来源的 frame 中展示。

2.开发中遇到springBoot springSecurty导致x-frame-option出现deny问题的,项目如下类定义方法即可解决:

package cn.xxx.config.common;

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;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer.AuthorizedUrl;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    public WebSecurityConfig() {
    }

    protected void configure(HttpSecurity http) throws Exception {
        ((HttpSecurity)((AuthorizedUrl)((HttpSecurity)http.csrf().disable()).authorizeRequests().anyRequest()).permitAll().and()).logout().permitAll();
    }
}
package cn.xxx.config.security;

import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import cn.xxx.config.common.WebSecurityConfig;

@Configuration
@Order(1)
public class SecurityConfig extends WebSecurityConfig {
   
   @Override
   protected void configure(HttpSecurity http) throws Exception {
      //http.headers().frameOptions().disable(); // 任何跨域
      http.headers().frameOptions().sameOrigin(); // 同源跨域
      http.csrf().disable()
         .authorizeRequests()
         .anyRequest().permitAll()
         .and().logout().permitAll();
      
      http.cors().configurationSource(corsConfigurationSource());
   }
   
   //配置跨域访问资源
   private CorsConfigurationSource corsConfigurationSource() {
      CorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
      CorsConfiguration corsConfiguration = new CorsConfiguration();
      corsConfiguration.addAllowedOrigin("*"); // 同源配置,*表示任何请求都视为同源,若需指定ip和端口可以改为如“localhost:8080”,多个以“,”分隔;
      corsConfiguration.addAllowedHeader("*");// header,允许哪些header,本案中使用的是token,此处可将*替换为token;
      corsConfiguration.addAllowedMethod("*"); // 允许的请求方法,PSOT、GET等
      ((UrlBasedCorsConfigurationSource) source).registerCorsConfiguration("/**", corsConfiguration); // 配置允许跨域访问的url
      return source;
   }
}

3.若出现因为前端请求的不同ip和端口跨域问题导致的不能访问,项目如下类定义方法可解决:

package cn.xxx.config.cors;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class CorsConfig extends WebMvcConfigurerAdapter {

   @Override
   public void addCorsMappings(CorsRegistry registry) {
      // TODO Auto-generated method stub
      // super.addCorsMappings(registry);
      registry.addMapping("/**")
         .allowedOrigins("*")
         .allowCredentials(true)
         .allowedMethods("GET", "POST", "DELETE", "PUT")
         .maxAge(3600);
   }

   @Override
   public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {

      MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
      //设置日期格式
      ObjectMapper objectMapper = new ObjectMapper();
       SimpleDateFormat smt = new SimpleDateFormat("yyyy-MM-dd");
       objectMapper.setDateFormat(smt);
      mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper);
      //设置中文编码格式
      List<MediaType> list = new ArrayList<MediaType>();
      list.add(MediaType.APPLICATION_JSON_UTF8);
      mappingJackson2HttpMessageConverter.setSupportedMediaTypes(list);
      converters.add(mappingJackson2HttpMessageConverter);
      super.configureMessageConverters(converters);
   }


}

 

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

txp1993

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值