11package org .baeldung .security ;
22
33import java .io .IOException ;
4+ import java .math .BigInteger ;
45import java .util .Map ;
56
67import javax .servlet .ServletException ;
78import javax .servlet .http .HttpServletRequest ;
89import javax .servlet .http .HttpServletResponse ;
910
11+ import org .apache .commons .codec .binary .Base64 ;
1012import org .springframework .security .authentication .AuthenticationManager ;
1113import org .springframework .security .authentication .BadCredentialsException ;
1214import org .springframework .security .authentication .UsernamePasswordAuthenticationToken ;
1315import org .springframework .security .core .Authentication ;
1416import org .springframework .security .core .AuthenticationException ;
1517import org .springframework .security .jwt .Jwt ;
1618import org .springframework .security .jwt .JwtHelper ;
19+ import org .springframework .security .jwt .crypto .sign .RsaVerifier ;
1720import org .springframework .security .oauth2 .client .OAuth2RestOperations ;
1821import org .springframework .security .oauth2 .client .OAuth2RestTemplate ;
1922import org .springframework .security .oauth2 .common .OAuth2AccessToken ;
@@ -42,7 +45,7 @@ public Authentication attemptAuthentication(HttpServletRequest request, HttpServ
4245 }
4346 try {
4447 final String idToken = accessToken .getAdditionalInformation ().get ("id_token" ).toString ();
45- final Jwt tokenDecoded = JwtHelper .decode (idToken );
48+ final Jwt tokenDecoded = JwtHelper .decodeAndVerify (idToken , verifier () );
4649 System .out .println ("===== : " + tokenDecoded .getClaims ());
4750
4851 final Map <String , String > authInfo = new ObjectMapper ().readValue (tokenDecoded .getClaims (), Map .class );
@@ -60,6 +63,15 @@ public void setRestTemplate(OAuth2RestTemplate restTemplate2) {
6063
6164 }
6265
66+ // details can be found at https://www.googleapis.com/oauth2/v2/certs
67+ private RsaVerifier verifier () {
68+ byte [] nbytes = Base64 .decodeBase64 ("vmyoDT6ND_YJa1ItdvULuTJr2pw4MvN3Z5kmSiJBm9glVoakcDEBGF4b5crKiPW7WDh2PZ0_yXY9ikDaTux7hxtgUtmm96KjmdBn_FYwv3SlsBRnzZw1oAG-2OdjlFWvlx4rXOhAzZ04ngPb3ELywwtKoO90hCy2DrNOMMSCuSu8zrFLw5oREawPcUFEQReipy_KRFf02VxFbK4Tj2FHVdBPPLW3W1KJD4S-NNwPnoeDrI6zWMv7WWAeSLAT0hX36r5FM9dM2uXTxPRCZzs-nqrUiHxn4duFIGgzuxCVbyigDrnfsmHx-B5tG1m7ts74xwf2P_PJwNNJ8qRihMsS2Q==" );
69+ byte [] ebytes = Base64 .decodeBase64 ("AQAB" );
70+ BigInteger n = new BigInteger (1 , nbytes );
71+ BigInteger e = new BigInteger (1 , ebytes );
72+ return new RsaVerifier (n , e );
73+ }
74+
6375 private static class NoopAuthenticationManager implements AuthenticationManager {
6476
6577 @ Override
0 commit comments