|
@@ -0,0 +1,102 @@
|
|
|
+package com.usoftchina.smartschool.auth.jwt;
|
|
|
+
|
|
|
+import com.usoftchina.smartschool.exception.BizException;
|
|
|
+import com.usoftchina.smartschool.exception.ExceptionCode;
|
|
|
+import com.usoftchina.smartschool.utils.ObjectUtils;
|
|
|
+import com.usoftchina.smartschool.utils.RsaUtils;
|
|
|
+import io.jsonwebtoken.*;
|
|
|
+import org.joda.time.DateTime;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
+import java.security.spec.InvalidKeySpecException;
|
|
|
+
|
|
|
+
|
|
|
+ * @author yingp
|
|
|
+ * @date 2018/10/2
|
|
|
+ */
|
|
|
+public class JwtHelper {
|
|
|
+
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(JwtHelper.class);
|
|
|
+
|
|
|
+
|
|
|
+ * 密钥加密token
|
|
|
+ *
|
|
|
+ * @param jwtInfo jwt 帐号信息
|
|
|
+ * @param priKeyPath 私钥地址
|
|
|
+ * @param expire 过期时间
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static JwtToken generateToken(JwtInfo jwtInfo, String priKeyPath, int expire) throws BizException {
|
|
|
+ try {
|
|
|
+ String compactJws =
|
|
|
+
|
|
|
+ Jwts.builder()
|
|
|
+
|
|
|
+ .setSubject(jwtInfo.getUserName())
|
|
|
+ .claim("appId", jwtInfo.getAppId())
|
|
|
+ .claim("userId", jwtInfo.getUserId())
|
|
|
+ .claim("userName", jwtInfo.getUserName())
|
|
|
+ .setExpiration(DateTime.now().plusSeconds(expire).toDate())
|
|
|
+
|
|
|
+ .signWith(SignatureAlgorithm.RS256, RsaUtils.getPrivateKey(priKeyPath))
|
|
|
+
|
|
|
+ .compact();
|
|
|
+ return new JwtToken(compactJws, expire);
|
|
|
+ } catch (IOException | NoSuchAlgorithmException | InvalidKeySpecException e) {
|
|
|
+ log.error(e.getMessage());
|
|
|
+ throw new BizException(ExceptionCode.JWT_GEN_TOKEN_FAIL.getCode(), ExceptionCode.JWT_GEN_TOKEN_FAIL.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 公钥解析token
|
|
|
+ *
|
|
|
+ * @param token
|
|
|
+ * @param pubKeyPath 公钥路径
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ private static Jws<Claims> parserToken(String token, String pubKeyPath) throws BizException {
|
|
|
+ try {
|
|
|
+ return Jwts.parser().setSigningKey(RsaUtils.getPublicKey(pubKeyPath)).parseClaimsJws(token);
|
|
|
+ } catch (ExpiredJwtException ex) {
|
|
|
+ log.error("ExpiredJwtException:", ex);
|
|
|
+
|
|
|
+ throw new BizException(ExceptionCode.JWT_TOKEN_EXPIRED.getCode(), ExceptionCode.JWT_TOKEN_EXPIRED.getMessage());
|
|
|
+ } catch (SignatureException ex) {
|
|
|
+ log.error("SignatureException:", ex);
|
|
|
+
|
|
|
+ throw new BizException(ExceptionCode.JWT_SIGNATURE.getCode(), ExceptionCode.JWT_SIGNATURE.getMessage());
|
|
|
+ } catch (IllegalArgumentException ex) {
|
|
|
+ log.error("IllegalArgumentException:", ex);
|
|
|
+
|
|
|
+ throw new BizException(ExceptionCode.JWT_ILLEGAL_ARGUMENT.getCode(), ExceptionCode.JWT_ILLEGAL_ARGUMENT.getMessage());
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("message:", e);
|
|
|
+ throw new BizException(ExceptionCode.JWT_PARSER_TOKEN_FAIL.getCode(), ExceptionCode.JWT_PARSER_TOKEN_FAIL.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 获取token中的用户信息
|
|
|
+ *
|
|
|
+ * @param token token
|
|
|
+ * @param pubKeyPath 公钥路径
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static JwtInfo getInfoFromToken(String token, String pubKeyPath) throws BizException {
|
|
|
+ Jws<Claims> claimsJws = parserToken(token, pubKeyPath);
|
|
|
+ Claims body = claimsJws.getBody();
|
|
|
+ return new JwtInfo(
|
|
|
+ ObjectUtils.getStringValue(body.get("appId")),
|
|
|
+ ObjectUtils.getLongValue(body.get("school_id")),
|
|
|
+ ObjectUtils.getLongValue(body.get("userId")),
|
|
|
+ ObjectUtils.getStringValue(body.get("userName"))
|
|
|
+ );
|
|
|
+ }
|
|
|
+}
|