提问者:小点点

H2控制台和Spring Security-permitAll()不工作


我正在创建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个答案

匿名用户

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

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

最终WebSecurityConfigrerAdapter

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

匿名用户

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

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

匿名用户

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

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