Skip to content

Instantly share code, notes, and snippets.

@sandipchitale
Last active November 19, 2023 11:28
Show Gist options
  • Save sandipchitale/b8d75fa41bf8e8701970411825983d7c to your computer and use it in GitHub Desktop.
Save sandipchitale/b8d75fa41bf8e8701970411825983d7c to your computer and use it in GitHub Desktop.
Set portmapper for oauth2Login() #oauth2Login #portMapper
ObjectPostProcessor<AuthenticationEntryPoint> authenticationEntryPointFilterPostProcessor = new ObjectPostProcessor<>() {
@Override
public <O extends AuthenticationEntryPoint> O postProcess(O authenticationEntryPoint) {
if (authenticationEntryPoint instanceof DelegatingAuthenticationEntryPoint delegatingAuthenticationEntryPoint) {
Field entryPointsField = ReflectionUtils.findField(DelegatingAuthenticationEntryPoint.class, "entryPoints");
assert entryPointsField != null;
entryPointsField.setAccessible(true);
LinkedHashMap<RequestMatcher, AuthenticationEntryPoint> entryPoints =
(LinkedHashMap<RequestMatcher, AuthenticationEntryPoint>) ReflectionUtils.getField(entryPointsField,
delegatingAuthenticationEntryPoint);
entryPoints.values().forEach(authenticationEntryPointValue -> {
if (authenticationEntryPointValue instanceof LoginUrlAuthenticationEntryPoint loginUrlAuthenticationEntryPoint) {
PortMapperImpl portMapper = new PortMapperImpl();
portMapper.setPortMappings(Map.of("80", "80", "8080", "8080"));
loginUrlAuthenticationEntryPoint.setPortMapper(portMapper);
}
});
}
return authenticationEntryPoint;
}
};
httpSecurity
.oauth2Login((OAuth2LoginConfigurer<HttpSecurity> httpSecurityOAuth2LoginConfigurer) -> {
httpSecurityOAuth2LoginConfigurer.withObjectPostProcessor(authenticationEntryPointFilterPostProcessor);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment