I'm trying to use Keycloak to secure a Spring Boot API using Spring Security. The API is running on port 8080 and Keycloak is on port 8081. This is my filter chain:
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests()
.requestMatchers(HttpMethod.POST, "/api/posts*").authenticated()
.anyRequest().permitAll()
.and()
.csrf().disable();
http.oauth2Login()
.and()
.logout()
.logoutSuccessUrl("/");
return http.build();
}
When I try to POST to /api/posts from Insomnia, It shows me the login screen even though I provided valid credentials.
(The access token is also there but it's not in the screenshot)
application.properties:
spring.security.oauth2.client.registration.keycloak.client-id=backend
spring.security.oauth2.client.registration.keycloak.authorization-grant-type=authorization_code
spring.security.oauth2.client.registration.keycloak.scope=openid
spring.security.oauth2.client.provider.keycloak.issuer-uri=http://localhost:8081/realms/test
spring.security.oauth2.client.provider.keycloak.user-name-attribute=preferred_username
