¶Ô±ÈÐÂÎļþ |
| | |
| | | package org.jeecg.common.system.util; |
| | | |
| | | import com.auth0.jwt.JWT; |
| | | import com.auth0.jwt.JWTVerifier; |
| | | import com.auth0.jwt.algorithms.Algorithm; |
| | | import com.auth0.jwt.exceptions.JWTDecodeException; |
| | | import com.auth0.jwt.interfaces.DecodedJWT; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.google.common.base.Joiner; |
| | | |
| | | import java.io.IOException; |
| | | import java.io.OutputStream; |
| | | import java.util.Date; |
| | | import javax.servlet.ServletResponse; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import javax.servlet.http.HttpSession; |
| | | |
| | | import org.apache.shiro.SecurityUtils; |
| | | import org.jeecg.common.api.vo.Result; |
| | | import org.jeecg.common.constant.CommonConstant; |
| | | import org.jeecg.common.constant.DataBaseConstant; |
| | | import org.jeecg.common.constant.SymbolConstant; |
| | | import org.jeecg.common.constant.TenantConstant; |
| | | import org.jeecg.common.exception.JeecgBootException; |
| | | import org.jeecg.common.system.vo.LoginUser; |
| | | import org.jeecg.common.system.vo.SysUserCacheInfo; |
| | | import org.jeecg.common.util.DateUtils; |
| | | import org.jeecg.common.util.SpringContextUtils; |
| | | import org.jeecg.common.util.oConvertUtils; |
| | | |
| | | /** |
| | | * @Author Scott |
| | | * @Date 2018-07-12 14:23 |
| | | * @Desc JWTå·¥å
·ç±» |
| | | **/ |
| | | public class JwtUtil { |
| | | |
| | | /**Tokenæææä¸º1å°æ¶ï¼Tokenå¨reidsä¸ç¼åæ¶é´ä¸ºä¸¤åï¼*/ |
| | | public static final long EXPIRE_TIME = 1000 * 24 * 60 * 60 * 1000; |
| | | static final String WELL_NUMBER = SymbolConstant.WELL_NUMBER + SymbolConstant.LEFT_CURLY_BRACKET; |
| | | |
| | | /** |
| | | * |
| | | * @param response |
| | | * @param code |
| | | * @param errorMsg |
| | | */ |
| | | public static void responseError(ServletResponse response, Integer code, String errorMsg) { |
| | | HttpServletResponse httpServletResponse = (HttpServletResponse) response; |
| | | // issues/I4YH95æµè§å¨æ¾ç¤ºä¹±ç é®é¢ |
| | | httpServletResponse.setHeader("Content-type", "text/html;charset=UTF-8"); |
| | | Result jsonResult = new Result(code, errorMsg); |
| | | jsonResult.setSuccess(false); |
| | | OutputStream os = null; |
| | | try { |
| | | os = httpServletResponse.getOutputStream(); |
| | | httpServletResponse.setCharacterEncoding("UTF-8"); |
| | | httpServletResponse.setStatus(code); |
| | | os.write(new ObjectMapper().writeValueAsString(jsonResult).getBytes("UTF-8")); |
| | | os.flush(); |
| | | os.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * æ ¡éªtokenæ¯å¦æ£ç¡® |
| | | * |
| | | * @param token å¯é¥ |
| | | * @param secret ç¨æ·çå¯ç |
| | | * @return æ¯å¦æ£ç¡® |
| | | */ |
| | | public static boolean verify(String token, String username, String secret) { |
| | | try { |
| | | // æ ¹æ®å¯ç çæJWTæéªå¨ |
| | | Algorithm algorithm = Algorithm.HMAC256(secret); |
| | | JWTVerifier verifier = JWT.require(algorithm).withClaim("username", username).build(); |
| | | // æéªTOKEN |
| | | DecodedJWT jwt = verifier.verify(token); |
| | | return true; |
| | | } catch (Exception exception) { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * è·å¾tokenä¸çä¿¡æ¯æ ésecretè§£å¯ä¹è½è·å¾ |
| | | * |
| | | * @return tokenä¸å
å«çç¨æ·å |
| | | */ |
| | | public static String getUsername(String token) { |
| | | try { |
| | | DecodedJWT jwt = JWT.decode(token); |
| | | return jwt.getClaim("username").asString(); |
| | | } catch (JWTDecodeException e) { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * çæç¾å,5minåè¿æ |
| | | * |
| | | * @param username ç¨æ·å |
| | | * @param secret ç¨æ·çå¯ç |
| | | * @return å å¯çtoken |
| | | */ |
| | | public static String sign(String username, String secret) { |
| | | Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME); |
| | | Algorithm algorithm = Algorithm.HMAC256(secret); |
| | | // é带usernameä¿¡æ¯ |
| | | return JWT.create().withClaim("username", username).withExpiresAt(date).sign(algorithm); |
| | | |
| | | } |
| | | |
| | | /** |
| | | * æ ¹æ®requestä¸çtokenè·åç¨æ·è´¦å· |
| | | * |
| | | * @param request |
| | | * @return |
| | | * @throws JeecgBootException |
| | | */ |
| | | public static String getUserNameByToken(HttpServletRequest request) throws JeecgBootException { |
| | | String accessToken = request.getHeader("X-Access-Token"); |
| | | String username = getUsername(accessToken); |
| | | if (oConvertUtils.isEmpty(username)) { |
| | | throw new JeecgBootException("æªè·åå°ç¨æ·"); |
| | | } |
| | | return username; |
| | | } |
| | | |
| | | /** |
| | | * ä»sessionä¸è·ååé |
| | | * @param key |
| | | * @return |
| | | */ |
| | | public static String getSessionData(String key) { |
| | | //${myVar}% |
| | | //å¾å°${} åé¢çå¼ |
| | | String moshi = ""; |
| | | String wellNumber = WELL_NUMBER; |
| | | |
| | | if(key.indexOf(SymbolConstant.RIGHT_CURLY_BRACKET)!=-1){ |
| | | moshi = key.substring(key.indexOf("}")+1); |
| | | } |
| | | String returnValue = null; |
| | | if (key.contains(wellNumber)) { |
| | | key = key.substring(2,key.indexOf("}")); |
| | | } |
| | | if (oConvertUtils.isNotEmpty(key)) { |
| | | HttpSession session = SpringContextUtils.getHttpServletRequest().getSession(); |
| | | returnValue = (String) session.getAttribute(key); |
| | | } |
| | | //ç»æå ä¸${} åé¢çå¼ |
| | | if(returnValue!=null){returnValue = returnValue + moshi;} |
| | | return returnValue; |
| | | } |
| | | |
| | | /** |
| | | * ä»å½åç¨æ·ä¸è·ååé |
| | | * @param key |
| | | * @param user |
| | | * @return |
| | | */ |
| | | public static String getUserSystemData(String key,SysUserCacheInfo user) { |
| | | if(user==null) { |
| | | user = JeecgDataAutorUtils.loadUserInfo(); |
| | | } |
| | | //#{sys_user_code}% |
| | | |
| | | // è·åç»å½ç¨æ·ä¿¡æ¯ |
| | | LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal(); |
| | | |
| | | String moshi = ""; |
| | | String wellNumber = WELL_NUMBER; |
| | | if(key.indexOf(SymbolConstant.RIGHT_CURLY_BRACKET)!=-1){ |
| | | moshi = key.substring(key.indexOf("}")+1); |
| | | } |
| | | String returnValue = null; |
| | | //éå¯¹ç¹æ®æ 示å¤ç#{sysOrgCode}ï¼å¤ææ¿æ¢ |
| | | if (key.contains(wellNumber)) { |
| | | key = key.substring(2,key.indexOf("}")); |
| | | } else { |
| | | key = key; |
| | | } |
| | | //æ¿æ¢ä¸ºç³»ç»ç»å½ç¨æ·å¸å· |
| | | if (key.equals(DataBaseConstant.SYS_USER_CODE)|| key.toLowerCase().equals(DataBaseConstant.SYS_USER_CODE_TABLE)) { |
| | | if(user==null) { |
| | | returnValue = sysUser.getUsername(); |
| | | }else { |
| | | returnValue = user.getSysUserCode(); |
| | | } |
| | | } |
| | | //æ¿æ¢ä¸ºç³»ç»ç»å½ç¨æ·çå®åå |
| | | else if (key.equals(DataBaseConstant.SYS_USER_NAME)|| key.toLowerCase().equals(DataBaseConstant.SYS_USER_NAME_TABLE)) { |
| | | if(user==null) { |
| | | returnValue = sysUser.getRealname(); |
| | | }else { |
| | | returnValue = user.getSysUserName(); |
| | | } |
| | | } |
| | | |
| | | //æ¿æ¢ä¸ºç³»ç»ç¨æ·ç»å½æä½¿ç¨çæºæç¼ç |
| | | else if (key.equals(DataBaseConstant.SYS_ORG_CODE)|| key.toLowerCase().equals(DataBaseConstant.SYS_ORG_CODE_TABLE)) { |
| | | if(user==null) { |
| | | returnValue = sysUser.getOrgCode(); |
| | | }else { |
| | | returnValue = user.getSysOrgCode(); |
| | | } |
| | | } |
| | | //æ¿æ¢ä¸ºç³»ç»ç¨æ·ææ¥æçæææºæç¼ç |
| | | else if (key.equals(DataBaseConstant.SYS_MULTI_ORG_CODE)|| key.toLowerCase().equals(DataBaseConstant.SYS_MULTI_ORG_CODE_TABLE)) { |
| | | if(user==null){ |
| | | //TODO ææ¶ä½¿ç¨ç¨æ·ç»å½é¨é¨ï¼åå¨é»è¾ç¼ºé·ï¼ä¸æ¯ç¨æ·ææ¥æçé¨é¨ |
| | | returnValue = sysUser.getOrgCode(); |
| | | }else{ |
| | | if(user.isOneDepart()) { |
| | | returnValue = user.getSysMultiOrgCode().get(0); |
| | | }else { |
| | | returnValue = Joiner.on(",").join(user.getSysMultiOrgCode()); |
| | | } |
| | | } |
| | | } |
| | | //æ¿æ¢ä¸ºå½åç³»ç»æ¶é´(å¹´ææ¥) |
| | | else if (key.equals(DataBaseConstant.SYS_DATE)|| key.toLowerCase().equals(DataBaseConstant.SYS_DATE_TABLE)) { |
| | | returnValue = DateUtils.formatDate(); |
| | | } |
| | | //æ¿æ¢ä¸ºå½åç³»ç»æ¶é´ï¼å¹´ææ¥æ¶åç§ï¼ |
| | | else if (key.equals(DataBaseConstant.SYS_TIME)|| key.toLowerCase().equals(DataBaseConstant.SYS_TIME_TABLE)) { |
| | | returnValue = DateUtils.now(); |
| | | } |
| | | //æµç¨ç¶æé»è®¤å¼ï¼é»è®¤æªåèµ·ï¼ |
| | | else if (key.equals(DataBaseConstant.BPM_STATUS)|| key.toLowerCase().equals(DataBaseConstant.BPM_STATUS_TABLE)) { |
| | | returnValue = "1"; |
| | | } |
| | | //update-begin-author:taoyan date:20210330 for:å¤ç§æ·IDä½ä¸ºç³»ç»åé |
| | | else if (key.equals(TenantConstant.TENANT_ID) || key.toLowerCase().equals(TenantConstant.TENANT_ID_TABLE)){ |
| | | returnValue = SpringContextUtils.getHttpServletRequest().getHeader(CommonConstant.TENANT_ID); |
| | | } |
| | | //update-end-author:taoyan date:20210330 for:å¤ç§æ·IDä½ä¸ºç³»ç»åé |
| | | if(returnValue!=null){returnValue = returnValue + moshi;} |
| | | return returnValue; |
| | | } |
| | | |
| | | // public static void main(String[] args) { |
| | | // String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1NjUzMzY1MTMsInVzZXJuYW1lIjoiYWRtaW4ifQ.xjhud_tWCNYBOg_aRlMgOdlZoWFFKB_givNElHNw3X0"; |
| | | // System.out.println(JwtUtil.getUsername(token)); |
| | | // } |
| | | } |