SOA中springmvc中restful服务动态刷新token信息

简介: token、springcloud

因为要考虑服务端token的动态刷新,而且还要单独启动定时调度任务去刷新token信息,(企业架构源码可以加求球:叁五三陆二肆柒二伍玖)保证token的时效及安全问题,直接分享动态刷新token的代码:

@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);  
    }  
      
}  
目录
相关文章
|
1月前
|
JSON API 数据库
解释如何在 Python 中实现 Web 服务(RESTful API)。
解释如何在 Python 中实现 Web 服务(RESTful API)。
45 0
|
14天前
|
安全 Java API
Java一分钟之-Spring Data REST:创建RESTful服务
【6月更文挑战第15天】Spring Data REST让基于Spring Data的项目轻松创建REST API,免去大量控制器代码。通过自动HTTP映射和链接生成,简化CRUD操作。文章涵盖启用REST、配置仓库、自定义端点、解决过度暴露、缺失逻辑和安全性问题,提供代码示例,如自定义Repository、投影和安全配置,强调在利用其便利性时注意潜在挑战。
26 5
|
18天前
|
SQL 缓存 测试技术
RESTful API设计的最佳实践:构建高效、可维护的Web服务接口
【6月更文挑战第11天】构建高效、可维护的RESTful API涉及多个最佳实践:遵循客户端-服务器架构、无状态性等REST原则;设计时考虑URL结构(动词+宾语,使用标准HTTP方法)、使用HTTP状态码、统一响应格式及错误处理;确保数据安全(HTTPS、认证授权、输入验证);实施版本控制;并提供详细文档和测试用例。这些实践能提升Web服务接口的性能和质量。
|
1天前
|
JSON Java Spring
实战SpringCloud响应式微服务系列教程(第八章)构建响应式RESTful服务
实战SpringCloud响应式微服务系列教程(第八章)构建响应式RESTful服务
|
3天前
|
安全 Java 测试技术
开发Java RESTful Web服务的技巧
开发Java RESTful Web服务的技巧
|
8天前
|
NoSQL Java MongoDB
实战SpringCloud响应式微服务系列教程(第十章)响应式RESTful服务完整代码示例
实战SpringCloud响应式微服务系列教程(第十章)响应式RESTful服务完整代码示例
|
1月前
|
JSON 网络架构 数据格式
SpringMVC -- REST风格开发,RESTful快速开发、RESTful注解开发
SpringMVC -- REST风格开发,RESTful快速开发、RESTful注解开发
40 2
|
1月前
|
Java API 数据库
利用Java构建高性能的RESTful Web服务
在现代软件开发中,RESTful Web服务已成为一种流行的架构模式,用于构建可扩展、可维护的网络应用。本文将探讨如何使用Java编程语言及其相关框架(如Spring Boot)来构建高性能的RESTful Web服务。我们将不仅仅关注基本的RESTful API设计,还将深入讨论性能优化、安全性、以及服务扩展性等方面的技术细节。通过本文,读者将能够掌握构建高效RESTful Web服务的核心技术和实践。
|
1月前
|
JSON JavaScript 中间件
利用Node.js和Express构建RESTful API服务
利用Node.js和Express构建RESTful API服务
28 0
|
1月前
|
前端开发 Java API
构建RESTful API:Java中的RESTful服务开发
【4月更文挑战第3天】本文介绍了在Java环境中构建RESTful API的重要性及方法。遵循REST原则,利用HTTP方法处理资源,实现CRUD操作。在Java中,常用框架如Spring MVC简化了RESTful服务开发,包括定义资源、设计表示层、实现CRUD、考虑安全性、文档和测试。通过Spring MVC示例展示了创建RESTful服务的步骤,强调了其在现代Web服务开发中的关键角色,有助于提升互操作性和用户体验。
构建RESTful API:Java中的RESTful服务开发