我搜索了高低,我仍然无法找到这个非常恼人的问题的简单答案,
我遵循了这个伟大的指南:JWT with multi service app一切都很好但是在指南的最后我们建议创建一个配置服务(模块),我已经完成了。
问题是我无法覆盖JwtConfig类的默认配置
项目结构如下:
-config-service
| JwtConfig.java
\
| resources
\
| jwtConfig.properties
-other-service (add dependency in the pom file of the config-service)
|
someOtherclass.java (import the JwtConfig class & using @Bean to initialize )
JwtConfig类:
/*all the imports*/
@PropertySource(value = "classpath:jwtConfig.properties")
public class JwtConfig {
@Value("${security.jwt.uri:/auth/**}")
private String Uri;
@Value("${security.jwt.header:Authorization}")
private String header;
@Value("${security.jwt.prefix:Bearer }")
private String prefix;
@Value("${security.jwt.expiration:#{24*60*60}}")
private int expiration;
@Value("${security.jwt.secret:JwtSecretKey}")
private String secret;
//getters
someOtherclass.java:
/*imports*/
@Configuration
@EnableWebSecurity
public class SecurityCredentialsConfig extends WebSecurityConfigurerAdapter
{
private JwtConfig jwtConfig;
@Autowired
public void setJwtConfig(JwtConfig jwtConfig) {
this.jwtConfig = jwtConfig;
}
@Bean
public JwtConfig jwtConfig() {
return new JwtConfig();
}
/*other code*/
问题是我放入jwtConfig.properties文件中的参数无关紧要,
例如:
security.jwt.uri=test
当其他服务加载它时,它不会出现在JwtConfig bean中。
仅加载默认值@ Value。
有人可以有任何建议吗?我该怎么办呢?非常感谢!
看完Mikhail Kholodkov后(谢谢!),
解决方案是将以下注释添加到使用服务执行点:
@PropertySources({
@PropertySource("classpath:jwtConfig.properties"),
@PropertySource("classpath:app.properties")
})
public class OtherServiceApplication {
public static void main(String[] args) {
SpringApplication.run(OtherServiceApplication.class, args);
}
}
我认为不是使用@PropertySources,更好的方法和更合适的方法是在包含“main方法”的模块中使用@ComponentScan。由于您需要一个JWTConfiguration类的实例而不是实际的.property文件,因此建议公开bean并使springboot从另一个模块中扫描它,而不是公开属性文件(因为这会生成jwtConfiguration.java文件)在另一个模块中相当无用。)所以你可能会尝试这样的事情
假设我们有两个模块 - 主模块内的Module1和Module2(只有pom)。我假设您知道无代码模块将应用程序打包为“pom”的练习并描述了内部的依赖模块
你的主要pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>XXX</groupId>
<artifactId>XXXX</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<name>ws-cms-engine</name>
<url>http://maven.apache.org</url>
<properties>
<spring-boot.version>2.0.0.RELEASE</spring-boot.version>
<spring-kafka.version>2.2.3.RELEASE</spring-kafka.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
............
............
<modules>
<module>Module1</module>
<module>Module2</module>
</modules>
现在让我们考虑你的JWTConfiguration在Module1中,它使用声明的resources文件夹中的属性文件 - application.properties
示例JWTConfiguation.java文件
package common.module2.config
@Configuration
@PropertySources("classpath:application.properties")
public class JWTConfiguration{
@Value("${config1}")
private String someConfig;
}
现在,如果您的Module2具有需要使用此配置的主类,那么这样的事情可能会有意义
我们需要从模块1中声明的bean中读取Spring Boot容器,而不是读取实际的属性文件
@ComponentScan(basepackages={"common.module2.config", "common.module1.this.config"})
@SpringBootApplication
public class Application(){
public static void main(String args[]){
SpringApplication.run(Application.class);
}
}
所以在这里我们告知spring2包中声明的bean在启动和初始化时需要由spring容器进行扫描
现在,您可以在所需服务中自动装配bean并使用它
@Service
public class SampleService{
@Autowired
JWTConfiguration config;
}
这应该自动装配JWTConfiguration的托管实例供您使用