Hi i’m trying to build a spring security configuration, but i’m facing the following error even with the public-key file in the correct location:
src/main/resources/keys/api_pub_key_dev
[Spring Security] – InvalidConfigurationPropertyValueException: Property spring.security.oauth2.resourceserver.public-key-location with value ‘class path resource [xxxxx/xxxxxxxx]’ is invalid: Public key location does not exist
my application-local.yml:
<code>spring: config:
activate:
on-profile: "local" security:
oauth2:
resourceserver:
jwt:
public-key-location: 'classpath:keys/api_pub_key_dev'
</code>
<code>spring: config:
activate:
on-profile: "local" security:
oauth2:
resourceserver:
jwt:
public-key-location: 'classpath:keys/api_pub_key_dev'
</code>
spring: config:
activate:
on-profile: "local" security:
oauth2:
resourceserver:
jwt:
public-key-location: 'classpath:keys/api_pub_key_dev'
My ResourceConfig:
<code>@NoArgsConstructor@Configuration(proxyBeanMethods = false)@EnableWebSecurity@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)public class ResourceServerConfig {private static final String ACCESSTOKEN = "Accesstoken";@BeanSecurityFilterChain filterChain(final HttpSecurity http, final AppAuthority appAuthority) {try {http.cors(withDefaults());http.authorizeRequests(requests -> requests.antMatchers(HttpMethod.GET, "/actuator/health").hasAnyAuthority(appAuthority.getActuatorHealthGroup()).antMatchers(HttpMethod.GET, "/actuator/health/liveness").hasAnyAuthority(appAuthority.getActuatorHealthGroup()).antMatchers(HttpMethod.GET, "/actuator/health/readiness").hasAnyAuthority(appAuthority.getActuatorHealthGroup()).antMatchers(HttpMethod.GET, "/actuator/info").hasAnyAuthority(appAuthority.getActuatorHealthGroup()).antMatchers(HttpMethod.GET, "/actuator/**").hasAnyAuthority(appAuthority.getMonitoracao().getExternalName()).antMatchers(HttpMethod.POST, "/**").permitAll().antMatchers(HttpMethod.GET, "/**").permitAll().antMatchers(HttpMethod.GET, "/public/**").permitAll().anyRequest().authenticated()).sessionManagement(management -> management.sessionCreationPolicy(SessionCreationPolicy.STATELESS)).csrf(csrf -> csrf.disable()).oauth2ResourceServer(server -> server.jwt().jwtAuthenticationConverter(jwtAuthenticationConverter()));return http.build();} catch (Exception e) {throw new InternalException("Falha ao configurar resource server", e);}}@BeanBearerTokenResolver bearerTokenResolver() {final DefaultBearerTokenResolver bearerTokenResolver = new DefaultBearerTokenResolver();bearerTokenResolver.setBearerTokenHeaderName(ACCESSTOKEN);return bearerTokenResolver;}private JwtAuthenticationConverter jwtAuthenticationConverter() {final var jwtAuthenticationConverter = new JwtAuthenticationConverter();jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(jwt -> {var authorities = jwt.getClaimAsStringList("usu-perfis");if (authorities == null) {authorities = Collections.emptyList();}final String tipo = jwt.getClaim("usu-tipo");authorities.add(tipo);final var scopesAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();final Collection<GrantedAuthority> grantedAuthorities = scopesAuthoritiesConverter.convert(jwt);grantedAuthorities.addAll(authorities.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList()));return grantedAuthorities;});return jwtAuthenticationConverter;}}</code><code>@NoArgsConstructor @Configuration(proxyBeanMethods = false) @EnableWebSecurity @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) public class ResourceServerConfig { private static final String ACCESSTOKEN = "Accesstoken"; @Bean SecurityFilterChain filterChain(final HttpSecurity http, final AppAuthority appAuthority) { try { http.cors(withDefaults()); http.authorizeRequests(requests -> requests .antMatchers(HttpMethod.GET, "/actuator/health").hasAnyAuthority(appAuthority.getActuatorHealthGroup()) .antMatchers(HttpMethod.GET, "/actuator/health/liveness").hasAnyAuthority(appAuthority.getActuatorHealthGroup()) .antMatchers(HttpMethod.GET, "/actuator/health/readiness").hasAnyAuthority(appAuthority.getActuatorHealthGroup()) .antMatchers(HttpMethod.GET, "/actuator/info").hasAnyAuthority(appAuthority.getActuatorHealthGroup()) .antMatchers(HttpMethod.GET, "/actuator/**").hasAnyAuthority(appAuthority.getMonitoracao().getExternalName()) .antMatchers(HttpMethod.POST, "/**").permitAll() .antMatchers(HttpMethod.GET, "/**").permitAll() .antMatchers(HttpMethod.GET, "/public/**").permitAll() .anyRequest().authenticated()) .sessionManagement(management -> management.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) .csrf(csrf -> csrf.disable()) .oauth2ResourceServer(server -> server .jwt() .jwtAuthenticationConverter(jwtAuthenticationConverter())); return http.build(); } catch (Exception e) { throw new InternalException("Falha ao configurar resource server", e); } } @Bean BearerTokenResolver bearerTokenResolver() { final DefaultBearerTokenResolver bearerTokenResolver = new DefaultBearerTokenResolver(); bearerTokenResolver.setBearerTokenHeaderName(ACCESSTOKEN); return bearerTokenResolver; } private JwtAuthenticationConverter jwtAuthenticationConverter() { final var jwtAuthenticationConverter = new JwtAuthenticationConverter(); jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(jwt -> { var authorities = jwt.getClaimAsStringList("usu-perfis"); if (authorities == null) { authorities = Collections.emptyList(); } final String tipo = jwt.getClaim("usu-tipo"); authorities.add(tipo); final var scopesAuthoritiesConverter = new JwtGrantedAuthoritiesConverter(); final Collection<GrantedAuthority> grantedAuthorities = scopesAuthoritiesConverter.convert(jwt); grantedAuthorities.addAll(authorities.stream() .map(SimpleGrantedAuthority::new) .collect(Collectors.toList())); return grantedAuthorities; }); return jwtAuthenticationConverter; } } </code>@NoArgsConstructor @Configuration(proxyBeanMethods = false) @EnableWebSecurity @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) public class ResourceServerConfig { private static final String ACCESSTOKEN = "Accesstoken"; @Bean SecurityFilterChain filterChain(final HttpSecurity http, final AppAuthority appAuthority) { try { http.cors(withDefaults()); http.authorizeRequests(requests -> requests .antMatchers(HttpMethod.GET, "/actuator/health").hasAnyAuthority(appAuthority.getActuatorHealthGroup()) .antMatchers(HttpMethod.GET, "/actuator/health/liveness").hasAnyAuthority(appAuthority.getActuatorHealthGroup()) .antMatchers(HttpMethod.GET, "/actuator/health/readiness").hasAnyAuthority(appAuthority.getActuatorHealthGroup()) .antMatchers(HttpMethod.GET, "/actuator/info").hasAnyAuthority(appAuthority.getActuatorHealthGroup()) .antMatchers(HttpMethod.GET, "/actuator/**").hasAnyAuthority(appAuthority.getMonitoracao().getExternalName()) .antMatchers(HttpMethod.POST, "/**").permitAll() .antMatchers(HttpMethod.GET, "/**").permitAll() .antMatchers(HttpMethod.GET, "/public/**").permitAll() .anyRequest().authenticated()) .sessionManagement(management -> management.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) .csrf(csrf -> csrf.disable()) .oauth2ResourceServer(server -> server .jwt() .jwtAuthenticationConverter(jwtAuthenticationConverter())); return http.build(); } catch (Exception e) { throw new InternalException("Falha ao configurar resource server", e); } } @Bean BearerTokenResolver bearerTokenResolver() { final DefaultBearerTokenResolver bearerTokenResolver = new DefaultBearerTokenResolver(); bearerTokenResolver.setBearerTokenHeaderName(ACCESSTOKEN); return bearerTokenResolver; } private JwtAuthenticationConverter jwtAuthenticationConverter() { final var jwtAuthenticationConverter = new JwtAuthenticationConverter(); jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(jwt -> { var authorities = jwt.getClaimAsStringList("usu-perfis"); if (authorities == null) { authorities = Collections.emptyList(); } final String tipo = jwt.getClaim("usu-tipo"); authorities.add(tipo); final var scopesAuthoritiesConverter = new JwtGrantedAuthoritiesConverter(); final Collection<GrantedAuthority> grantedAuthorities = scopesAuthoritiesConverter.convert(jwt); grantedAuthorities.addAll(authorities.stream() .map(SimpleGrantedAuthority::new) .collect(Collectors.toList())); return grantedAuthorities; }); return jwtAuthenticationConverter; } }
can anyone help me on this?
Thank you