`
白马探
  • 浏览: 14616 次
社区版块
存档分类
最新评论

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.       
  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. }
分享到:
评论

相关推荐

    springMVC搭建Restful服务

    在本教程中,我们将专注于如何使用Spring MVC 4搭建一个基于RESTful风格的服务。REST(Representational State Transfer)是一种软件架构风格,它设计用于创建简单、可扩展的网络服务。 **一、RESTful服务的基本...

    SpringMVC的Restful风格Demo

    在SpringMVC中实现RESTful风格,可以创建更加灵活、易于理解和维护的API。让我们深入探讨一下SpringMVC如何实现RESTful风格以及相关知识点。 首先,理解RESTful的基本原则至关重要。REST(Representational State ...

    springmvc 3.2.8 restful支持

    在3.2.8版本中,它增强了对RESTful风格的支持,使得开发者可以更容易地创建符合REST原则的服务。REST(Representational State Transfer)是一种网络应用程序的设计风格和开发方式,基于HTTP协议,强调资源的表述和...

    SpringMVC-7 RESTful CRUD

    在SpringMVC中,我们使用`@RestController`注解来标记控制器类,它结合了`@Controller`和`@ResponseBody`,用于创建RESTful服务。例如: ```java @RestController @RequestMapping("/users") public class ...

    springmvc 实现restful

    在实际开发中,还需要考虑安全性、性能优化、版本控制等问题,确保RESTful服务的质量和稳定性。 总结来说,Spring MVC提供了强大的工具和支持,使得实现RESTful服务变得简单而高效。通过合理的资源设计和HTTP方法的...

    SpringMVC 使用 RESTful 架构实现 CRUD 操作

    在本教程中,我们将探讨如何使用 SpringMVC 实现基于 RESTful 风格的 CRUD(Create、Read、Update、Delete)操作。 **CRUD 操作概述** CRUD 操作是任何数据驱动应用的基础,它们分别代表创建新记录、读取现有记录、...

    springmvc之restful风格CRUD

    本文将深入探讨如何在Spring MVC中实现RESTful风格的CRUD操作,这对初学者尤其有价值。 首先,了解REST(Representational State Transfer,表述性状态转移)是一种网络应用程序的设计风格和开发方式,基于HTTP协议...

    SpringMVC RESTFUL风格

    在SpringMVC框架中实现RESTFUL风格非常简单,只需要合理运用`@RequestMapping`和`@PathVariable`注解即可。此外,RESTFUL风格的应用还能带来诸如易于缓存、更好的安全性等一系列好处,因此在现代Web应用开发中得到了...

    SpringMVC&Restful.jar

    SpringMVC&Restful所需要的jar包,其中包含了spring-aop-4.0.0.RELEASE.jar,bean,aop,context,core,web,webMVC,commons等jar

    Spring MVC--6.RESTful SpringMVC CRUD

    在本教程中,我们将深入探讨如何使用Spring MVC构建RESTful Web服务,并实现基本的CRUD操作。Spring MVC是Spring框架的一部分,它为构建基于Java的Web应用程序提供了强大的模型-视图-控制器(MVC)架构。RESTful ...

    spring 3.0 应用springmvc 构造RESTful URL 详细讲解

    在Spring 3.0框架中,Spring MVC是一个强大的用于构建Web应用程序的模块,它支持构建RESTful风格的URL,使得应用程序更加符合Web服务的最佳实践。REST(Representational State Transfer)是一种设计模式,强调通过...

    SpringMVC+mongodb+freemarker+apiToken开源内容管理系统.zip

    这是一个基于SpringMVC、MongoDB、FreeMarker和API Token构建的开源内容管理系统,适用于Web开发,特别是毕业设计项目。下面将详细介绍这些技术栈及其在系统中的应用。 **SpringMVC**: SpringMVC是Spring框架的一...

    spring+springmvc+mybatis+restful+mysql

    标题 "spring+springmvc+mybatis+restful+mysql" 描述了一个基于Java技术栈的Web应用程序开发架构,这是企业级应用开发中常见的组合。这个架构由五个主要部分组成: 1. **Spring**:这是一个全面的Java应用框架,...

    springMVC伪静态和restful服务以及json日期格式

    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...

    restful webservice in springMVC Demo

    在"restful webservice in springMVC Demo"中,开发者可能创建了一个简单的REST服务,例如,用于管理用户信息。服务可能包括了获取用户列表、创建新用户、更新用户信息和删除用户等操作。客户端部分(restful_...

    SpringMVC+Maven实现Restful源码

    本文将深入探讨如何使用SpringMVC和Maven来实现一个Restful服务的源码,以及这两个工具各自的功能和优势。 首先,让我们了解一下SpringMVC。SpringMVC是Spring框架的一部分,专门用于构建Web应用程序。它采用模型-...

    swagger整合SpringMvc生成Restful api

    swagger提供的接口文档相比传统的文档方式更加直观也更加高效,但是在网上找了很多关于Swagger与SpringMvc整合的资料,发现都比较繁琐,不是很满意,于是有了这篇博客,希望对大家有所帮助。教程:...

    SpringMVC+restful demo

    **SpringMVC+RESTful 框架...通过阅读和运行这些代码,你将更深入地理解SpringMVC的控制器、服务层以及RESTful设计模式。同时,这也有助于你掌握HTTP方法在实际应用中的运用,以及如何通过注解实现请求和响应的映射。

    spring_3.0_应用springmvc_构造RESTful_URL_详细讲解

    RESTful URL设计是现代Web服务的核心原则之一,它强调资源的表述状态转移,使得API更加清晰、易于理解和使用。本教程将深入探讨如何在Spring 3.0中应用Spring MVC来构造RESTful URL。 首先,了解REST...

Global site tag (gtag.js) - Google Analytics