springBoot springSecurty x-frame-options deny

项目中用到iframe嵌入网页,然后用到springsecurity就被拦截了 浏览器报错  x-frame-options deny
原因是因为springSecurty使用X-Frame-Options防止网页被Frame


解决办法把x-frame-options disable即可

protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/js/**","/css/**","/img/**","/layui/**","/layouts/**","/font-awesome/**").permitAll()
                .anyRequest().authenticated()  // "/index", 
                .and()
                .headers().frameOptions().disable()
                .and()
            .formLogin()
                .loginPage("/login")
                .defaultSuccessUrl("/index", true)
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }

Spring Boot使用Jetty作为Web容器时,配置X-Frame-Options HTTP头是为了防止网页被嵌入到其他网站(跨站嵌入,Clickjacking)的安全措施。默认情况下,如果你没有在Spring Boot应用的配置显式设置这个头部,可能是因为Jetty的默认安全策略没有包含这一项。 要为Spring Boot应用配置X-Frame-Options,你需要在Spring Boot的`application.properties`或`application.yml`文件添加相关的配置,或者直接在Java代码进行配置。具体步骤如下: 1. **在`application.properties`**: ```properties server.servlet.context-path=/ # 如果你的应用运行在根路径,不需此项 server.jetty.xframeoptions.enabled=true server.jetty.xframeoptions.value=SAMEORIGIN # 可选值有SAMEORIGIN, SAMEsite,或DENY ``` 2. **在`application.yml`**: ```yaml server: servlet: context-path: / jetty: x-frame-options: enabled: true value: SAMEORIGIN ``` 3. **在Java代码动态配置**: ```java @Configuration public class WebSecurityConfig { @Bean public ServerCustomizer jettyServerCustomizer() { return (server -> { server.setHandler(new HandlerWrapper() { @Override protected void doHandle(ServletRequest request, ServletResponse response, boolean containsError) throws IOException, ServletException { response.setHeader("X-Frame-Options", "SAMEORIGIN"); super.doHandle(request, response, containsError); } }); }); } } ``` 确保你重启应用后,`X-Frame-Options`头应该会被设置并生效。如果问题仍然存在,检查一下是否有其他间件或配置冲突,或者检查日志以获取更多关于配置执行的详细信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值