Java Code Examples for org.springframework.security.jwt.JwtHelper#decode()
The following examples show how to use
org.springframework.security.jwt.JwtHelper#decode() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OAuth2ClientCredentialsService.java From flair-registry with Apache License 2.0 | 6 votes |
public String getAccessToken() { if (accessToken == null) { retrieveNewAccessToken(); } Jwt jwt = JwtHelper.decode(accessToken); String claims = jwt.getClaims(); JsonParser jsonParser = JsonParserFactory.getJsonParser(); Map<String, Object> claimMap = jsonParser.parseMap(claims); Integer exp = (Integer) claimMap.get("exp"); int now = (int) (System.currentTimeMillis() / 1000L); if (exp < now) { retrieveNewAccessToken(); } return accessToken; }
Example 2
Source File: GrantByResourceOwnerPasswordCredentialTest.java From demo-spring-boot-security-oauth2 with MIT License | 6 votes |
@SuppressWarnings({"rawtypes", "unchecked"}) @Test public void getJwtTokenByClientCredentialForUser() throws JsonParseException, JsonMappingException, IOException { ResponseEntity<String> response = new TestRestTemplate("trusted-app", "secret").postForEntity("http://localhost:" + port + "/oauth/token?grant_type=password&username=user&password=password", null, String.class); String responseText = response.getBody(); assertEquals(HttpStatus.OK, response.getStatusCode()); HashMap jwtMap = new ObjectMapper().readValue(responseText, HashMap.class); assertEquals("bearer", jwtMap.get("token_type")); assertEquals("read write", jwtMap.get("scope")); assertTrue(jwtMap.containsKey("access_token")); assertTrue(jwtMap.containsKey("expires_in")); assertTrue(jwtMap.containsKey("jti")); String accessToken = (String) jwtMap.get("access_token"); Jwt jwtToken = JwtHelper.decode(accessToken); String claims = jwtToken.getClaims(); HashMap claimsMap = new ObjectMapper().readValue(claims, HashMap.class); assertEquals("spring-boot-application", ((List<String>) claimsMap.get("aud")).get(0)); assertEquals("trusted-app", claimsMap.get("client_id")); assertEquals("user", claimsMap.get("user_name")); assertEquals("read", ((List<String>) claimsMap.get("scope")).get(0)); assertEquals("write", ((List<String>) claimsMap.get("scope")).get(1)); assertEquals("ROLE_USER", ((List<String>) claimsMap.get("authorities")).get(0)); }
Example 3
Source File: GrantByResourceOwnerPasswordCredentialTest.java From demo-spring-boot-security-oauth2 with MIT License | 6 votes |
@SuppressWarnings({"rawtypes", "unchecked"}) @Test public void getJwtTokenByClientCredentialForAdmin() throws JsonParseException, JsonMappingException, IOException { ResponseEntity<String> response = new TestRestTemplate("trusted-app", "secret").postForEntity("http://localhost:" + port + "/oauth/token?grant_type=password&username=admin&password=password", null, String.class); String responseText = response.getBody(); assertEquals(HttpStatus.OK, response.getStatusCode()); HashMap jwtMap = new ObjectMapper().readValue(responseText, HashMap.class); assertEquals("bearer", jwtMap.get("token_type")); assertEquals("read write", jwtMap.get("scope")); assertTrue(jwtMap.containsKey("access_token")); assertTrue(jwtMap.containsKey("expires_in")); assertTrue(jwtMap.containsKey("jti")); String accessToken = (String) jwtMap.get("access_token"); Jwt jwtToken = JwtHelper.decode(accessToken); String claims = jwtToken.getClaims(); HashMap claimsMap = new ObjectMapper().readValue(claims, HashMap.class); assertEquals("spring-boot-application", ((List<String>) claimsMap.get("aud")).get(0)); assertEquals("trusted-app", claimsMap.get("client_id")); assertEquals("admin", claimsMap.get("user_name")); assertEquals("read", ((List<String>) claimsMap.get("scope")).get(0)); assertEquals("write", ((List<String>) claimsMap.get("scope")).get(1)); assertEquals("ROLE_ADMIN", ((List<String>) claimsMap.get("authorities")).get(0)); }
Example 4
Source File: JWTAuthentication.java From codeway_service with GNU General Public License v3.0 | 5 votes |
public static boolean invalidJwtAccessToken(String authentication) { //verifier = Optional.ofNullable(verifier).orElse(new MacSigner(signingKey)); //是否无效true表示无效 boolean invalid = Boolean.TRUE; try { String pubKey = JWTAuthentication.getPubKey(PUBLIC_KEY); RsaVerifier rsaVerifier = new RsaVerifier(pubKey); Jwt jwt = JwtHelper.decode(authentication); jwt.verifySignature(rsaVerifier); invalid = Boolean.FALSE; } catch (InvalidSignatureException | IllegalArgumentException ex) { LogBack.error("user token has expired or signature error"); } return invalid; }
Example 5
Source File: OAuth2CookieHelper.java From cubeai with Apache License 2.0 | 5 votes |
/** * Retrieve the given claim from the given token. * * @param refreshToken the JWT token to examine. * @param claimName name of the claim to get. * @param clazz the Class we expect to find there. * @return the desired claim. * @throws InvalidTokenException if we cannot find the claim in the token or it is of wrong type. */ @SuppressWarnings("unchecked") private <T> T getClaim(String refreshToken, String claimName, Class<T> clazz) { Jwt jwt = JwtHelper.decode(refreshToken); String claims = jwt.getClaims(); Map<String, Object> claimsMap = jsonParser.parseMap(claims); Object claimValue = claimsMap.get(claimName); if (claimValue == null) { return null; } if (!clazz.isAssignableFrom(claimValue.getClass())) { throw new InvalidTokenException("claim is not of expected type: " + claimName); } return (T) claimValue; }
Example 6
Source File: JWTAuthentication.java From codeway_service with GNU General Public License v3.0 | 5 votes |
public static boolean invalidJwtAccessToken(String authentication) { //verifier = Optional.ofNullable(verifier).orElse(new MacSigner(signingKey)); //是否无效true表示无效 boolean invalid = Boolean.TRUE; try { String pubKey = JWTAuthentication.getPubKey(PUBLIC_KEY); RsaVerifier rsaVerifier = new RsaVerifier(pubKey); Jwt jwt = JwtHelper.decode(authentication); jwt.verifySignature(rsaVerifier); invalid = Boolean.FALSE; } catch (InvalidSignatureException | IllegalArgumentException ex) { LogBack.error("user token has expired or signature error"); } return invalid; }
Example 7
Source File: Claims.java From OAuth-2.0-Cookbook with MIT License | 5 votes |
public static Claims createFrom(ObjectMapper jsonMapper, OAuth2AccessToken accessToken) { try { String idToken = accessToken.getAdditionalInformation().get("id_token").toString(); Jwt decodedToken = JwtHelper.decode(idToken); return jsonMapper.readValue(decodedToken.getClaims(), Claims.class); } catch (IOException e) { throw new RuntimeException(e); } }
Example 8
Source File: Claims.java From OAuth-2.0-Cookbook with MIT License | 5 votes |
public static Claims createFrom(ObjectMapper jsonMapper, OAuth2AccessToken accessToken) { try { String idToken = accessToken.getAdditionalInformation().get("id_token").toString(); Jwt decodedToken = JwtHelper.decode(idToken); return jsonMapper.readValue(decodedToken.getClaims(), Claims.class); } catch (IOException e) { throw new RuntimeException(e); } }
Example 9
Source File: GrantByClientCredentialTest.java From demo-spring-boot-security-oauth2 with MIT License | 5 votes |
@SuppressWarnings({"rawtypes", "unchecked"}) @Test public void getJwtTokenByTrustedClient() throws JsonParseException, JsonMappingException, IOException { ResponseEntity<String> response = new TestRestTemplate("trusted-app", "secret").postForEntity("http://localhost:" + port + "/oauth/token?client_id=trusted-app&grant_type=client_credentials", null, String.class); String responseText = response.getBody(); assertEquals(HttpStatus.OK, response.getStatusCode()); HashMap jwtMap = new ObjectMapper().readValue(responseText, HashMap.class); assertEquals("bearer", jwtMap.get("token_type")); assertEquals("read write", jwtMap.get("scope")); assertTrue(jwtMap.containsKey("access_token")); assertTrue(jwtMap.containsKey("expires_in")); assertTrue(jwtMap.containsKey("jti")); String accessToken = (String) jwtMap.get("access_token"); Jwt jwtToken = JwtHelper.decode(accessToken); String claims = jwtToken.getClaims(); logJson(claims); HashMap claimsMap = new ObjectMapper().readValue(claims, HashMap.class); assertEquals("spring-boot-application", ((List<String>) claimsMap.get("aud")).get(0)); assertEquals("trusted-app", claimsMap.get("client_id")); assertEquals("read", ((List<String>) claimsMap.get("scope")).get(0)); assertEquals("write", ((List<String>) claimsMap.get("scope")).get(1)); List<String> authorities = (List<String>) claimsMap.get("authorities"); assertEquals(1, authorities.size()); assertEquals("ROLE_TRUSTED_CLIENT", authorities.get(0)); }
Example 10
Source File: OAuthTokenUtil.java From careconnect-reference-implementation with Apache License 2.0 | 5 votes |
private static OAuthToken parseJwtToken(String jwtToken) { try { Jwt jwt = JwtHelper.decode(jwtToken); ObjectMapper mapper = new ObjectMapper(); return mapper.readValue(jwt.getClaims().getBytes(), OAuthToken.class); } catch (IOException e) { throw new AuthenticationException("Invalid OAuth2 Token", e); } }
Example 11
Source File: OAuth2CookieHelper.java From tutorials with MIT License | 5 votes |
/** * Retrieve the given claim from the given token. * * @param refreshToken the JWT token to examine. * @param claimName name of the claim to get. * @param clazz the Class we expect to find there. * @return the desired claim. * @throws InvalidTokenException if we cannot find the claim in the token or it is of wrong type. */ @SuppressWarnings("unchecked") private <T> T getClaim(String refreshToken, String claimName, Class<T> clazz) { Jwt jwt = JwtHelper.decode(refreshToken); String claims = jwt.getClaims(); Map<String, Object> claimsMap = jsonParser.parseMap(claims); Object claimValue = claimsMap.get(claimName); if (claimValue == null) { return null; } if (!clazz.isAssignableFrom(claimValue.getClass())) { throw new InvalidTokenException("claim is not of expected type: " + claimName); } return (T) claimValue; }
Example 12
Source File: AuthService.java From codeway_service with GNU General Public License v3.0 | 4 votes |
public Jwt getJwt(String authentication) { return JwtHelper.decode(authentication); }
Example 13
Source File: AuthService.java From codeway_service with GNU General Public License v3.0 | 4 votes |
public Jwt getJwt(String authentication) { return JwtHelper.decode(authentication); }
Example 14
Source File: AuthService.java From JetfireCloud with Apache License 2.0 | 4 votes |
@Override public Jwt getJwt(String authentication) { return JwtHelper.decode(StringUtils.substring(authentication, BEARER_BEGIN_INDEX)); }
Example 15
Source File: JwtUserService.java From springboot-vue.js-bbs with Apache License 2.0 | 4 votes |
private Jwt getParsedToken(String accessToken) { return JwtHelper.decode(accessToken.split(" ")[1]); }