I have few spring boot microservices, and for some of the I want to define a custom security starter to utilise the same security filter chain. I did the setup on one service, everything works as expected, but when I want to move it in my common auth project, the beans are correctly initiated, but the authentication does not work anymore. In security started I need two beans defined as follows:
@Configuration
public class SecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
return http.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(auth -> auth.anyRequest().authenticated())
.oauth2ResourceServer(
oauth2 -> oauth2.jwt(jwt -> jwt.jwtAuthenticationConverter(JwtUtil::createJwtUser)))
.build();
}
@Bean
public JwtDecoder jwtDecoder() {
return token -> {
try {
JWT jwt = JWTParser.parse(token);
var headers = jwt.getHeader().toJSONObject();
var claims = jwt.getJWTClaimsSet().getClaims();
var expiresAt = jwt.getJWTClaimsSet().getExpirationTime().toInstant();
var createdAt = jwt.getJWTClaimsSet().getIssueTime().toInstant();
return new Jwt(token, createdAt, expiresAt, headers, claims);
} catch (ParseException e) {
throw new RuntimeException(e);
}
};
}
}
I move this exact class into my common auth library, I changed @Configuration with @AutoConfiguration and defined the config bean in my META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports file but it’s not working properly.