Commit 3e4e8b4c by 刘宇峰

授权服务

1 parent 31448eae
...@@ -5,6 +5,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties; ...@@ -5,6 +5,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
/** /**
...@@ -29,7 +30,7 @@ public class ApiGatewayServiceConfig { ...@@ -29,7 +30,7 @@ public class ApiGatewayServiceConfig {
public void setServices(Map<String, GatewayService> services) { public void setServices(Map<String, GatewayService> services) {
Map<String, GatewayService> newMap = new ConcurrentHashMap<>(); Map<String, GatewayService> newMap = new ConcurrentHashMap<>();
for(Map.Entry<String, GatewayService> entry : services.entrySet()){ for (Map.Entry<String, GatewayService> entry : services.entrySet()) {
GatewayService gatewayService = entry.getValue(); GatewayService gatewayService = entry.getValue();
gatewayService.setName(entry.getKey()); gatewayService.setName(entry.getKey());
newMap.put(gatewayService.getAppKey(), gatewayService); newMap.put(gatewayService.getAppKey(), gatewayService);
...@@ -37,7 +38,17 @@ public class ApiGatewayServiceConfig { ...@@ -37,7 +38,17 @@ public class ApiGatewayServiceConfig {
this.services = newMap; this.services = newMap;
} }
public GatewayService getServiceByAppKey(String appKey){ public GatewayService getServiceByAppKey(String appKey) {
return this.services .get(appKey); return this.services.get(appKey);
} }
public GatewayService getServiceByName(String serName) {
for (GatewayService v : services.values()) {
if (Objects.equals(v.getName(), serName)) {
return v;
}
}
return null;
}
} }
...@@ -3,13 +3,17 @@ package cn.com.glsx.auth.modules.controller; ...@@ -3,13 +3,17 @@ package cn.com.glsx.auth.modules.controller;
import cn.com.glsx.auth.config.ApiGatewayServiceConfig; import cn.com.glsx.auth.config.ApiGatewayServiceConfig;
import cn.com.glsx.auth.model.GatewayService; import cn.com.glsx.auth.model.GatewayService;
import cn.com.glsx.auth.model.OpenApiResult; import cn.com.glsx.auth.model.OpenApiResult;
import cn.com.glsx.auth.modules.request.CraneQueryAccessTokenRequest; import cn.com.glsx.auth.modules.request.QueryAccessTokenRequest;
import cn.com.glsx.auth.modules.utils.SM3Utils; import cn.com.glsx.auth.modules.utils.SM3Utils;
import cn.com.glsx.auth.utils.GatewayJwtUtils; import cn.com.glsx.auth.utils.GatewayJwtUtils;
import com.glsx.plat.common.annotation.SysLog;
import com.glsx.plat.common.enums.OperateType;
import com.glsx.plat.common.utils.DateUtils; import com.glsx.plat.common.utils.DateUtils;
import com.glsx.plat.common.utils.MD5Util; import com.glsx.plat.common.utils.MD5Util;
import com.glsx.plat.exception.OpenApiResultException; import com.glsx.plat.exception.OpenApiResultException;
import com.glsx.plat.exception.ResultException; import com.glsx.plat.exception.ResultException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
...@@ -31,6 +35,7 @@ import java.util.Map; ...@@ -31,6 +35,7 @@ import java.util.Map;
* @Date 2023/4/6 18:08 * @Date 2023/4/6 18:08
*/ */
@Slf4j @Slf4j
@Tag(name = "授权Api接口")
@RestController @RestController
@RequestMapping(value = "/open/api") @RequestMapping(value = "/open/api")
public class AuthController { public class AuthController {
...@@ -41,10 +46,12 @@ public class AuthController { ...@@ -41,10 +46,12 @@ public class AuthController {
@Autowired @Autowired
private GatewayJwtUtils gatewayJwtUtils; private GatewayJwtUtils gatewayJwtUtils;
@SysLog(module = "授权", action = OperateType.API, value = "授权", saveLog = false)
@Operation(summary = "获取AccessToken接口", description = "提供给客户方的业务接口的认证令牌,在调用其他接口时,作为调用接口的凭证参数")
@PostMapping("/access/queryAccessToken") @PostMapping("/access/queryAccessToken")
public OpenApiResult<Map<String, Object>> queryAccessToken(@Valid @RequestBody CraneQueryAccessTokenRequest request) { public OpenApiResult<Map<String, Object>> queryAccessToken(@Valid @RequestBody QueryAccessTokenRequest request) {
try { try {
GatewayService gatewayService = verrfyRequestParam(request); GatewayService gatewayService = verifyRequestParam(request);
//生成token返回 //生成token返回
Map<String, Object> userMap = new HashMap<>(); Map<String, Object> userMap = new HashMap<>();
userMap.put("name", gatewayService.getName()); userMap.put("name", gatewayService.getName());
...@@ -55,11 +62,11 @@ public class AuthController { ...@@ -55,11 +62,11 @@ public class AuthController {
resultMap.put("expiresAt", DateUtils.format(DateUtils.addDateDays(new Date(), 1), DateUtils.NORMAL_DATE_TIME_PATTERN)); resultMap.put("expiresAt", DateUtils.format(DateUtils.addDateDays(new Date(), 1), DateUtils.NORMAL_DATE_TIME_PATTERN));
return OpenApiResult.success(resultMap); return OpenApiResult.success(resultMap);
} catch (Exception e) { } catch (Exception e) {
throw OpenApiResultException.create("授权失败"); throw OpenApiResultException.create("授权失败 " + e.getMessage());
} }
} }
private GatewayService verrfyRequestParam(CraneQueryAccessTokenRequest request) { private GatewayService verifyRequestParam(QueryAccessTokenRequest request) {
GatewayService gatewayService = apiGatewayServiceConfig.getServiceByAppKey(request.getAppKey()); GatewayService gatewayService = apiGatewayServiceConfig.getServiceByAppKey(request.getAppKey());
if (gatewayService == null) { if (gatewayService == null) {
throw ResultException.create("请求参数校验不通过"); throw ResultException.create("请求参数校验不通过");
......
package cn.com.glsx.auth.modules.controller;
import cn.com.glsx.auth.modules.request.LoginRequest;
import com.glsx.plat.common.annotation.SysLog;
import com.glsx.plat.common.enums.OperateType;
import com.glsx.plat.core.web.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@Tag(name = "登录接口")
@RestController
@RequestMapping(value = "/login")
public class LoginController {
@Operation(summary = "系统登录", description = "通过账号密码或手机验证码方式登录系统")
@SysLog(module = "系统登录", action = OperateType.LOGIN, value = "登录", saveLog = false)
@PostMapping(value = "/login")
public R login(@RequestBody @Validated LoginRequest request) {
//1、用户中心登录
//User user = userService.localAuth(request);
//2、创建授权令牌
String token = "123456";
Map<String, Object> rtnMap = new HashMap<>();
//rtnMap.put("user", user);
rtnMap.put("token", token);
return R.ok().data(rtnMap);
}
@Operation(summary = "登出系统")
@SysLog(module = "系统登出", action = OperateType.LOGOUT, value = "登出", saveLog = false)
@GetMapping(value = "/logout")
public R logout() {
return R.ok();
}
}
package cn.com.glsx.auth.modules.request;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
@Data
public class LoginRequest {
@Schema(description = "登录平台")
private String platform;
@Schema(description = "登录类型(1xxx,2xxx)")
@NotNull(message = "登录类型不能为空")
private Integer loginType;
@Schema(description = "账号")
@NotBlank(message = "账号不能为空")
private String account;
@Schema(description = "验证码/密码")
@NotBlank(message = "验证码/密码不能为空")
private String code;
}
...@@ -7,13 +7,13 @@ import javax.validation.constraints.NotBlank; ...@@ -7,13 +7,13 @@ import javax.validation.constraints.NotBlank;
/** /**
* @author huangch * @author huangch
* @version V1.0 * @version V1.0
* @Title: CraneQueryAccessTokenRequest * @Title: QueryAccessTokenRequest
* @Description: * @Description:
* @Company: Didihu.com.cn * @Company: Didihu.com.cn
* @Date 2023/4/13 15:31 * @Date 2023/4/13 15:31
*/ */
@Data @Data
public class CraneQueryAccessTokenRequest { public class QueryAccessTokenRequest {
@NotBlank(message = "appKey不能为空") @NotBlank(message = "appKey不能为空")
private String appKey; private String appKey;
...@@ -28,4 +28,5 @@ public class CraneQueryAccessTokenRequest { ...@@ -28,4 +28,5 @@ public class CraneQueryAccessTokenRequest {
private String timestamp; private String timestamp;
private String grantType; private String grantType;
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!