We have a entitlements database which has application id, roles and users mapped to roles per application. Following the advice on thread how do I map user roles to oauth2
I was struggling with same issue, since by default the securityContext has client details I extended the DefaultOauth2RequestFactory and have set the User authentication manually in SecurityContext
public TokenRequest createTokenRequest(Map requestParameters, ClientDetails authenticatedClient) {
SecurityContextHolder.getContext()
.setAuthentication(new UsernamePasswordAuthenticationToken(requestParameters.get("username"), null,
userDetailsService.loadUserByUsername(requestParameters.get("username")).getAuthorities()));
return super.createTokenRequest(requestParameters, authenticatedClient);
}
With this code in place the SecurityContext will always be populated by User authentication rather than Client authentication you can do this for specific grant type aswell
if (requestParameters.get("grant_type").equals("password")) { //same code as above }