Skip to content

Commit 76dbe45

Browse files
committed
add verify signature
1 parent 798920d commit 76dbe45

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

‎spring-security-openid/src/main/java/org/baeldung/security/OpenIdConnectFilter.java‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
package org.baeldung.security;
22

33
import java.io.IOException;
4+
import java.math.BigInteger;
45
import java.util.Map;
56

67
import javax.servlet.ServletException;
78
import javax.servlet.http.HttpServletRequest;
89
import javax.servlet.http.HttpServletResponse;
910

11+
import org.apache.commons.codec.binary.Base64;
1012
import org.springframework.security.authentication.AuthenticationManager;
1113
import org.springframework.security.authentication.BadCredentialsException;
1214
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
1315
import org.springframework.security.core.Authentication;
1416
import org.springframework.security.core.AuthenticationException;
1517
import org.springframework.security.jwt.Jwt;
1618
import org.springframework.security.jwt.JwtHelper;
19+
import org.springframework.security.jwt.crypto.sign.RsaVerifier;
1720
import org.springframework.security.oauth2.client.OAuth2RestOperations;
1821
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
1922
import 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

Comments
 (0)