I have searched High and low and still I am unable to find a simple answer to this very annoying problem,
I have followed this great guide: JWT with multi service app Everything works great but in the end of the guide we are suggested to create a config-service(module) , which I have done.
The problem is that I am unable to override the default configuration of JwtConfig class
The project structure is as follows:
-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 )
The JwtConfig class:
/*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
The 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*/
The problem is that it does not matter what parameters I put in the jwtConfig.properties file,
For example:
security.jwt.uri=test
It will not appear in the JwtConfig bean when the other service loads it.
Only the default @Value's are loaded.
can someone have any advice? how may I fix it? Many thanks!
I think instead of using @PropertySources , the better approach and more suitable one would be to use @ComponentScan in your module that contains the "main method". Since you would need an instance of JWTConfiguration class rather than the actual .property file, it is more recommended to expose the bean and make springboot scan it from another module, rather than to expose the property file (because that makes the jwtConfiguration.java file in another module rather useless.) So you could probably try something like this
Say we have two modules - Module1 and Module2 inside the main module (that has only pom). I presume you know the drill that the no-code module just packages the app as "pom" and describes the dependant modules inside
Your main pom
Now lets consider that your JWTConfiguration is in Module1 and it uses the property file in the resources folder declared -
application.properties
Sample JWTConfiguation.java file
Now if your Module2, has the main class that needs to make use of this configuration then probably something like this would make sense
We need to make the SpringBoot container read from the bean declared in module1 rather than read the actual property file
So here we inform that beans declared in module2 package needs to be scanned by the spring container when it starts and initializes
Now you can Autowire the bean in your required service and use it
That should autowire the managed instance of JWTConfiguration for you to use
After looking in Mikhail Kholodkov post(Thanks!),
The solution is to add the following annotation to the using service Execution point: