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

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

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