我正在尝试使用official documentation中所述的Spring OAuth2 Autoconfig设置新项目。我的设置如下:
build.gradle
subprojects {
...
dependencies {
testCompile("io.projectreactor:reactor-test")
testCompile("org.springframework.boot:spring-boot-starter-test")
testCompile("org.springframework.security:spring-security-test")
compileOnly("org.projectlombok:lombok")
compile("org.springframework.boot:spring-boot-starter-webflux")
compile("org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:2.1.0.M2")
compile("org.springframework.security.oauth:spring-security-oauth2:2.3.3.RELEASE")
compile("org.springframework.boot:spring-boot-starter-security")
}
}
Application.java
@SpringBootApplication
@EnableAuthorizationServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
@PropertySource("classpath:service.properties")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
AuthenticationConfiguration.java
@Configuration
public class AuthenticationConfiguration {
@Bean
UserDetailsService userDetailsService() {
UserDetails greg = User.withUsername("user")
.password("{bcrypt}$2a$10$dXJ3SW6G7P50lGmMkkmwe.20cQQubK3.HZWzG3YB1tlRy.fqvM/BG")
.roles("read")
.build();
return new InMemoryUserDetailsManager(greg);
}
}
service.properties
security.oauth2.client.client-id=webapp
security.oauth2.client.client-secret=secret
根据documentation,这是我需要的最小设置。但不幸的是,在构建项目时,spring无法导入一些bean。似乎缺少某些过滤器。这是一个堆栈跟踪:
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [com.example.Application]; nested exception is java.lang.IllegalStateException: Failed to introspect annotated methods on class org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerSecurityConfiguration
at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:646) ~[spring-context-5.1.0.RC2.jar:5.1.0.RC2]
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:303) ~[spring-context-5.1.0.RC2.jar:5.1.0.RC2]
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245) ~[spring-context-5.1.0.RC2.jar:5.1.0.RC2]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:202) ~[spring-context-5.1.0.RC2.jar:5.1.0.RC2]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:170) ~[spring-context-5.1.0.RC2.jar:5.1.0.RC2]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:315) ~[spring-context-5.1.0.RC2.jar:5.1.0.RC2]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:232) ~[spring-context-5.1.0.RC2.jar:5.1.0.RC2]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275) ~[spring-context-5.1.0.RC2.jar:5.1.0.RC2]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:95) ~[spring-context-5.1.0.RC2.jar:5.1.0.RC2]
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:693) ~[spring-context-5.1.0.RC2.jar:5.1.0.RC2]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:530) ~[spring-context-5.1.0.RC2.jar:5.1.0.RC2]
at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.refresh(ReactiveWebServerApplicationContext.java:61) ~[spring-boot-2.1.0.M2.jar:2.1.0.M2]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:785) [spring-boot-2.1.0.M2.jar:2.1.0.M2]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:418) [spring-boot-2.1.0.M2.jar:2.1.0.M2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:337) [spring-boot-2.1.0.M2.jar:2.1.0.M2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1269) [spring-boot-2.1.0.M2.jar:2.1.0.M2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1257) [spring-boot-2.1.0.M2.jar:2.1.0.M2]
at com.example.Application.main(Application.java:30) [classes/:na]
Caused by: java.lang.IllegalStateException: Failed to introspect annotated methods on class org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerSecurityConfiguration
at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:169) ~[spring-core-5.1.0.RC2.jar:5.1.0.RC2]
at org.springframework.context.annotation.ConfigurationClassParser.retrieveBeanMethodMetadata(ConfigurationClassParser.java:393) ~[spring-context-5.1.0.RC2.jar:5.1.0.RC2]
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:318) ~[spring-context-5.1.0.RC2.jar:5.1.0.RC2]
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245) ~[spring-context-5.1.0.RC2.jar:5.1.0.RC2]
at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:636) ~[spring-context-5.1.0.RC2.jar:5.1.0.RC2]
... 17 common frames omitted
Caused by: java.lang.NoClassDefFoundError: javax/servlet/Filter
at java.base/java.lang.Class.getDeclaredMethods0(Native Method) ~[na:na]
at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3119) ~[na:na]
at java.base/java.lang.Class.getDeclaredMethods(Class.java:2268) ~[na:na]
at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:158) ~[spring-core-5.1.0.RC2.jar:5.1.0.RC2]
... 21 common frames omitted
Caused by: java.lang.ClassNotFoundException: javax.servlet.Filter
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582) ~[na:na]
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:190) ~[na:na]
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499) ~[na:na]
... 25 common frames omitted