protected String encode(OAuth2AccessToken accessToken, OAuth2Authentication authentication) { String content; try { content = objectMapper.formatMap(tokenConverter.convertAccessToken(accessToken, authentication)); } catch (Exception e) { throw new IllegalStateException("Cannot convert access token to JSON", e); } String token = JwtHelper.encode(content, signer).getEncoded(); return token; }
public static Jwt encode(CharSequence content, Signer signer) { return encode(content, signer, Collections.<String, String>emptyMap()); }
protected String encode(OAuth2AccessToken accessToken, OAuth2Authentication authentication) { String content; try { content = objectMapper.formatMap(tokenConverter.convertAccessToken(accessToken, authentication)); } catch (Exception e) { throw new IllegalStateException("Cannot convert access token to JSON", e); } String token = JwtHelper.encode(content, signer).getEncoded(); return token; }
@Override public void apply(RequestTemplate template) { try { String token = null; if (SecurityContextHolder.getContext() != null && SecurityContextHolder.getContext().getAuthentication() != null && SecurityContextHolder.getContext().getAuthentication().getDetails() instanceof OAuth2AuthenticationDetails) { OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) SecurityContextHolder .getContext().getAuthentication().getDetails(); if (details.getTokenType() != null && details.getTokenValue() != null) { token = details.getTokenType() + " " + details.getTokenValue(); } else if (details.getDecodedDetails() instanceof CustomUserDetails) { token = OAUTH_TOKEN_PREFIX + JwtHelper.encode(OBJECT_MAPPER.writeValueAsString(details.getDecodedDetails()), signer).getEncoded(); } } if (token == null) { token = OAUTH_TOKEN_PREFIX + JwtHelper.encode(OBJECT_MAPPER.writeValueAsString(defaultUserDetails), signer).getEncoded(); } template.header(RequestVariableHolder.HEADER_JWT, token); setLabel(template); } catch (Exception e) { LOGGER.error("generate jwt token failed {}", e); } }
@Override public void apply(RequestTemplate template) { if (HystrixRequestContext.isCurrentThreadInitialized()) { String token = HystrixHeaderInterceptor.token.get(); String label = HystrixHeaderInterceptor.label.get(); if (token != null) { template.header(HystrixHeaderInterceptor.HEADER_TOKEN, token); }else { try { String jwtToken = "Bearer " + JwtHelper.encode(OBJECT_MAPPER.writeValueAsString(DEFAULT_USER), SIGNER).getEncoded(); LOGGER.info("token {}",jwtToken); template.header(HystrixHeaderInterceptor.HEADER_TOKEN, jwtToken); } catch (IOException e) { LOGGER.info("error {}",e); } } if (label != null) { template.header(HystrixHeaderInterceptor.HEADER_LABEL, label); } }else { try { String jwtToken = "Bearer " + JwtHelper.encode(OBJECT_MAPPER.writeValueAsString(DEFAULT_USER), SIGNER).getEncoded(); LOGGER.info("token {}",jwtToken); template.header(HystrixHeaderInterceptor.HEADER_TOKEN, jwtToken); } catch (IOException e) { LOGGER.info("error {}",e); } } }
public String getBearerToken(final String scope) { final ZonedDateTime soon = ZonedDateTime.now().plusDays(365); final String jwtToken = "{\n" + " \"aud\": [\n" + " \"" + aud + "\"\n" + " ],\n" + " \"exp\": " + soon.toEpochSecond() + ",\n" + " \"user_name\": \"3d44bbc24614e28edd094bc54ef0497809717af5\",\n" + " \"jti\": \"3cee521d-96a7-4d82-b726-7e02355f3a55\",\n" + " \"client_id\": \"fe0661e5a99e4d43bd3496cc6c58025f\",\n" + " \"scope\": [\n" + " \"" + scope + "\"\n" + " ]\n" + "}"; final RsaSigner rsaSigner = new RsaSigner((RSAPrivateKey) keyPair.getPrivate()); final Jwt encode = JwtHelper.encode(jwtToken, rsaSigner); return "Bearer " + encode.getEncoded(); } }