gpt4 book ai didi

spring-boot - 从 Spring Security OAuth2 迁移到 Keycloak 时如何解决 `permitAll only works with HttpSecurity.authorizeRequests()`?

转载 作者:行者123 更新时间:2023-12-04 14:16:19 27 4
gpt4 key购买 nike

我目前正在从 Spring Security OAuth2 迁移到 Keycloak(在 Spring Security 团队决定弃用 Spring Security OAuth2 项目之后),但我遇到了这个异常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.IllegalStateException: permitAll only works with HttpSecurity.authorizeRequests()
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:656)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:484)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
at org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$330.00000000ED7F64A0.getObject(Unknown Source)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)

我有 10 个微服务要迁移,我定义了一个 common jar 包含所有配置。这是 common 中的 Keycloak 配置:
@EnableGlobalMethodSecurity( prePostEnabled = true, securedEnabled = true )
@KeycloakConfiguration
@Import( KeycloakSpringBootConfigResolver.class )
public class CommonsKeycloakSecurityConfigurerAdapter extends KeycloakWebSecurityConfigurerAdapter {

@Override
protected void configure( HttpSecurity http ) throws Exception {
http.oauth2ResourceServer(); // Equivalent to @EnableResourceServer in S.S. OAuth2
super.configure( http );
}


@Autowired
public void configureGlobal( AuthenticationManagerBuilder auth ) {

SimpleAuthorityMapper grantedAuthorityMapper = new SimpleAuthorityMapper();
grantedAuthorityMapper.setPrefix( "ROLE_" );
grantedAuthorityMapper.setConvertToUpperCase( true );

KeycloakAuthenticationProvider keycloakAuthenticationProvider = keycloakAuthenticationProvider();
keycloakAuthenticationProvider.setGrantedAuthoritiesMapper( grantedAuthorityMapper );
auth.authenticationProvider( keycloakAuthenticationProvider );
}

@Value("${spring.application.name}"
public String APP_NAME;

@Bean
public SessionAuthenticationStrategy sessionAuthenticationStrategyPorvider() {
return APP_NAME.equals( "securityservice")
? new RegisterSessionAuthenticationStrategy( new SessionRegistryImpl() )
: new NullAuthenticatedSessionStrategy();
}

@Bean
@DependsOn( "sessionAuthenticationStrategyPorvider" )
@Override
protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
return sessionAuthenticationStrategyPorvider();
}

@Bean
@Scope( scopeName = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS )
public KeycloakSecurityContext provideKeycloakSecurityContext() {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
Principal principal = requireNonNull( attributes ).getRequest().getUserPrincipal();
if ( principal == null ) {
return null;
}
if ( principal instanceof KeycloakAuthenticationToken ) {
principal = (Principal) ( (KeycloakAuthenticationToken) principal ).getPrincipal();
}
if ( principal instanceof KeycloakPrincipal ) {
return ( (KeycloakPrincipal) principal ).getKeycloakSecurityContext();
}
return null;
}

}

和属性在 common/src/main/resources/application.yml :
keycloak:
auth-server-url: "" #defined in application-dev.yml and application-prod.yml
realm: ${project.name}
resource: ${spring.application.name}
credentials:
secret: ${application.kck.secret}
use-resource-role-mappings: true
ssl-required: 'none'
principal-attribute: preferred_username
common.jar然后导入到所有其他微服务中。在 productservice我已经定义了这些特定的 http 安全规则:
@Configuration
@Order( 1 )
public class UIResourceProtection extends WebSecurityConfigurerAdapter { // Note that I use WebSecurityConfigurerAdapter instead of KeycloakWebSecurityConfigurerAdapter

@Override
public void configure( HttpSecurity http ) throws Exception {
http.sessionManagement().sessionCreationPolicy( STATELESS );
http.requestMatchers().antMatchers( "/ui/product/**" )
.and()
.cors().and()
.authorizeRequests()
.antMatchers( "/ui/product/private").hasRole( "USER" )
.antMatchers( HttpMethod.PUT, "/ui/product/public" ).hasRole( "USER" )
.antMatchers( HttpMethod.GET, ""/ui/product/cost"","/ui/product/public").authenticated()
.antMatchers( HttpMethod.GET, "/ui/product/public/{id}" ).authenticated()
.antMatchers( "/ui/product/public/dashboard" ).hasRole( "USER" );
}
}

和同一个项目中的另一个(即: productservice)
@Configuration
@RequiredArgsConstructor
@Order( 2 )
public class SelfResourceProtection extends WebSecurityConfigurerAdapter { // Note that I use WebSecurityConfigurerAdapter instead of KeycloakWebSecurityConfigurerAdapter

@Override
public void configure( HttpSecurity http ) throws Exception {
http.requestMatchers().antMatchers("/product/**")
.and()
.authorizeRequests()
.antMatchers( HttpMethod.GET, "/product/update-db" ).hasRole( "ADMIN" )
.antMatchers( HttpMethod.GET, "/product/private/{id}" ).permitAll();
}
}

当我开始时 productservice使用该配置,我有上面的异常(exception): Caused by: java.lang.IllegalStateException: permitAll only works with HttpSecurity.authorizeRequests()
更改配置参数后,仍然抛出异常。

知道如何解决吗?

依赖版本:
Spring Boot : 2.2.4.RELEASE
Keycloak : 8.0.1
Spring Security: 5.2.1.RELEASE
Spring Security OAuth2 Resource Server: 5.2.1.RELEASE

非常感谢

最佳答案

此异常是由类 CommonsKeycloakSecurityConfigurerAdapter 引起的.
如果您查看 KeycloakWebSecurityConfigurerAdapter 的源代码您正在扩展,您会注意到部分配置状态

.logoutUrl("/sso/logout").permitAll()

为了 permitAll()被应用,Spring Security 预计 authorizeRequests()进行配置。

由于 KeycloakWebSecurityConfigurerAdapter中没有这样的配置,您需要添加一个 authorizeRequests()阻止扩展它的自定义配置。

关于spring-boot - 从 Spring Security OAuth2 迁移到 Keycloak 时如何解决 `permitAll only works with HttpSecurity.authorizeRequests()`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59919562/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com