Я пытаюсь сопоставить набор свойств из файла .properties в объекте POJO. Это потому, что я хочу повторно использовать свойства в классе @Configuration, а также потому, что мои файлы свойств зашифрованы, и я не хочу хранить пароли клиента OAuth в виде простого текста в классе конфигурации.
Вот POJO :
@ConfigurationProperties(ignoreInvalidFields = false, prefix = "oauth")
public class OAuthClient {
private Map<String, String> webapp = new LinkedHashMap<>();
private Map<String, String> admin = new LinkedHashMap<>();
public Map<String, String> getWebapp() {
return webapp;
}
public void setWebapp(Map<String, String> webapp) {
this.webapp = new LinkedHashMap<>(webapp);
}
public Map<String, String> getAdmin() {
return admin;
}
public void setAdmin(Map<String, String> admin) {
this.admin = new LinkedHashMap<>(admin);
}
}
Мне все хорошо до сих пор. Я выставляю OAuthClient
в bean-компоненте в следующем классе конфигурации:
@Configuration
@EnableConfigurationProperties(OAuthClient.class)
@PropertySource("classpath:oauth_clients.properties")
public class OAuthClientsConfiguration {
@Bean
public OAuthClient oAuthClient() {
return new OAuthClient();
}
}
Вот файл oauth_clients.properties
:
oauth.OAuthClient.webapp.id=client1
oauth.OAuthClient.webapp.secret=pass1
oauth.OAuthClient.admin.id=client2
oauth.OAuthClient.admin.secret=pass2
И, наконец, я хочу использовать свойства в мой пользовательский AuthorizationServerConfigurerAdapter
, , но приложение всегда не запускается с NPE при получении свойств от auwotired OAuthClient
.
Вот AuthorizationServerConfigurerAdapter
:
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
@Autowired
private PasswordEncoder passwordEncoder;
@Autowired
private OAuthClient oAuthClient;
...
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient(oAuthClient.getWebapp().get("id"))
.secret(passwordEncoder.encode(oAuthClient.getWebapp().get("secret")))
.authorizedGrantTypes(PASSWORD_GRANT, REFRESH_TOKEN_GRANT, AUTHORIZATION_CODE_GRANT)
.scopes(READ_SCOPE, WRITE_SCOPE)
.autoApprove(true)
.accessTokenValiditySeconds(60 * 60)
.refreshTokenValiditySeconds(6 * 60 * 60)
.and()
.withClient(oAuthClient.getAdmin().get("id"))
.secret(passwordEncoder.encode(oAuthClient.getAdmin().get("secret")))
.authorizedGrantTypes(PASSWORD_GRANT, REFRESH_TOKEN_GRANT, AUTHORIZATION_CODE_GRANT)
.scopes(READ_SCOPE, WRITE_SCOPE)
.autoApprove(true)
.authorities(ADMIN_AUTHORITY)
.accessTokenValiditySeconds(60 * 30)
.refreshTokenValiditySeconds(60 * 60);
}
Вот полная трассировка стека:
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.1.6.RELEASE:run (default-cli) on project safedrive-auth-server: An exception occurred while running. null: InvocationTargetException: Error creating bean with name 'org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerSecurityConfiguration': Injection of autowired dependencies failed; nested exception is java.lang.NullPointerException -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.1.6.RELEASE:run (default-cli) on project safedrive-auth-server: An exception occurred while running. null
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:216)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:120)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:355)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:216)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:160)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.MojoExecutionException: An exception occurred while running. null
at org.springframework.boot.maven.AbstractRunMojo$IsolatedThreadGroup.rethrowUncaughtException(AbstractRunMojo.java:511)
at org.springframework.boot.maven.RunMojo.runWithMavenJvm(RunMojo.java:93)
at org.springframework.boot.maven.AbstractRunMojo.run(AbstractRunMojo.java:243)
at org.springframework.boot.maven.AbstractRunMojo.execute(AbstractRunMojo.java:198)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:132)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
... 19 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:542)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerSecurityConfiguration': Injection of autowired dependencies failed; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:380)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1411)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:845)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:742)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:311)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1202)
at com.adrianpop.AuthServer.main(AuthServer.java:11)
... 6 more
Caused by: java.lang.NullPointerException
at org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder.encode(BCryptPasswordEncoder.java:80)
at com.adrianpop.config.AuthorizationServerConfiguration.configure(AuthorizationServerConfiguration.java:76)
at org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerSecurityConfiguration.configure(AuthorizationServerSecurityConfiguration.java:58)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:708)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374)
... 23 more
[ERROR]
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
Я изо всех сил пытался заставить это работать в течение нескольких часов, и это поражает мои нервы. Я также проверил официальную документацию, но код выглядит хорошо для меня. Я знаю, что упускаю что-то простое, но не могу понять, что именно.
Есть предложения? Спасибо.