JWTUtils.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package com.icontrols.oauth.utils;
  2. import java.io.UnsupportedEncodingException;
  3. import java.security.GeneralSecurityException;
  4. import java.security.NoSuchAlgorithmException;
  5. import java.util.Base64;
  6. import java.util.Date;
  7. import java.util.Base64.Decoder;
  8. import org.json.JSONObject;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. import com.icontrols.oauth.constants.CONSTANTS;
  12. import com.icontrols.oauth.controller.ViewController;
  13. import com.icontrols.oauth.model.JWTInfo;
  14. import io.jsonwebtoken.Jwts;
  15. import io.jsonwebtoken.SignatureAlgorithm;
  16. public class JWTUtils {
  17. private static final Logger logger = LoggerFactory.getLogger(JWTUtils.class);
  18. public JWTUtils() {
  19. super();
  20. }
  21. // // icontrols1793!@# base64인코딩하고앞에 난수(dlswmd) 붙임
  22. // public static String ACCESS_TOEKN_SECRET_KEY = "dlswmdaWNvbnRyb2xzMTc5MyFAIw==";
  23. // // icontrolsrefresh1793!@#
  24. // public static String REFRESH_TOKEN_SECRET_KEY = "aWNvbnRyb2xzcmVmcmVzaDE3OTMhQCM=";
  25. static public String generateToken(JWTInfo info, String type) throws UnsupportedEncodingException {
  26. String secretKey = "";
  27. String jwt = "";
  28. if (type.equals("accessToken")) {
  29. secretKey = CONSTANTS.ACCESS_TOEKN_SECRET_KEY;
  30. jwt = Jwts.builder().setExpiration(new Date(43200000)) // 1시간 -> 설정정보로 변경해야함
  31. .claim("cmpIp", info.getCmpIp()).claim("homeId", info.getHomeId())
  32. .claim("iat", System.currentTimeMillis() / 1000).claim("clientInfo", info.getClientInfo())
  33. .signWith(SignatureAlgorithm.HS256, secretKey.getBytes("UTF-8")).compact();
  34. } else if (type.equals("refreshToken")) {
  35. secretKey = CONSTANTS.REFRESH_TOKEN_SECRET_KEY;
  36. jwt = Jwts.builder().setExpiration(new Date(259200000)) // 1일 -> 설정정보로 변경해야함
  37. .claim("cmpIp", info.getCmpIp()).claim("homeId", info.getHomeId())
  38. .claim("iat", System.currentTimeMillis() / 1000).claim("clientInfo", info.getClientInfo())
  39. .signWith(SignatureAlgorithm.HS256, secretKey.getBytes("UTF-8")).compact();
  40. }
  41. return jwt;
  42. }
  43. static public boolean validateToken(String token, String type) throws UnsupportedEncodingException {
  44. String secretKey = "";
  45. if (type.equals("accessToken")) {
  46. secretKey = CONSTANTS.ACCESS_TOEKN_SECRET_KEY;
  47. } else if (type.equals("refreshToken")) {
  48. secretKey = CONSTANTS.REFRESH_TOKEN_SECRET_KEY;
  49. }
  50. logger.info(token);
  51. if (token.split("\\.")[0] == null || token.split("\\.")[1] == null || token.split("\\.")[2] == null)
  52. return false;
  53. Decoder decoder = Base64.getDecoder();
  54. // 시그니처가 맞는지 확인
  55. String enHeader = token.split("\\.")[0];
  56. logger.info("H= " + enHeader);
  57. String enPayload = token.split("\\.")[1];
  58. String dePayload = new String(decoder.decode(enPayload));
  59. logger.info("P= " + enPayload);
  60. String signiture = token.split("\\.")[2];
  61. logger.info("S= " + signiture);
  62. JSONObject obj = new JSONObject(dePayload);
  63. if (obj.isNull("cmpIp") || obj.isNull("homeId") || obj.isNull("iat") || obj.isNull("clientInfo"))
  64. return false;
  65. String jwt = Jwts.builder().claim("exp", obj.get("exp")) // 1시간 -> 설정정보로 변경해야함
  66. .claim("cmpIp", obj.get("cmpIp")).claim("homeId", obj.get("homeId")).claim("iat", obj.get("iat"))
  67. .claim("clientInfo", obj.get("clientInfo"))
  68. .signWith(SignatureAlgorithm.HS256, secretKey.getBytes("UTF-8")).compact();
  69. logger.info("new = " + jwt);
  70. if (token.equals(jwt)) { // 갱신인 경우에는 시간 검사를 안하기 위해
  71. if (type.equals("accessToken")) {
  72. long iat = Long.parseLong(obj.get("iat").toString());
  73. long currentTime = System.currentTimeMillis() / 1000;
  74. if (currentTime > iat + Long.parseLong(obj.get("exp").toString())) {
  75. return false;
  76. }
  77. }
  78. return true;
  79. }
  80. return false;
  81. }
  82. static public JWTInfo getJWTInfoFromToken(String token) {
  83. Decoder decoder = Base64.getDecoder();
  84. String enPayload = token.split("\\.")[1];
  85. String dePayload = new String(decoder.decode(enPayload));
  86. logger.info("P= " + enPayload);
  87. JSONObject obj = new JSONObject(dePayload);
  88. JWTInfo jwtInfo = new JWTInfo();
  89. jwtInfo.setCmpIp(obj.get("cmpIp").toString());
  90. jwtInfo.setHomeId(obj.get("homeId").toString());
  91. jwtInfo.setClientInfo(obj.get("clientInfo").toString());
  92. return jwtInfo;
  93. }
  94. static public String getCmpIpFromToken(String token) {
  95. Decoder decoder = Base64.getDecoder();
  96. String enPayload = token.split("\\.")[1];
  97. String dePayload = new String(decoder.decode(enPayload));
  98. logger.info("P= " + enPayload);
  99. JSONObject obj = new JSONObject(dePayload);
  100. String cmpIp = obj.get("cmpIp").toString();
  101. Decoder dcoder = Base64.getDecoder();
  102. cmpIp = new String(decoder.decode(cmpIp));
  103. return cmpIp;
  104. }
  105. static public String getHomeIdFromToken(String token)
  106. throws UnsupportedEncodingException, NoSuchAlgorithmException, GeneralSecurityException {
  107. Decoder decoder = Base64.getDecoder();
  108. String enPayload = token.split("\\.")[1];
  109. String dePayload = new String(decoder.decode(enPayload));
  110. JSONObject obj = new JSONObject(dePayload);
  111. String encodedHomeId = obj.get("homeId").toString();
  112. AES256Util aes = new AES256Util();
  113. String homeId = aes.decrypt(encodedHomeId);
  114. return homeId;
  115. }
  116. static public String getClientInfoFromToken(String token)
  117. throws UnsupportedEncodingException, NoSuchAlgorithmException, GeneralSecurityException {
  118. Decoder decoder = Base64.getDecoder();
  119. String enPayload = token.split("\\.")[1];
  120. String dePayload = new String(decoder.decode(enPayload));
  121. JSONObject obj = new JSONObject(dePayload);
  122. String clientInfo = obj.get("clientInfo").toString();
  123. return clientInfo;
  124. }
  125. }