设为首页收藏本站我的广告

运维网

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
总共321条微博

每日一博

查看: 170|回复: 1

[其它] 【站长推荐】SOA中springmvc中restful服务动态刷新token信息【顶】

[复制链接]

该用户从未签到

啊拉 发表于 2017-11-9 10:51:14 | 显示全部楼层 |阅读模式
【重大新闻】 因为要考虑服务端token的动态刷新,而且还要单独启动定时调度任务去刷新token信息,保证token的时效及安全问题,直接分享动态刷新token的代码:

Java代码













@CrossOrigin(origins = "*", maxAge = 3600,methods={RequestMethod.GET,RequestMethod.POST,RequestMethod.DELETE,RequestMethod.PUT})

@RestController

@RequestMapping(value = "/rest/soa")

public class SoaServiceResource {

private static final Logger logger = Logger.getLogger(SoaServiceResource.class);

@Autowired

private SoaAppSecretService soaAppSecretService;

/**

* 刷新应用token信息

*@paramrequest

* @param response

* @return

*/

@RequestMapping(value = "/refAppSecret", method = RequestMethod.GET)

public ResponseVO refAppSecret(@RequestParam(required=false) String appname, HttpServletRequest request, HttpServletResponse response){

try {

if(StringUtils.isEmpty(appname)){

return SoaResponseCode.buildEnumResponseVO(SoaServiceEnum.APPNAME_NOT_NULL, null);

}

//根据应用名获取秘钥信息

SoaAppSecret appSecret = soaAppSecretService.findAppSecretByAppName(appname);

if(null == appSecret){

return SoaResponseCode.buildEnumResponseVO(SoaServiceEnum.APPNAME_NOT_EXIST, null);

}

String appsecret = appSecret.getAppsecret();

if(StringUtils.isNotEmpty(appsecret)){

long afterTime = DateUtils.getAfterTime(new Date());

String token = EncryptUtil2.encryptToAES(EncryptUtil2.AESKey,  appname + EncryptUtil2.CONNECTOR + appsecret + EncryptUtil2.CONNECTOR + afterTime);

SoaAppSecret soaAppSecret = new SoaAppSecret();

soaAppSecret.setToken(token);

soaAppSecret.setAppname(appname);

soaAppSecret.setUpdateDate(new Date());

soaAppSecretService.refAppSecret(soaAppSecret);

JSONObject data = new JSONObject();

data.put("token", token);

return SoaResponseCode.buildEnumResponseVO(SoaServiceEnum.REQUEST_SUCCESS, data);

}

return SoaResponseCode.buildEnumResponseVO(SoaServiceEnum.APPSECRET_NOT_EXIST, null);

} catch (Exception e) {

logger.error("SoaServiceResource >> refAppSecret >> Exception " + e.getMessage());

return SoaResponseCode.buildEnumResponseVO(SoaServiceEnum.SYSTEM_ERROR, null);

}

}

/**

* 批量刷新应用秘钥信息

* @param request

* @param response

* @return

*/

@RequestMapping(value = "/batchRefAppSecret", method = RequestMethod.GET)

public ResponseVO batchRefAppSecret(HttpServletRequest request, HttpServletResponse response){

List appSecretList = soaAppSecretService.findList(new SoaAppSecret());

if(null != appSecretList && appSecretList.size() > 0){

for(SoaAppSecret soaAppSecret : appSecretList){

try {

String appsecret = soaAppSecret.getAppsecret();

if(StringUtils.isNotEmpty(appsecret)){

long afterTime = DateUtils.getAfterTime(new Date());

String token = EncryptUtil2.encryptToAES(EncryptUtil2.AESKey,  soaAppSecret.getAppname() + EncryptUtil2.CONNECTOR + appsecret + EncryptUtil2.CONNECTOR + afterTime);

soaAppSecret.setToken(token);

soaAppSecret.setUpdateDate(new Date());

soaAppSecretService.refAppSecret(soaAppSecret);

JSONObject data = new JSONObject();

data.put("token", token);

return SoaResponseCode.buildEnumResponseVO(SoaServiceEnum.REQUEST_SUCCESS, data);

}

logger.info("SoaServiceResource >> batchRefAppSecret >> 刷新应用秘钥信息成功,应用名: " + soaAppSecret.getAppname() + ",新的token信息: " + soaAppSecret.getToken() );

return SoaResponseCode.buildEnumResponseVO(SoaServiceEnum.APPSECRET_NOT_EXIST, null);

} catch (Exception e) {

logger.error("SoaServiceResource >> batchRefAppSecret >> Exception " + e.getMessage());

}

}

}

return SoaResponseCode.buildEnumResponseVO(SoaServiceEnum.REQUEST_SUCCESS, true);

}

/**

* 获取应用token

* @param request

* @param response

* @return

*/

@RequestMapping(value = "/findAppSecret", method = RequestMethod.GET)

public ResponseVO findAppSecret(@RequestParam(required=false) String appname, HttpServletRequest request, HttpServletResponse response){

if(StringUtils.isEmpty(appname)){

return SoaResponseCode.buildEnumResponseVO(SoaServiceEnum.APPNAME_NOT_NULL, null);

}

SoaAppSecret appSecret = soaAppSecretService.findAppSecretByAppName(appname);

if(null == appSecret){

return SoaResponseCode.buildEnumResponseVO(SoaServiceEnum.APPNAME_NOT_EXIST, null);

}

JSONObject data = new JSONObject();

data.put("token", appSecret.getToken());

return SoaResponseCode.buildEnumResponseVO(SoaServiceEnum.REQUEST_SUCCESS, data);

}

}

愿意了解框架技术或者源码的朋友直接求求交流分享技术:3133806896

分布式的一些解决方案,有愿意了解的朋友可以找我们团队探讨

更多详细源码参考来源

运维网 感谢您的阅读
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|申请友链|sitemap|手机版|小黑屋|Archiver|运维网 ( 京ICP备16008201号  

GMT+8, 2018-1-22 02:47 , Processed in 0.251882 second(s), 40 queries , XCache On.

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表