因为要考虑服务端token的动态刷新,而且还要单独启动定时调度任务去刷新token信息,保证token的时效及安全问题,直接分享动态刷新token的代码:
@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信息 * @param request * @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<SoaAppSecret> 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); } }
相关推荐
使用springMVC4 搭建Restful轻服务。
基于springmvc3.2.x +tomcat8.0 ,使用ajax 发送restful请求处理数据
SpringMVC的Restful风格Demo
利用springmvc ,实现对http url 的restful 编码风格,需要大家提前配置好maven 环境
SpringMVC 使用 RESTful 架构实现 CRUD 操作
spring_3.0_应用springmvc_构造RESTful_URL_详细讲解
SpringMVC&Restful所需要的jar包,其中包含了spring-aop-4.0.0.RELEASE.jar,bean,aop,context,core,web,webMVC,commons等jar
使用REST风格的URL实现数据信息的增删改查操作、使用了SpringMVC提供的表单标签来简化页面开发并实现表单回显功能、且解决了SpringMVC的静态资源处理问题
pring_3.0_应用springmvc_构造RESTful_URL_详细讲解
springmvc的rest风格的增删改查,适合springmvc的新手
Spring MVC--6.RESTful SpringMVC CRUD
SpringMVC RESTFUL风格的写法,以及与非RESTFUL的区别
SpringMVC+mongodb+freemarker+apiToken开源内容管理系统.zip
swagger提供的接口文档相比传统的文档方式更加直观也更加高效,但是在网上找了很多关于Swagger与SpringMvc整合的资料,发现都比较繁琐,不是很满意,于是有了这篇博客,希望对大家有所帮助。教程:...
restful访问方式: http://localhost:8080/jsp-rewrite/api/saveUser requestheader:Content-Type=application/json;charset=UTF-8 requestbody:{"id":3,"username":"王五","userpwd":"123456","birthdy":"2013-11...
该源码使用SpringMVC+RESTFul搭建的web 服务框架。适用于初学者了解本框架的处理过程
Dubbo-Zookeeper-Netty-SpringMVC, 使用dubbo注册服务,netty做服务器,springmvc提供restful接口
springmvc的RESTFul实现小案例
基于springmvc 的 RestfulCRUD实例练习程序代码工程,解压后倒入eclipse即可查看,个人以编译通过