将spring-security-web
依赖项从4.2.20.RELEASE
升级到5.6.9
后,我收到以下异常。
无法在anyRequest之后配置它
下面是详细的日志记录信息。
Caused by: java.lang.IllegalStateException: Can't configure anyRequest after itself
at org.springframework.util.Assert.state(Assert.java:76) ~[spring-core-5.3.17.jar:5.3.17]
at org.springframework.security.config.annotation.web.AbstractRequestMatcherRegistry.anyRequest(AbstractRequestMatcherRegistry.java:76) ~[spring-security-config-5.6.9.jar:5.6.9]
at com.premierinc.trs.user.config.ProvisioningSecurityConfig.configure(ProvisioningSecurityConfig.java:32) ~[user-1.5.6.1-SNAPSHOT.jar:?]
at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.getHttp(WebSecurityConfigurerAdapter.java:217) ~[spring-security-config-5.6.9.jar:5.6.9]
at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.init(WebSecurityConfigurerAdapter.java:315) ~[spring-security-config-5.6.9.jar:5.6.9]
at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.init(WebSecurityConfigurerAdapter.java:93) ~[spring-security-config-5.6.9.jar:5.6.9]
at com.premierinc.trs.user.config.ProvisioningSecurityConfig$$EnhancerBySpringCGLIB$$8b2eea9c.init(<generated>) ~[user-1.5.6.1-SNAPSHOT.jar:?]
at org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder.init(AbstractConfiguredSecurityBuilder.java:338) ~[spring-security-config-5.6.9.jar:5.6.9]
at org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder.doBuild(AbstractConfiguredSecurityBuilder.java:300) ~[spring-security-config-5.6.9.jar:5.6.9]
at org.springframework.security.config.annotation.AbstractSecurityBuilder.build(AbstractSecurityBuilder.java:38) ~[spring-security-config-5.6.9.jar:5.6.9]
at org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration.springSecurityFilterChain(WebSecurityConfiguration.java:127) ~[spring-security-config-5.6.9.jar:5.6.9]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_341]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_341]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_341]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_341]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.3.17.jar:5.3.17]
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-5.3.17.jar:5.3.17]
... 64 more
下面是我的ProvisioningSecurityConfig.java
文件,上面的日志在代码的第32行给出了异常:
@Configuration
@EnableWebSecurity
public class ProvisioningSecurityConfig extends WebSecurityConfigurerAdapter {
private static final Logger logger = LoggerFactory.getLogger(ProvisioningSecurityConfig.class);
@Value("${provision.ldapGroupWithServiceAccess:SERVICE_ACCESS_APP_PROV}")
private String ldapGroupWithServiceAccess;
@Override
protected void configure(HttpSecurity http) throws Exception {
logger.info(String.format("provision.ldapGroupWithServiceAccess->%s", ldapGroupWithServiceAccess));
http.csrf().disable();
// Do not create sessions - authenticate user on every request
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
final String relativeProvisionEndpointPattern = VUserUris.ROOT + VUserUris.PROVISION;
final String fullProvisionEndpointPattern = CommonUris.SERVLET_ROOT + relativeProvisionEndpointPattern;
http
.requestMatchers()
.antMatchers(fullProvisionEndpointPattern + "/**", fullProvisionEndpointPattern + "*",
relativeProvisionEndpointPattern + "/**", relativeProvisionEndpointPattern + "*")
.and()
.authorizeRequests()
.anyRequest().authenticated()
.and()
.httpBasic()
.and()
.anonymous().disable();
http
.requestMatchers()
.antMatchers(fullProvisionEndpointPattern + "/**", fullProvisionEndpointPattern + "*",
relativeProvisionEndpointPattern + "/**", relativeProvisionEndpointPattern + "*")
.and()
.authorizeRequests()
.anyRequest().hasRole(ldapGroupWithServiceAccess); //line 32
}
@Configuration
protected static class AuthenticationConfiguration extends GlobalAuthenticationConfigurerAdapter {
private static final Logger logger = LoggerFactory.getLogger(AuthenticationConfiguration.class);
@Value("${provision.ldapUrl:ldap://ldapserver:589/}")
private String ldapUrl;
@Value("${provision.ldapUserDnPattern:uid={0},ou=applicationIds,ou=Users,o=vishnu.com}")
private String ldapUserDnPattern;
@Value("${provision.ldapGroupSearchBase:ou=Groups,o=vishnu.com}")
private String ldapGroupSearchBase;
@Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
logger.info(String.format("provision.ldapUrl->%s", ldapUrl));
logger.info(String.format("provision.ldapUserDnPattern->%s", ldapUserDnPattern));
logger.info(String.format("provision.ldapGroupSearchBase->%s", ldapGroupSearchBase));
DefaultSpringSecurityContextSource context = new DefaultSpringSecurityContextSource(ldapUrl);
context.afterPropertiesSet();
auth.ldapAuthentication()
.userDnPatterns(ldapUserDnPattern)
.groupSearchBase(ldapGroupSearchBase)
.contextSource(context);
}
}
}
1条答案
按热度按时间whlutmcx1#
您将调用
anyRequest
两次,将您的授权规则合并到一个调用中,如下所示: