Tuh*_*rma 6 spring spring-security spring-boot
我们最近从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/**
Run Code Online (Sandbox Code Playgroud)
我什至明确允许 /h2-console/**
httpSecurity.authorizeRequests()
.antMatchers(allowedResources)
.permitAll()
Run Code Online (Sandbox Code Playgroud)
尝试访问时,我一直收到403 localhost:8080/h2-console。我尝试了许多设置以及放置:
management.security.enabled=true
security.basic.enabled=true
Run Code Online (Sandbox Code Playgroud)
但是我无法访问h2-console。
Par*_*ala 58
由于 H2 有它自己的身份验证提供程序,您可以完全按照与静态内容相同的方式跳过 h2 控制台路径的 Spring Security。
为了做到这一点,在您的 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/**");
}
Run Code Online (Sandbox Code Playgroud)
如果您在生产环境中使用 h2,请确保为您的 h2 控制台设置适当的安全措施(例如,设置不明显的路径、正确的密码、ip 白名单)。
arg*_*oth 24
Spring安全性阻止H2数据库的/ h2_console(或您在application.yaml中配置的路径)路径。
要访问H2控制台,只需将以下代码添加到WebSecurityConfigurer中。
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/h2_console/**").permitAll();
http.csrf().disable();
http.headers().frameOptions().disable();
}
}
Run Code Online (Sandbox Code Playgroud)
不要在生产环境中使用此配置。=)
小智 8
使用 Spring Boot 3,以下内容对我有用:
@Configuration
class securityConfig {
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
return (web) -> web.ignoring().requestMatchers(new AntPathRequestMatcher("/h2-console/**"));
}
}
Run Code Online (Sandbox Code Playgroud)
我想提供与 @argoth 提议的配置类似的配置,但更适合生产:)
@Profile("h2") // to make sure it is active only if h2 profile is active
@Configuration
@ConditionalOnProperty( //to make sure it is active if console is enabled
value="spring.h2.console.enabled",
havingValue = "true",
matchIfMissing = false)
public class H2SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// this may not be required, depends on your app configuration
http.authorizeRequests()
// we need config just for console, nothing else
.antMatchers("/h2_console/**").permitAll();
// this will ignore only h2-console csrf, spring security 4+
http.csrf().ignoringAntMatchers("/h2-console/**");
//this will allow frames with same origin which is much more safe
http.headers().frameOptions().sameOrigin();
}
}
Run Code Online (Sandbox Code Playgroud)
事实上,在 boot 1.3 中也有类似的配置,称为 H2ConsoleSecurityConfiguration,但现在它已经消失了: Old class
更新。这里非常重要的注意事项!当您有多个时WebSecurityConfigurerAdapter,它们可能会相互冲突,因此如果您WebSecurityConfigurerAdapter的代码中有另一个,则需要以某种方式合并它们。为了让您更详细地了解为什么会发生冲突,冲突的发生是由于每个适配器都设置了自己的过滤器链,并且每个请求都必须通过两个过滤器链。如果其中一个链禁止frameOptions,而其他链则不禁止,则请求将不会通过第一个链。也就是说,请小心使用多个配置器。
尽管得票最高的答案是正确的。
截至目前,WebSecurityConfigurerAdapter 在较新的 Spring Security 版本中已被弃用,解决方法是为WebSecurityCustomizer. 下面安全配置类中的 Bean 就可以实现这一点。
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
return (web) -> web.ignoring().antMatchers("/h2-console/**");
}
Run Code Online (Sandbox Code Playgroud)
我启用了调试日志记录并看到了这一点:
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
Run Code Online (Sandbox Code Playgroud)
我意识到我的用户没有ROLE_USER. 我假设ROLE_ADMIN>ROLE_USER但我仍然需要更好地理解这一点。
我将我的设置更新为:
security:
basic:
enabled: true
authorize-mode: NONE
Run Code Online (Sandbox Code Playgroud)
我能够访问/h2-console/**现在。
| 归档时间: |
|
| 查看次数: |
8632 次 |
| 最近记录: |