Im using oauth2 and sso in my angular application. My rest backend verify the authentication token which is send by every request. Now I wanna use the oid claim to load the user from my database and save it in the principal. Also I wanna add the user authorities in the "GrantedAuthorities".
FooService
[...]
public Foo getFooByOid(String oid) throws FooNotFoundException {
return fooRepository.findByOid(oid)
.orElseThrow(() -> new FooNotFoundException("Foo with oid: " + oid + " not found"));
}
SecurityConfig
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests(auth -> auth
.anyRequest().authenticated())
.oauth2ResourceServer(oauth2 -> {
oauth2.jwt();
});
}
}
The spring.security.oauth2.resourceserver.jwt.jwk-set-uri and spring.security.oauth2.resourceserver.jwt.issuer-uri are defined in application.properties
After that my principal contains all the claims, headers and the token:
Can any one help me how to do? Thanks
You can do this via a custom AuthenticationManager, if you write code such as this:
The properties you set on oauth2ResourceServer influence the behaviour of Spring's BearerTokenAuthenticationFilter class
You will then need to validate the JWT yourself, plus add your custom claims handling on top, then cache results for subsequent requests with the same token, which is tricky.
EXAMPLE OF MINE
I have a fairly complete sample that behaves like this, which can run on your PC and which you can maybe borrow some ideas from - it is quite an advanced sample though: