Spring Boot /h2-console 在 Spring Security 1.5.2 中抛出 403

新手上路,请多包涵

我们最近从 Spring Boot 1.4.1 升级到 1.5.2。 1.5.2 的特性之一是,如果 Spring Security 是包的一部分,那么它会受到基本身份验证的保护。即使在基本身份验证之后,我也无法访问 /h2-console 。它抛出 403 禁止。

application.yml :

 spring:
  datasource:
    driver-class-name: org.h2.Driver
    url: jdbc:h2:file:../app-db/app_db;AUTO_SERVER=TRUE
    username: sa
    password: sa
    initialize: false
  jpa:
    hibernate:
      ddl-auto: validate
    show-sql: true
    database-platform: org.hibernate.dialect.H2Dialect
  h2:
    console:
      enabled: true
      settings:
        web-allow-others: true
  allowed:
    resources: /h2-console/**

我什至明确允许 /h2-console/**

  httpSecurity.authorizeRequests()
                .antMatchers(allowedResources)
                .permitAll()

尝试访问时,我不断收到 403 localhost:8080/h2-console 。我尝试了许多设置以及放置:

 management.security.enabled=true
security.basic.enabled=true

但我无法访问 h2 控制台。

原文由 Tuhin Kanti Sharma 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 937
2 个回答

我启用了调试日志并看到了这个:

 o.s.s.w.a.i.FilterSecurityInterceptor    : Secure object: FilterInvocation: URL: /h2-console/; Attributes: [hasAnyRole('ROLE_USER','ROLE_ACTUATOR')]
2017-05-05 13:16:09.304 DEBUG 90365 --- [nio-8080-exec-2] o.s.s.w.a.i.FilterSecurityInterceptor    : Previously Authenticated: org.springframework.security.authentication.UsernamePasswordAuthenticationToken@33d2af72: Principal: org.springframework.security.ldap.userdetails.LdapUserDetailsImpl@7371d5f4: Dn: cn=XYZ,ou=XYZ,ou=Active,ou=ABC_USERS,dc=internal,dc=organization,dc=com; Username: uname; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; CredentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: ROLE_ADMIN; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@b364: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: 86EF50EF548ED4DBCE4D661AEC93F88C; Granted Authorities: ROLE_ADMIN
2017-05-05 13:16:09.305 DEBUG 90365 --- [nio-8080-exec-2] o.s.s.access.vote.AffirmativeBased       : Voter: org.springframework.security.web.access.expression.WebExpressionVoter@51d3d69, returned: -1
2017-05-05 13:16:09.305 DEBUG 90365 --- [nio-8080-exec-2] o.s.s.w.a.ExceptionTranslationFilter     : Access is denied (user is not anonymous); delegating to AccessDeniedHandler

我意识到我的用户没有 ROLE_USER 。我假设 ROLE_ADMIN > ROLE_USER 但我仍然需要更好地理解这一点。

我将设置更新为:

 security:
  basic:
    enabled: true
    authorize-mode: NONE

我现在可以访问 /h2-console/**

原文由 Tuhin Kanti Sharma 发布,翻译遵循 CC BY-SA 3.0 许可协议

由于 H2 有自己的身份验证提供程序,因此您可以完全跳过 Spring Security 以获取 h2 控制台的路径,就像处理静态内容一样。

为了做到这一点,在您的 Spring 安全配置中,您必须覆盖将 org.springframework.security.config.annotation.web.builders.WebSecurity 实例作为参数而不是采用 --- org.springframework.security.config.annotation.web.builders.HttpSecurity --- 实例的配置方法

    @Override
    public void configure(WebSecurity web) throws Exception {
        web
            .ignoring()
            .antMatchers("/h2-console/**");
    }

如果您在生产环境中使用 h2,请确保为您的 h2 控制台设置了适当的安全措施(例如,设置非显而易见的路径、正确的密码、ip 白名单)。

原文由 Parakrama Dharmapala 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题