|
| 1 | +package org.baeldung.security; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +import javax.servlet.ServletException; |
| 7 | +import javax.servlet.http.HttpServletRequest; |
| 8 | +import javax.servlet.http.HttpServletResponse; |
| 9 | + |
| 10 | +import org.springframework.security.authentication.AuthenticationManager; |
| 11 | +import org.springframework.security.authentication.BadCredentialsException; |
| 12 | +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| 13 | +import org.springframework.security.core.Authentication; |
| 14 | +import org.springframework.security.core.AuthenticationException; |
| 15 | +import org.springframework.security.jwt.Jwt; |
| 16 | +import org.springframework.security.jwt.JwtHelper; |
| 17 | +import org.springframework.security.oauth2.client.OAuth2RestOperations; |
| 18 | +import org.springframework.security.oauth2.client.OAuth2RestTemplate; |
| 19 | +import org.springframework.security.oauth2.common.OAuth2AccessToken; |
| 20 | +import org.springframework.security.oauth2.common.exceptions.InvalidTokenException; |
| 21 | +import org.springframework.security.oauth2.common.exceptions.OAuth2Exception; |
| 22 | +import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter; |
| 23 | + |
| 24 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 25 | + |
| 26 | +public class OpenIdConnectFilter extends AbstractAuthenticationProcessingFilter { |
| 27 | + public OAuth2RestOperations restTemplate; |
| 28 | + |
| 29 | + public OpenIdConnectFilter(String defaultFilterProcessesUrl) { |
| 30 | + super(defaultFilterProcessesUrl); |
| 31 | + setAuthenticationManager(new NoopAuthenticationManager()); |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException { |
| 36 | + |
| 37 | + OAuth2AccessToken accessToken; |
| 38 | + try { |
| 39 | + accessToken = restTemplate.getAccessToken(); |
| 40 | + } catch (final OAuth2Exception e) { |
| 41 | + throw new BadCredentialsException("Could not obtain access token", e); |
| 42 | + } |
| 43 | + try { |
| 44 | + final String idToken = accessToken.getAdditionalInformation().get("id_token").toString(); |
| 45 | + final Jwt tokenDecoded = JwtHelper.decode(idToken); |
| 46 | + System.out.println("===== : " + tokenDecoded.getClaims()); |
| 47 | + |
| 48 | + final Map<String, String> authInfo = new ObjectMapper().readValue(tokenDecoded.getClaims(), Map.class); |
| 49 | + |
| 50 | + final OpenIdConnectUserDetails user = new OpenIdConnectUserDetails(authInfo, accessToken); |
| 51 | + return new UsernamePasswordAuthenticationToken(user, null, user.getAuthorities()); |
| 52 | + } catch (final InvalidTokenException e) { |
| 53 | + throw new BadCredentialsException("Could not obtain user details from token", e); |
| 54 | + } |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + public void setRestTemplate(OAuth2RestTemplate restTemplate2) { |
| 59 | + restTemplate = restTemplate2; |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + private static class NoopAuthenticationManager implements AuthenticationManager { |
| 64 | + |
| 65 | + @Override |
| 66 | + public Authentication authenticate(Authentication authentication) throws AuthenticationException { |
| 67 | + throw new UnsupportedOperationException("No authentication should be done with this AuthenticationManager"); |
| 68 | + } |
| 69 | + |
| 70 | + } |
| 71 | +} |
0 commit comments