H2控制台和Spring Security - permitAll()无效

时间:2017-01-31 15:26:32

标签: java spring spring-security h2

我创建了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"
}

任何人都知道如何修复它?

3 个答案:

答案 0 :(得分:4)

在我的情况下,

有同样的问题:

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

答案 1 :(得分:3)

我解决了我的问题:

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

答案 2 :(得分:1)

我有类似的配置。你能试试吗?

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