spring cloud oauth2 jwt 解析示例


spring cloud oauth2 jwt 解析示例

**********************

相关类及接口

JwtHelper:jwt编解码、获取header

  1. public class JwtHelper {
  2. static byte[] PERIOD = Codecs.utf8Encode(".");
  3. *************
  4. 构造方法
  5. public JwtHelper() {
  6. }
  7. *************
  8. 普通方法
  9. public static Jwt decode(String token) { //解码操作
  10. public static Jwt decodeAndVerify(String token, SignatureVerifier verifier) {
  11. public static Jwt encode(CharSequence content, Signer signer) { //编码操作
  12. public static Jwt encode(CharSequence content, Signer signer, Map<String, String> headers) {
  13. public static Map<String, String> headers(String token) { //获取header

Jwt:jwt接口

  1. public interface Jwt extends BinaryFormat {
  2. String getClaims(); //获取编码的原始信息,为json字符串
  3. String getEncoded(); //获取原始信息编码后的字符串
  4. void verifySignature(SignatureVerifier var1);
  5. }

OAuth2AuthenticationDetails:认证信息

  1. public class OAuth2AuthenticationDetails implements Serializable {
  2. private static final long serialVersionUID = -4809832298438307309L;
  3. public static final String ACCESS_TOKEN_VALUE = OAuth2AuthenticationDetails.class.getSimpleName() + ".ACCESS_TOKEN_VALUE";
  4. public static final String ACCESS_TOKEN_TYPE = OAuth2AuthenticationDetails.class.getSimpleName() + ".ACCESS_TOKEN_TYPE";
  5. private final String remoteAddress;
  6. private final String sessionId;
  7. private final String tokenValue;
  8. private final String tokenType;
  9. private final String display;
  10. private Object decodedDetails;
  11. ***********
  12. 构造方法
  13. public OAuth2AuthenticationDetails(HttpServletRequest request) {
  14. ***********
  15. 普通方法
  16. public void setDecodedDetails(Object decodedDetails) {
  17. public String getTokenValue() { //获取token字符串的值
  18. public String getTokenType() {
  19. public String getRemoteAddress() {
  20. public String getSessionId() {
  21. public Object getDecodedDetails() {

**********************

示例

认证服务器配置参照spring cloud oauth2 jwt 使用示例

*********************

资源服务器

HelloController

  1. @RestController
  2. public class HelloController {
  3. @Value("${security.oauth2.client.access-token-uri}")
  4. private String accessTokenUri;
  5. @RequestMapping("/hello")
  6. public String hello(){
  7. return "hello world";
  8. }
  9. @RequestMapping("/redirect") //获取授权码时的回调地址,使用获得的授权码获取access_token
  10. public Map get(@RequestParam(value = "code") String code){
  11. OkHttpClient httpClient=new OkHttpClient();
  12. RequestBody requestBody=new FormBody.Builder()
  13. .add("grant_type","authorization_code")
  14. .add("client","user")
  15. .add("redirect_uri","http://localhost:8082/redirect")
  16. .add("code",code)
  17. .build();
  18. Request request=new Request.Builder()
  19. .url(accessTokenUri)
  20. .post(requestBody)
  21. .addHeader("Authorization","Basic dXNlcjoxMjM0NTY=")
  22. .build();
  23. Map result=null;
  24. try {
  25. Response response=httpClient.newCall(request).execute();
  26. System.out.println(response);
  27. ObjectMapper objectMapper=new ObjectMapper();
  28. result=objectMapper.readValue(Objects.requireNonNull(response.body()).string(),Map.class);
  29. System.out.println("access_token:"+result.get("access_token"));
  30. System.out.println("token_type:"+result.get("token_type"));
  31. System.out.println("refresh_token:"+result.get("refresh_token"));
  32. System.out.println("expires_in:"+result.get("expires_in"));
  33. System.out.println("scope:"+result.get("scope"));
  34. }catch (Exception e){
  35. System.out.println(e.getMessage());
  36. }
  37. return result;
  38. }
  39. @RequestMapping("/parse")
  40. public Object parse(Authentication authentication){ //解析jwt
  41. OAuth2AuthenticationDetails oAuth2AuthenticationDetails=(OAuth2AuthenticationDetails) authentication.getDetails();
  42. String token=oAuth2AuthenticationDetails.getTokenValue();
  43. Jwt jwt=JwtHelper.decode(token);
  44. String claims=jwt.getClaims();
  45. String encoded=jwt.getEncoded();
  46. System.out.println("claims 原始信息:"+claims); //获取原始信息json字符串
  47. System.out.println("access token编码信息:"+encoded); //获取编码后的字符串
  48. return JwtHelper.decode(token);
  49. }
  50. }

**********************

使用测试

认证获取token后,调用:localhost:8082/parse,header设置为

  1. keyuthorization
  2. valueeyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHRlbnNpb24iOiJqd3Qg5ouT5bGV5L-h5oGvIiwidXNlcl9uYW1lIjoiZ3RseCIsInNjb3BlIjpbInVzZXIiXSwiZXhwIjoxNTgxMTY5ODgwLCJhdXRob3JpdGllcyI6WyJhZG1pbiJdLCJqdGkiOiJjY2E5NWNiMy1jMWEyLTQ4N2QtOTIyMi1hNTczODI3MTAwMjkiLCJjbGllbnRfaWQiOiJ1c2VyIn0.j2To3Q4rlZwqvjZGp1VornK9CfXXkV81J9G_9_jxwWU
  3. ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzkzMTYyNQ_size_16_color_FFFFFF_t_70][]

控制台输出

  1. claims 原始信息:{"extension":"jwt 拓展信息","user_name":"gtlx","scope":["user"],"exp":1581169880,"authorities":["admin"],"jti":"cca95cb3-c1a2-487d-9222-a57382710029","client_id":"user"}
  2. access token编码信息:eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHRlbnNpb24iOiJqd3Qg5ouT5bGV5L-h5oGvIiwidXNlcl9uYW1lIjoiZ3RseCIsInNjb3BlIjpbInVzZXIiXSwiZXhwIjoxNTgxMTY5ODgwLCJhdXRob3JpdGllcyI6WyJhZG1pbiJdLCJqdGkiOiJjY2E5NWNiMy1jMWEyLTQ4N2QtOTIyMi1hNTczODI3MTAwMjkiLCJjbGllbnRfaWQiOiJ1c2VyIn0.j2To3Q4rlZwqvjZGp1VornK9CfXXkV81J9G_9_jxwWU

发表评论

表情:
评论列表 (有 0 条评论,71人围观)

还没有评论,来说两句吧...

相关阅读