【问题标题】:H2 console and Spring Security - permitAll() not workingH2 控制台和 Spring Security - permitAll() 不起作用
【发布时间】:2017-01-31 15:26:31
【问题描述】:

我正在创建 rest api 并实现 Spring Security - 一切正常,但我希望(目前,当我仍在开发时)能够让任何未经授权的人打开 localhost:8080/console。 我的代码:

@Override
protected void configure(HttpSecurity http) throws Exception {
    // allow everyone to register an account; /console is just for testing
    http.authorizeRequests().antMatchers("/register", "/console").permitAll();

    http.authorizeRequests().anyRequest().fullyAuthenticated();

    // making H2 console working
    http.headers().frameOptions().disable();

    /*
    https://docs.spring.io/spring-security/site/docs/current/reference/html/csrf.html#when-to-use-csrf-protection
    for non-browser APIs there is no need to use csrf protection
    */
    http.csrf().disable();
}

真正奇怪的是 - localhost:8080/register 不需要任何身份验证,但 /console 返回:

{
"timestamp": 1485876313847,
"status": 403,
"error": "Forbidden",
"message": "Access Denied",
"path": "/console"
}

有人知道怎么解决吗?

【问题讨论】:

    标签: java spring spring-security h2


    【解决方案1】:

    我通过以下方式解决了我的问题:

    http.headers().frameOptions().disable();
    

    【讨论】:

      【解决方案2】:

      在我的情况下也有同样的问题:

      csrf().ignoringAntMatchers("/h2-console/**")
      

      最终WebSecurityConfigurerAdapter:

      http.authorizeRequests().antMatchers("/").permitAll()
                  .and()
                  .authorizeRequests().antMatchers("/h2-console/**").permitAll()
                  .and()
                  .headers().frameOptions().disable()
                  .and()
                  .csrf().ignoringAntMatchers("/h2-console/**")
                  .and()
                  .cors().disable();
      

      【讨论】:

        【解决方案3】:

        我有类似的配置。可以试试吗?

        http
            .authorizeRequests()
                .antMatchers("/register").permitAll()
                .and()
            .authorizeRequests()
                .antMatchers("/console/**").permitAll();
        

        【讨论】:

        • 这也很好用,虽然不需要加倍 .authorazieRequests() 。 'http.authorizeRequests().antMatchers("/register", "/console/**").permitAll();'
        【解决方案4】:

        我通过以下方式解决了我的问题:

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

        【讨论】:

          猜你喜欢
          • 2016-02-16
          • 2018-01-31
          • 2017-08-20
          • 1970-01-01
          • 2019-12-12
          • 1970-01-01
          • 2019-09-27
          • 2020-10-02
          • 2018-04-17
          相关资源
          最近更新 更多