spring security 'X-Frame-Options' to 'deny'.

本文详细介绍了在SpringBoot项目中引入Spring Security后遇到的X-Frame-Options导致的页面加载错误问题及其解决方案。通过覆盖configure(HttpSecurity http)方法,禁用frameOptions,解决页面框架无法获取子页面的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

springboot 2.1.3 RELEASE

引入security

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

html使用了 frameset 标签,在请求页面的时候就会报错,类似

'X-Frame-Options' to 'deny'.

解决办法

在  WebSecurityConfigurerAdapter 的继承实现类里面覆盖  configure(HttpSecurity http)

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.headers().frameOptions().disable().// 解决页面框架不能获取子页面
                and().
                csrf().disable().authorizeRequests()
                .antMatchers("/login","/regist","/user/regist")
                .permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .formLogin()
                .loginProcessingUrl("/user/login")
                .permitAll()
                .successHandler((httpServletRequest, httpServletResponse, authentication) -> {
                    System.out.println("登陆成功处理==============");
                    //跳转到首页
                    httpServletResponse.sendRedirect("/main");
                })
                .failureHandler((httpServletRequest, httpServletResponse, e) -> {
                    System.out.println("登陆失败处理=============");
                    //返回到登陆页面
                    httpServletResponse.sendRedirect("/login");
                })
                .and()
                //登出
                .logout()
                .logoutSuccessHandler(((httpServletRequest, httpServletResponse, authentication) -> {
                    System.out.println("登出成功处理=============");
                    httpServletResponse.sendRedirect("/login");
                }));
//                .and()
//                .exceptionHandling()
//                .authenticationEntryPoint(((httpServletRequest, httpServletResponse, e) -> {
//                    //应该回到当前页面,并且状态不变
//                }));
    }

有注释  // 解决页面框架不能获取子页面     那里就是解决的代码了,网上都没有加上.add(),不完整。

### 配置 X-Frame-Options 头 为了在 Spring Security 6.4.1 中配置 `X-Frame-Options` HTTP 安全响应头,可以利用内置的安全配置方法。此头部用于指示浏览器是否应该允许在一个 `<frame>` 或 `<iframe>` 中加载页面,以此预防点击劫持攻击。 通过自定义安全配置类并重载其配置方法,可以在应用程序中设置该头部。具体来说,在扩展了 `WebSecurityConfigurerAdapter` 的配置类中的 `configure(HttpSecurity http)` 方法内实现这一功能[^1]: ```java @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.headers() .frameOptions().policy(FrameOptionsPolicy.DENY); // Other configurations... } } ``` 上述代码片段展示了如何拒绝任何框架加载当前网页的方式之一——即调用 `.frameOptions().policy(FrameOptionsPolicy.DENY)` 来阻止所有尝试嵌入页面的行为。除了 `DENY` 政策外,还可以选择其他两种政策:`SAMEORIGIN` 和 `ALLOW_FROM uri`(后者仅适用于某些旧版本)。对于大多数情况而言,推荐使用 `SAMEORIGIN` 策略以确保只有来自同一源的页面才能将本页作为 iframe 加载。 如果正在使用的不是继承自 `WebSecurityConfigurerAdapter` 的方式,则可以直接在 `@Bean` 注解的方法里创建 `SecurityFilterChain` 实现相同的效果: ```java @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http.headers() .frameOptions().sameOrigin(); // 使用 SAMEORIGIN 策略 // 其他配置... return http.build(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值