I have a spring boot app, which delegate authentication and access management to KeyCLoak
: the linking parameters are defined in the application.proerties as follows:
## keycloak configuration:
keycloak.realm = Demo-Realm
keycloak.auth-server-url = http://localhost:8080/auth
keycloak.ssl-required = external
keycloak.resource = springboot-microservice
keycloak.credentials.secret = xxxx-xxxx-xxxx-xxx-xxxxxxxxxxx
keycloak.use-resource-role-mappings = true
keycloak.bearer-only = true
## Port
server.port=9090
When I started the app and try to access an API for using the access token for an authorized user provided by KeyCloak, a get the expected response with 200 code;
but when restart the app and ommit the Authorization header, the API send 403 error and keeps sending the same error even after providing a valid access token again! So I have the restart the app the get it works again.
Here is my Configuration Class :
@Configuration
@KeycloakConfiguration
@EnableGlobalMethodSecurity(jsr250Enabled = true)
public class ResourceServiceConfig extends KeycloakWebSecurityConfigurerAdapter {
@Bean
@Override
protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl());
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
KeycloakAuthenticationProvider keycloakAuthenticationProvider = keycloakAuthenticationProvider();
keycloakAuthenticationProvider.setGrantedAuthoritiesMapper( new SimpleAuthorityMapper() );
auth.authenticationProvider(keycloakAuthenticationProvider);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().permitAll();
http.csrf().disable();
}
}
My Controller:
@RestController
@RequestMapping("/workout")
public class WorkoutController {
@Autowired
private WorkoutService workoutService;
@PostMapping("/add")
public void addNewRecord(@RequestBody Workout newWorkout){
workoutService.saveWorkout(newWorkout);
}
@RolesAllowed("admin")
@GetMapping("/find/all")
public List<Workout> findAllUserRecords(){
return workoutService.findWorkouts();
}
@DeleteMapping("/{id}")
public void deleteRecordById(@PathVariable Long id){
workoutService.deleteWorkout(id);
}
}
I'm new to KeyCloak. could anyone help.