123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- package com.miniframe.service;
- import com.miniframe.tools.JwtManageUtil;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Map;
- public class AccessClient {
- public static Map<String,AccessToken> clientJwtToken=new HashMap<>();
- public static class AccessToken{
- private String uniqueId;
- private String token;
- private String userId;
- private String saltToken;
- private Date lastTokenTime;
- public String getUniqueId() {
- return uniqueId;
- }
- public void setUniqueId(String uniqueId) {
- this.uniqueId = uniqueId;
- }
- public String getUserId() {
- return userId;
- }
- public void setUserId(String userId) {
- this.userId = userId;
- }
- public String getSaltToken() {
- return saltToken;
- }
- public void setSaltToken(String saltToken) {
- this.saltToken = saltToken;
- }
- public Date getLastTokenTime() {
- return lastTokenTime;
- }
- public AccessToken(String uniqueId, String userId, String saltToken){
- this.uniqueId=uniqueId;
- this.userId=userId;
- this.saltToken=saltToken;
- generateToken();
- }
- private void generateToken(){
- try {
- this.lastTokenTime=new Date();
- this.token=JwtManageUtil.getClientJWT(uniqueId,userId,saltToken);
- } catch (Exception e) {
- this.token=null;
- this.lastTokenTime=null;
- e.printStackTrace();
- }
- }
- public String getToken(){
- if(lastTokenTime==null){
- generateToken();
- }else{
- Date now=new Date();
- if((now.getTime()+600000-lastTokenTime.getTime())>=JwtManageUtil.Constant.JWT_TTL){ //token超时,重新生成
- generateToken();
- }
- }
- return this.token;
- }
- }
- static {
- try {
- //增加jwt token :projectName svcName userToAccessSvc
- clientJwtToken.put("cae_service_manager",new AccessToken("b87cb209-e852-42bc-8d58-bc771a57b3eb","manager","12345678901234567890"));
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static String getToken(String projectName,String serviceName,String user){
- String id=projectName+"_"+serviceName+"_"+user;
- if(clientJwtToken.containsKey(id)){
- return clientJwtToken.get(id).getToken();
- }
- return "";
- }
- }
|