From 62b508a7f872fa4f8e14aa8d766e4a6c1e34cd34 Mon Sep 17 00:00:00 2001 From: 潘志宝 <979469083@qq.com> Date: 星期五, 22 十一月 2024 10:37:35 +0800 Subject: [PATCH] Merge branch 'master' of http://dlindusit.com:53929/r/iailab-plat --- iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/service/auth/AdminAuthService.java | 8 ++++ iailab-module-system/iailab-module-system-biz/src/main/resources/application-dev.yaml | 8 ++-- iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/controller/admin/auth/AuthController.java | 10 ++++- iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/convert/oauth2/OAuth2OpenConvert.java | 2 iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/service/auth/AdminAuthServiceImpl.java | 6 +++ iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/controller/admin/oauth2/OAuth2OpenController.java | 31 +++++++++------ 6 files changed, 46 insertions(+), 19 deletions(-) diff --git a/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/controller/admin/auth/AuthController.java b/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/controller/admin/auth/AuthController.java index fc005c4..2181fd7 100644 --- a/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/controller/admin/auth/AuthController.java +++ b/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/controller/admin/auth/AuthController.java @@ -1,6 +1,7 @@ package com.iailab.module.system.controller.admin.auth; import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.date.LocalDateTimeUtil; import cn.hutool.core.util.StrUtil; import com.iailab.framework.common.enums.CommonStatusEnum; import com.iailab.framework.common.enums.UserTypeEnum; @@ -97,8 +98,13 @@ @PermitAll @Operation(summary = "刷新令牌") @Parameter(name = "refreshToken", description = "刷新令牌", required = true) - public CommonResult<AuthLoginRespVO> refreshToken(@RequestParam("refreshToken") String refreshToken) { - return success(authService.refreshToken(refreshToken)); + public Map<String, Object> refreshToken(@RequestParam("refreshToken") String refreshToken, @RequestParam("clientId") String clientId) { + AuthLoginRespVO authLoginRespVO = authService.refreshToken(refreshToken, clientId); + Map<String, Object> map = new HashMap<>(); + map.put("access_token", authLoginRespVO.getAccessToken()); + map.put("refresh_token", authLoginRespVO.getRefreshToken()); + map.put("expires_time", LocalDateTimeUtil.toEpochMilli(authLoginRespVO.getExpiresTime()) / 1000L); + return map; } @GetMapping("/get-permission-info") diff --git a/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/controller/admin/oauth2/OAuth2OpenController.java b/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/controller/admin/oauth2/OAuth2OpenController.java index 44791bf..0b3b283 100644 --- a/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/controller/admin/oauth2/OAuth2OpenController.java +++ b/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/controller/admin/oauth2/OAuth2OpenController.java @@ -1,5 +1,6 @@ package com.iailab.module.system.controller.admin.oauth2; +import cn.hutool.core.date.LocalDateTimeUtil; import cn.hutool.core.lang.Assert; import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ObjectUtil; @@ -33,6 +34,7 @@ import javax.annotation.security.PermitAll; import javax.servlet.http.HttpServletRequest; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -86,23 +88,23 @@ @Operation(summary = "获得访问令牌", description = "适合 code 授权码模式,或者 implicit 简化模式;在 sso.vue 单点登录界面被【获取】调用") @Parameters({ @Parameter(name = "grant_type", required = true, description = "授权类型", example = "code"), - @Parameter(name = "code", description = "授权范围", example = "userinfo.read"), + @Parameter(name = "code", description = "授权码", example = "asdfasdfasdf"), @Parameter(name = "redirect_uri", description = "重定向 URI", example = "https://www.baidu.com"), @Parameter(name = "state", description = "状态", example = "1"), @Parameter(name = "username", example = "tudou"), @Parameter(name = "password", example = "cai"), // 多个使用空格分隔 - @Parameter(name = "scope", example = "user_info"), + @Parameter(name = "scope", description = "授权范围", example = "user.read"), @Parameter(name = "refresh_token", example = "123424233"), }) - public CommonResult<OAuth2OpenAccessTokenRespVO> postAccessToken(HttpServletRequest request, - @RequestParam("grant_type") String grantType, - @RequestParam(value = "code", required = false) String code, // 授权码模式 - @RequestParam(value = "redirect_uri", required = false) String redirectUri, // 授权码模式 - @RequestParam(value = "state", required = false) String state, // 授权码模式 - @RequestParam(value = "username", required = false) String username, // 密码模式 - @RequestParam(value = "password", required = false) String password, // 密码模式 - @RequestParam(value = "scope", required = false) String scope, // 密码模式 - @RequestParam(value = "refresh_token", required = false) String refreshToken) { // 刷新模式 + public Map<String, Object> postAccessToken(HttpServletRequest request, + @RequestParam("grant_type") String grantType, + @RequestParam(value = "code", required = false) String code, // 授权码模式 + @RequestParam(value = "redirect_uri", required = false) String redirectUri, // 授权码模式 + @RequestParam(value = "state", required = false) String state, // 授权码模式 + @RequestParam(value = "username", required = false) String username, // 密码模式 + @RequestParam(value = "password", required = false) String password, // 密码模式 + @RequestParam(value = "scope", required = false) String scope, // 密码模式 + @RequestParam(value = "refresh_token", required = false) String refreshToken) { // 刷新模式 List<String> scopes = OAuth2Utils.buildScopes(scope); // 1.1 校验授权类型 OAuth2GrantTypeEnum grantTypeEnum = OAuth2GrantTypeEnum.getByGrantType(grantType); @@ -137,7 +139,12 @@ throw new IllegalArgumentException("未知授权类型:" + grantType); } Assert.notNull(accessTokenDO, "访问令牌不能为空"); // 防御性检查 - return success(OAuth2OpenConvert.INSTANCE.convert(accessTokenDO)); + Map<String, Object> map = new HashMap<>(); + map.put("access_token", accessTokenDO.getAccessToken()); + map.put("refresh_token", accessTokenDO.getRefreshToken()); + map.put("expires_time", LocalDateTimeUtil.toEpochMilli(accessTokenDO.getExpiresTime()) / 1000L); + map.put("client_id", accessTokenDO.getClientId()); + return map; } @DeleteMapping("/token") diff --git a/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/convert/oauth2/OAuth2OpenConvert.java b/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/convert/oauth2/OAuth2OpenConvert.java index fc97c28..9610826 100644 --- a/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/convert/oauth2/OAuth2OpenConvert.java +++ b/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/convert/oauth2/OAuth2OpenConvert.java @@ -34,7 +34,7 @@ default OAuth2OpenCheckTokenRespVO convert2(OAuth2AccessTokenDO bean) { OAuth2OpenCheckTokenRespVO respVO = BeanUtils.toBean(bean, OAuth2OpenCheckTokenRespVO.class); - respVO.setExp(LocalDateTimeUtil.toEpochMilli(bean.getExpiresTime()) / 1000L); + respVO.setExp(LocalDateTimeUtil.toEpochMilli(bean.getExpiresTime())); respVO.setUserType(UserTypeEnum.ADMIN.getValue()); return respVO; } diff --git a/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/service/auth/AdminAuthService.java b/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/service/auth/AdminAuthService.java index e5aa0e0..6e8db15 100644 --- a/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/service/auth/AdminAuthService.java +++ b/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/service/auth/AdminAuthService.java @@ -70,4 +70,12 @@ */ AuthLoginRespVO refreshToken(String refreshToken); + /** + * 刷新访问令牌 + * + * @param refreshToken 刷新令牌 + * @return 登录结果 + */ + AuthLoginRespVO refreshToken(String refreshToken, String clientId); + } diff --git a/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/service/auth/AdminAuthServiceImpl.java b/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/service/auth/AdminAuthServiceImpl.java index 46d51a1..c28af71 100644 --- a/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/service/auth/AdminAuthServiceImpl.java +++ b/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/service/auth/AdminAuthServiceImpl.java @@ -208,6 +208,12 @@ } @Override + public AuthLoginRespVO refreshToken(String refreshToken, String clientId) { + OAuth2AccessTokenDO accessTokenDO = oauth2TokenService.refreshAccessToken(refreshToken, clientId); + return AuthConvert.INSTANCE.convert(accessTokenDO); + } + + @Override public void logout(String token, Integer logType) { // 删除访问令牌 OAuth2AccessTokenDO accessTokenDO = oauth2TokenService.removeAccessToken(token); diff --git a/iailab-module-system/iailab-module-system-biz/src/main/resources/application-dev.yaml b/iailab-module-system/iailab-module-system-biz/src/main/resources/application-dev.yaml index 90aa6ff..10c052f 100644 --- a/iailab-module-system/iailab-module-system-biz/src/main/resources/application-dev.yaml +++ b/iailab-module-system/iailab-module-system-biz/src/main/resources/application-dev.yaml @@ -53,7 +53,7 @@ host: 127.0.0.1 # 地址 port: 6379 # 端口 database: 1 # 数据库索引 - #password: 123456 # 密码,建议生产环境开启 + password: 123456 # 密码,建议生产环境开启 --- #################### MQ 消息队列相关配置 #################### @@ -64,10 +64,10 @@ spring: # RabbitMQ 配置项,对应 RabbitProperties 配置类 rabbitmq: - host: 127.0.0.1 # RabbitMQ 服务的地址 + host: 172.16.8.200 # RabbitMQ 服务的地址 port: 5672 # RabbitMQ 服务的端口 - username: guest # RabbitMQ 服务的账号 - password: guest # RabbitMQ 服务的密码 + username: admin # RabbitMQ 服务的账号 + password: admin123 # RabbitMQ 服务的密码 # Kafka 配置项,对应 KafkaProperties 配置类 kafka: bootstrap-servers: 127.0.0.1:9092 # 指定 Kafka Broker 地址,可以设置多个,以逗号分隔 -- Gitblit v1.9.3