admin管理员组文章数量:1406052
I'd like to hide Keycloak behind spring cloud gateway but still use it as OIDC provider. I've managed to permit /auth
(keycloak's) endpoint for that but now I'm fighting with http basic auth which IDK how to disable. Everytime I try to hit any other endpoint which should be protected by keycloak I get browser form login. With following config
@Bean
@Order(1)
SecurityWebFilterChain publicEndpoints(final ServerHttpSecurity http) {
return http.authorizeExchange(auth ->
auth.pathMatchers("/auth", "/auth/**").permitAll())
.csrf(ServerHttpSecurity.CsrfSpec::disable)
.cors(ServerHttpSecurity.CorsSpec::disable)
.formLogin(ServerHttpSecurity.FormLoginSpec::disable)
.headers(c -> c.frameOptions(ServerHttpSecurity.HeaderSpec.FrameOptionsSpec::disable))
.httpBasic(basic ->
basic.authenticationEntryPoint(new HttpStatusServerEntryPoint(HttpStatus.UNAUTHORIZED)))
.build();
}
@Bean
@Order(2)
SecurityWebFilterChain springSecurityFilterChain(final ServerHttpSecurity http) {
return http.authorizeExchange(auth -> auth.anyExchange().authenticated())
.oauth2Login(withDefaults())
.oauth2ResourceServer((oauth2) -> oauth2.jwt(withDefaults()))
.csrf(ServerHttpSecurity.CsrfSpec::disable)
.cors(ServerHttpSecurity.CorsSpec::disable)
.formLogin(ServerHttpSecurity.FormLoginSpec::disable)
.headers(c -> c.frameOptions(ServerHttpSecurity.HeaderSpec.FrameOptionsSpec::disable))
//.httpBasic(ServerHttpSecurity.HttpBasicSpec::disable)
.build();
}
I get error on application startup:
Caused by: .springframework.beans.BeanInstantiationException: Failed to instantiate [.springframework.security.web.server.SecurityWebFilterChain]: Factory method 'publicEndpoints' threw exception with message: authenticationManager cannot be null
2025-03-06T15:35:01.907890843Z at .springframework.beans.factory.support.SimpleInstantiationStrategy.lambda$instantiate$0(SimpleInstantiationStrategy.java:199) ~[spring-beans-6.2.3.jar:6.2.3]
2025-03-06T15:35:01.907892426Z at .springframework.beans.factory.support.SimpleInstantiationStrategy.instantiateWithFactoryMethod(SimpleInstantiationStrategy.java:88) ~[spring-beans-6.2.3.jar:6.2.3]
2025-03-06T15:35:01.907893385Z at .springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:168) ~[spring-beans-6.2.3.jar:6.2.3]
2025-03-06T15:35:01.907894343Z at .springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-6.2.3.jar:6.2.3]
2025-03-06T15:35:01.907895260Z ... 39 common frames omitted
since I don't want to use any auth manager for publicEndpoints
I don't understand how to configure it to disable form login and use oauth
I'd like to hide Keycloak behind spring cloud gateway but still use it as OIDC provider. I've managed to permit /auth
(keycloak's) endpoint for that but now I'm fighting with http basic auth which IDK how to disable. Everytime I try to hit any other endpoint which should be protected by keycloak I get browser form login. With following config
@Bean
@Order(1)
SecurityWebFilterChain publicEndpoints(final ServerHttpSecurity http) {
return http.authorizeExchange(auth ->
auth.pathMatchers("/auth", "/auth/**").permitAll())
.csrf(ServerHttpSecurity.CsrfSpec::disable)
.cors(ServerHttpSecurity.CorsSpec::disable)
.formLogin(ServerHttpSecurity.FormLoginSpec::disable)
.headers(c -> c.frameOptions(ServerHttpSecurity.HeaderSpec.FrameOptionsSpec::disable))
.httpBasic(basic ->
basic.authenticationEntryPoint(new HttpStatusServerEntryPoint(HttpStatus.UNAUTHORIZED)))
.build();
}
@Bean
@Order(2)
SecurityWebFilterChain springSecurityFilterChain(final ServerHttpSecurity http) {
return http.authorizeExchange(auth -> auth.anyExchange().authenticated())
.oauth2Login(withDefaults())
.oauth2ResourceServer((oauth2) -> oauth2.jwt(withDefaults()))
.csrf(ServerHttpSecurity.CsrfSpec::disable)
.cors(ServerHttpSecurity.CorsSpec::disable)
.formLogin(ServerHttpSecurity.FormLoginSpec::disable)
.headers(c -> c.frameOptions(ServerHttpSecurity.HeaderSpec.FrameOptionsSpec::disable))
//.httpBasic(ServerHttpSecurity.HttpBasicSpec::disable)
.build();
}
I get error on application startup:
Caused by: .springframework.beans.BeanInstantiationException: Failed to instantiate [.springframework.security.web.server.SecurityWebFilterChain]: Factory method 'publicEndpoints' threw exception with message: authenticationManager cannot be null
2025-03-06T15:35:01.907890843Z at .springframework.beans.factory.support.SimpleInstantiationStrategy.lambda$instantiate$0(SimpleInstantiationStrategy.java:199) ~[spring-beans-6.2.3.jar:6.2.3]
2025-03-06T15:35:01.907892426Z at .springframework.beans.factory.support.SimpleInstantiationStrategy.instantiateWithFactoryMethod(SimpleInstantiationStrategy.java:88) ~[spring-beans-6.2.3.jar:6.2.3]
2025-03-06T15:35:01.907893385Z at .springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:168) ~[spring-beans-6.2.3.jar:6.2.3]
2025-03-06T15:35:01.907894343Z at .springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-6.2.3.jar:6.2.3]
2025-03-06T15:35:01.907895260Z ... 39 common frames omitted
since I don't want to use any auth manager for publicEndpoints
I don't understand how to configure it to disable form login and use oauth
1 Answer
Reset to default 0When using more than one filter chain, all but the last one in @Order
must contain a securityMatcher
definition to limit the requests it is applied to, or the next in @Order
are never tried. So, in your case, add .securityMatcher(...)
to your 1st filter chain.
As a side note, there are 2 issues with your second filter chain:
- you should not mix stateful (session-based like
oauth2Login
&formLogin
are) and stateless (no session likeoauth2ResourceServer
&basic
) request authorization in the same filter chain - you should not disable CSRF protection in a stateful filter chain (your second filter chain is stateful because of
oauth2Login
)
本文标签: Hiding keycloak behind spring cloud gateway and disabling basic authStack Overflow
版权声明:本文标题:Hiding keycloak behind spring cloud gateway and disabling basic auth - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744964854a2634884.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
publicEndpoints
fixed the problem.securityMatcher(ServerWebExchangeMatchers.pathMatchers("/auth", "/auth/**"))
– bilak Commented Mar 6 at 16:17