【问题标题】:X-Frame DENY in Spring securitySpring 安全性中的 X-Frame DENY
【发布时间】:2016-08-04 19:56:35
【问题描述】:

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

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请求:

这是我的弹簧配置:

@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();

        }
    }

我该如何解决这个问题?谢谢(尽管出现错误,下载工作正常)

【问题讨论】:

    标签: jquery ajax spring spring-security x-frame-options


    【解决方案1】:

    你可以在你的 spring 安全配置文件中这样做:

    <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();
        }
    }
    

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

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

    代替:

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

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

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

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

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

    【讨论】:

      【解决方案2】:

      试试

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

      【讨论】:

      • 您能否向我们提供与您的代码相关的更多详细信息。?谢谢
      • @Robert 我已经详细回答了这个问题,以防万一。
      猜你喜欢
      • 2017-07-29
      • 1970-01-01
      • 2017-07-25
      • 1970-01-01
      • 1970-01-01
      • 2017-08-06
      • 1970-01-01
      • 2015-09-17
      • 2016-11-11
      相关资源
      最近更新 更多