SOA中springmvc中restful服務動態刷新token信息

由於要考慮服務端token的動態刷新,並且還要單獨啓動定時調度任務去刷新token信息,保證token的時效及安全問題,直接分享動態刷新token的代碼:html

Java代碼 複製代碼 收藏代碼安全

  1. @CrossOrigin(origins = "*", maxAge = 3600,methods={RequestMethod.GET,RequestMethod.POST,RequestMethod.DELETE,RequestMethod.PUT})  
  2. @RestController  
  3. @RequestMapping(value = "/rest/soa")  
  4. public class SoaServiceResource {  
  5.       
  6.     private static final Logger logger = Logger.getLogger(SoaServiceResource.class);  
  7.     @Autowired  
  8.     private SoaAppSecretService soaAppSecretService;  
  9.       
  10.     /** 
  11.      * 刷新應用token信息 
  12.      * @param request 
  13.      * @param response 
  14.      * @return 
  15.      */  
  16.     @RequestMapping(value = "/refAppSecret", method = RequestMethod.GET)  
  17.     public ResponseVO refAppSecret(@RequestParam(required=false) String appname, HttpServletRequest request, HttpServletResponse response){  
  18.         try {  
  19.             if(StringUtils.isEmpty(appname)){  
  20.                 return SoaResponseCode.buildEnumResponseVO(SoaServiceEnum.APPNAME_NOT_NULL, null);  
  21.             }  
  22.             //根據應用名獲取祕鑰信息  
  23.             SoaAppSecret appSecret = soaAppSecretService.findAppSecretByAppName(appname);  
  24.             if(null == appSecret){  
  25.                 return SoaResponseCode.buildEnumResponseVO(SoaServiceEnum.APPNAME_NOT_EXIST, null);  
  26.             }  
  27.             String appsecret = appSecret.getAppsecret();  
  28.             if(StringUtils.isNotEmpty(appsecret)){  
  29.                 long afterTime = DateUtils.getAfterTime(new Date());  
  30.                 String token = EncryptUtil2.encryptToAES(EncryptUtil2.AESKey,  appname + EncryptUtil2.CONNECTOR + appsecret + EncryptUtil2.CONNECTOR + afterTime);  
  31.                 SoaAppSecret soaAppSecret = new SoaAppSecret();  
  32.                 soaAppSecret.setToken(token);  
  33.                 soaAppSecret.setAppname(appname);  
  34.                 soaAppSecret.setUpdateDate(new Date());  
  35.                 soaAppSecretService.refAppSecret(soaAppSecret);  
  36.                 JSONObject data = new JSONObject();  
  37.                 data.put("token", token);  
  38.                 return SoaResponseCode.buildEnumResponseVO(SoaServiceEnum.REQUEST_SUCCESS, data);  
  39.             }  
  40.             return SoaResponseCode.buildEnumResponseVO(SoaServiceEnum.APPSECRET_NOT_EXIST, null);  
  41.         } catch (Exception e) {  
  42.             logger.error("SoaServiceResource >> refAppSecret >> Exception " + e.getMessage());  
  43.             return SoaResponseCode.buildEnumResponseVO(SoaServiceEnum.SYSTEM_ERROR, null);  
  44.         }  
  45.     }  
  46.       
  47.     /** 
  48.      * 批量刷新應用祕鑰信息 
  49.      * @param request 
  50.      * @param response 
  51.      * @return 
  52.      */  
  53.     @RequestMapping(value = "/batchRefAppSecret", method = RequestMethod.GET)  
  54.     public ResponseVO batchRefAppSecret(HttpServletRequest request, HttpServletResponse response){  
  55.         List<SoaAppSecret> appSecretList = soaAppSecretService.findList(new SoaAppSecret());  
  56.         if(null != appSecretList && appSecretList.size() > 0){  
  57.             for(SoaAppSecret soaAppSecret : appSecretList){  
  58.                 try {  
  59.                     String appsecret = soaAppSecret.getAppsecret();  
  60.                     if(StringUtils.isNotEmpty(appsecret)){  
  61.                         long afterTime = DateUtils.getAfterTime(new Date());  
  62.                         String token = EncryptUtil2.encryptToAES(EncryptUtil2.AESKey,  soaAppSecret.getAppname() + EncryptUtil2.CONNECTOR + appsecret + EncryptUtil2.CONNECTOR + afterTime);  
  63.                         soaAppSecret.setToken(token);  
  64.                         soaAppSecret.setUpdateDate(new Date());  
  65.                         soaAppSecretService.refAppSecret(soaAppSecret);  
  66.                         JSONObject data = new JSONObject();  
  67.                         data.put("token", token);  
  68.                         return SoaResponseCode.buildEnumResponseVO(SoaServiceEnum.REQUEST_SUCCESS, data);  
  69.                     }  
  70.                       
  71.                     logger.info("SoaServiceResource >> batchRefAppSecret >> 刷新應用祕鑰信息成功,應用名: " + soaAppSecret.getAppname() + ",新的token信息: " + soaAppSecret.getToken() );  
  72.                     return SoaResponseCode.buildEnumResponseVO(SoaServiceEnum.APPSECRET_NOT_EXIST, null);  
  73.                 } catch (Exception e) {  
  74.                     logger.error("SoaServiceResource >> batchRefAppSecret >> Exception " + e.getMessage());  
  75.                 }  
  76.             }  
  77.         }  
  78.         return SoaResponseCode.buildEnumResponseVO(SoaServiceEnum.REQUEST_SUCCESS, true);  
  79.     }  
  80.       
  81.     /** 
  82.      * 獲取應用token 
  83.      * @param request 
  84.      * @param response 
  85.      * @return 
  86.      */  
  87.     @RequestMapping(value = "/findAppSecret", method = RequestMethod.GET)  
  88.     public ResponseVO findAppSecret(@RequestParam(required=false) String appname, HttpServletRequest request, HttpServletResponse response){  
  89.         if(StringUtils.isEmpty(appname)){  
  90.             return SoaResponseCode.buildEnumResponseVO(SoaServiceEnum.APPNAME_NOT_NULL, null);  
  91.         }  
  92.         SoaAppSecret appSecret = soaAppSecretService.findAppSecretByAppName(appname);  
  93.         if(null == appSecret){  
  94.             return SoaResponseCode.buildEnumResponseVO(SoaServiceEnum.APPNAME_NOT_EXIST, null);  
  95.         }  
  96.         JSONObject data = new JSONObject();  
  97.         data.put("token", appSecret.getToken());  
  98.         return SoaResponseCode.buildEnumResponseVO(SoaServiceEnum.REQUEST_SUCCESS, data);  
  99.     }  
  100.       
  101. }  

    願意瞭解框架技術或者源碼的朋友直接求求交流分享技術:3133806896app

    分佈式的一些解決方案,有願意瞭解的朋友能夠找咱們團隊探討框架

    更多詳細源碼參考來源分佈式

相關文章
相關標籤/搜索