I am relatively new to Spring Configuration and I am trying to configure an oauth2client and authenticate it through my external identity provider OpenId Connect. I am applying authorization code grant flow.
When the user first accesses the server like localhost:8080
I have an
OpenIDConnectAuthenticationFilter
bean that redirects to my authentication portal from the IDP:
public class OpenIDConnectAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
@Value("${my.oauth2.clientId}")
private String clientId;
@Value("${my.oauth2.clientSecret}")
private String clientSecret;
@Value("${my.oauth2.userinfolink}")
private String userinfolink;
@Resource
private OAuth2RestOperations restTemplate;
@Autowired
private MyAuthorityMapper appAuthorityMapper;
protected OpenIDConnectAuthenticationFilter(String defaultFilterProcessesUrl) {
super(defaultFilterProcessesUrl);
System.out.println("defaultFilterProcessesUrl :" + defaultFilterProcessesUrl);
setAuthenticationManager(authentication -> authentication); // AbstractAuthenticationProcessingFilter requires an authentication manager.
}
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException {
System.out.println("attemptAuthentication ");
System.out.println(request.getParameterMap());
Map<String, String> form = new HashMap<String, String>();
form.put("client_id", clientId);
form.put("client_secret", clientSecret);
ResponseEntity<MyUserInfo> userInfoResponseEntity = null;
userInfoResponseEntity = restTemplate.getForEntity(userinfolink, MyUserInfo.class, form);
MyUserInfo myUserInfo = userInfoResponseEntity.getBody();
List userGroupList = new ArrayList();
return new PreAuthenticatedAuthenticationToken(myUserInfo, empty(), this.appAuthorityMapper.mapAuthorities(userGroupList));
}
}
And my Oauth2Client is:
However my login process gets stopped when I want to exchange the code grant against a token
It seems that the /callback
functionality is not being executed and the authenticaiton function implemented above is never entered.
Thanks.
It looks that your application expects callback call under different path. By default Spring expects that exchange code will be returned to endpoint
{baseUrl}/login/oauth2/code/{registrationId}
whereregistrationId
is a name that you are using in your configuration file.You can change that in either configuration file:
or in code by implementing method
void configure(HttpSecurity http)