afbff5f8a2c1689b96b65f495568573660377cd6.svn-base 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package org.jeecg.common.system.util;
  2. import com.auth0.jwt.JWT;
  3. import com.auth0.jwt.JWTVerifier;
  4. import com.auth0.jwt.algorithms.Algorithm;
  5. import com.auth0.jwt.exceptions.JWTDecodeException;
  6. import com.auth0.jwt.interfaces.DecodedJWT;
  7. import com.google.common.base.Joiner;
  8. import java.util.Date;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpSession;
  11. import org.apache.shiro.SecurityUtils;
  12. import org.jeecg.common.constant.CommonConstant;
  13. import org.jeecg.common.constant.DataBaseConstant;
  14. import org.jeecg.common.exception.JeecgBootException;
  15. import org.jeecg.common.system.vo.LoginUser;
  16. import org.jeecg.common.system.vo.SysUserCacheInfo;
  17. import org.jeecg.common.util.DateUtils;
  18. import org.jeecg.common.util.SpringContextUtils;
  19. import org.jeecg.common.util.oConvertUtils;
  20. /**
  21. * @Author Scott
  22. * @Date 2018-07-12 14:23
  23. * @Desc JWT工具类
  24. **/
  25. public class JwtUtil {
  26. // Token过期时间30分钟(用户登录过期时间是此时间的两倍,以token在reids缓存时间为准)
  27. public static final long EXPIRE_TIME = 30 * 60 * 1000;
  28. /**
  29. * 校验token是否正确
  30. *
  31. * @param token 密钥
  32. * @param secret 用户的密码
  33. * @return 是否正确
  34. */
  35. public static boolean verify(String token, String username, String secret) {
  36. try {
  37. // 根据密码生成JWT效验器
  38. Algorithm algorithm = Algorithm.HMAC256(secret);
  39. JWTVerifier verifier = JWT.require(algorithm).withClaim("username", username).build();
  40. // 效验TOKEN
  41. DecodedJWT jwt = verifier.verify(token);
  42. return true;
  43. } catch (Exception exception) {
  44. return false;
  45. }
  46. }
  47. /**
  48. * 获得token中的信息无需secret解密也能获得
  49. *
  50. * @return token中包含的用户名
  51. */
  52. public static String getUsername(String token) {
  53. try {
  54. DecodedJWT jwt = JWT.decode(token);
  55. return jwt.getClaim("username").asString();
  56. } catch (JWTDecodeException e) {
  57. return null;
  58. }
  59. }
  60. /**
  61. * 生成签名,5min后过期
  62. *
  63. * @param username 用户名
  64. * @param secret 用户的密码
  65. * @return 加密的token
  66. */
  67. public static String sign(String username, String secret) {
  68. Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
  69. Algorithm algorithm = Algorithm.HMAC256(secret);
  70. // 附带username信息
  71. return JWT.create().withClaim("username", username).withExpiresAt(date).sign(algorithm);
  72. }
  73. /**
  74. * 根据request中的token获取用户账号
  75. *
  76. * @param request
  77. * @return
  78. * @throws JeecgBootException
  79. */
  80. public static String getUserNameByToken(HttpServletRequest request) throws JeecgBootException {
  81. String accessToken = request.getHeader("X-Access-Token");
  82. String username = getUsername(accessToken);
  83. if (oConvertUtils.isEmpty(username)) {
  84. throw new JeecgBootException("未获取到用户");
  85. }
  86. return username;
  87. }
  88. /**
  89. * 从session中获取变量
  90. * @param key
  91. * @return
  92. */
  93. public static String getSessionData(String key) {
  94. //${myVar}%
  95. //得到${} 后面的值
  96. String moshi = "";
  97. if(key.indexOf("}")!=-1){
  98. moshi = key.substring(key.indexOf("}")+1);
  99. }
  100. String returnValue = null;
  101. if (key.contains("#{")) {
  102. key = key.substring(2,key.indexOf("}"));
  103. }
  104. if (oConvertUtils.isNotEmpty(key)) {
  105. HttpSession session = SpringContextUtils.getHttpServletRequest().getSession();
  106. returnValue = (String) session.getAttribute(key);
  107. }
  108. //结果加上${} 后面的值
  109. if(returnValue!=null){returnValue = returnValue + moshi;}
  110. return returnValue;
  111. }
  112. /**
  113. * 从当前用户中获取变量
  114. * @param key
  115. * @param user
  116. * @return
  117. */
  118. //TODO 急待改造 sckjkdsjsfjdk
  119. public static String getUserSystemData(String key,SysUserCacheInfo user) {
  120. if(user==null) {
  121. user = JeecgDataAutorUtils.loadUserInfo();
  122. }
  123. //#{sys_user_code}%
  124. // 获取登录用户信息
  125. LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
  126. String moshi = "";
  127. if(key.indexOf("}")!=-1){
  128. moshi = key.substring(key.indexOf("}")+1);
  129. }
  130. String returnValue = null;
  131. //针对特殊标示处理#{sysOrgCode},判断替换
  132. if (key.contains("#{")) {
  133. key = key.substring(2,key.indexOf("}"));
  134. } else {
  135. key = key;
  136. }
  137. //替换为系统登录用户帐号
  138. if (key.equals(DataBaseConstant.SYS_USER_CODE)|| key.toLowerCase().equals(DataBaseConstant.SYS_USER_CODE_TABLE)) {
  139. if(user==null) {
  140. returnValue = sysUser.getUsername();
  141. }else {
  142. returnValue = user.getSysUserCode();
  143. }
  144. }
  145. //替换为系统登录用户真实名字
  146. else if (key.equals(DataBaseConstant.SYS_USER_NAME)|| key.toLowerCase().equals(DataBaseConstant.SYS_USER_NAME_TABLE)) {
  147. if(user==null) {
  148. returnValue = sysUser.getRealname();
  149. }else {
  150. returnValue = user.getSysUserName();
  151. }
  152. }
  153. //替换为系统用户登录所使用的机构编码
  154. else if (key.equals(DataBaseConstant.SYS_ORG_CODE)|| key.toLowerCase().equals(DataBaseConstant.SYS_ORG_CODE_TABLE)) {
  155. if(user==null) {
  156. returnValue = sysUser.getOrgCode();
  157. }else {
  158. returnValue = user.getSysOrgCode();
  159. }
  160. }
  161. //替换为系统用户所拥有的所有机构编码
  162. else if (key.equals(DataBaseConstant.SYS_MULTI_ORG_CODE)|| key.toLowerCase().equals(DataBaseConstant.SYS_MULTI_ORG_CODE_TABLE)) {
  163. if(user==null){
  164. //TODO 暂时使用用户登录部门,存在逻辑缺陷,不是用户所拥有的部门
  165. returnValue = sysUser.getOrgCode();
  166. }else{
  167. if(user.isOneDepart()) {
  168. returnValue = user.getSysMultiOrgCode().get(0);
  169. }else {
  170. returnValue = Joiner.on(",").join(user.getSysMultiOrgCode());
  171. }
  172. }
  173. }
  174. //替换为当前系统时间(年月日)
  175. else if (key.equals(DataBaseConstant.SYS_DATE)|| key.toLowerCase().equals(DataBaseConstant.SYS_DATE_TABLE)) {
  176. returnValue = DateUtils.formatDate();
  177. }
  178. //替换为当前系统时间(年月日时分秒)
  179. else if (key.equals(DataBaseConstant.SYS_TIME)|| key.toLowerCase().equals(DataBaseConstant.SYS_TIME_TABLE)) {
  180. returnValue = DateUtils.now();
  181. }
  182. //流程状态默认值(默认未发起)
  183. else if (key.equals(DataBaseConstant.BPM_STATUS)|| key.toLowerCase().equals(DataBaseConstant.BPM_STATUS_TABLE)) {
  184. returnValue = "1";
  185. }
  186. //update-begin-author:taoyan date:20210330 for:多租户ID作为系统变量
  187. else if (key.equals(DataBaseConstant.TENANT_ID) || key.toLowerCase().equals(DataBaseConstant.TENANT_ID_TABLE)){
  188. returnValue = sysUser.getRelTenantIds();
  189. if(oConvertUtils.isEmpty(returnValue) || (returnValue!=null && returnValue.indexOf(",")>0)){
  190. returnValue = SpringContextUtils.getHttpServletRequest().getHeader(CommonConstant.TENANT_ID);
  191. }
  192. }
  193. //update-end-author:taoyan date:20210330 for:多租户ID作为系统变量
  194. if(returnValue!=null){returnValue = returnValue + moshi;}
  195. return returnValue;
  196. }
  197. // public static void main(String[] args) {
  198. // String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1NjUzMzY1MTMsInVzZXJuYW1lIjoiYWRtaW4ifQ.xjhud_tWCNYBOg_aRlMgOdlZoWFFKB_givNElHNw3X0";
  199. // System.out.println(JwtUtil.getUsername(token));
  200. // }
  201. }