feat: 增加ERP接口鉴权与响应规范
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package cn.code.nl.module.lms.controller.erp;
|
||||
|
||||
import jakarta.validation.ConstraintViolationException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.MissingRequestHeaderException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
/** ERP 接口异常处理器。 */
|
||||
@Slf4j
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
@RestControllerAdvice(basePackages = "cn.code.nl.module.lms.controller.erp")
|
||||
public class ErpExceptionHandler {
|
||||
|
||||
/** 将请求参数异常转换为 400 响应。 */
|
||||
@ExceptionHandler({MethodArgumentNotValidException.class, ConstraintViolationException.class,
|
||||
HttpMessageNotReadableException.class, BindException.class})
|
||||
public ResponseEntity<ErpResult<Void>> handleBadRequest(Exception exception) {
|
||||
return build(HttpStatus.BAD_REQUEST, "请求参数错误");
|
||||
}
|
||||
|
||||
/** 将 Authorization 请求头缺失转换为 401 响应。 */
|
||||
@ExceptionHandler(MissingRequestHeaderException.class)
|
||||
public ResponseEntity<ErpResult<Void>> handleMissingAuthorization(MissingRequestHeaderException exception) {
|
||||
return build(HttpStatus.UNAUTHORIZED, "Authorization不能为空");
|
||||
}
|
||||
|
||||
/** 将带状态的 ERP 异常转换为对应响应。 */
|
||||
@ExceptionHandler(ResponseStatusException.class)
|
||||
public ResponseEntity<ErpResult<Void>> handleResponseStatus(ResponseStatusException exception) {
|
||||
int code = exception.getStatusCode().value();
|
||||
String message = exception.getReason() == null ? "请求处理失败" : exception.getReason();
|
||||
return ResponseEntity.status(code).body(ErpResult.error(code, message));
|
||||
}
|
||||
|
||||
/** 将业务对象不存在转换为 404 响应。 */
|
||||
@ExceptionHandler(NoSuchElementException.class)
|
||||
public ResponseEntity<ErpResult<Void>> handleNotFound(NoSuchElementException exception) {
|
||||
return build(HttpStatus.NOT_FOUND, "业务对象不存在");
|
||||
}
|
||||
|
||||
/** 将未识别异常转换为 500 响应。 */
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<ErpResult<Void>> handleException(Exception exception) {
|
||||
log.error("ERP接口处理发生未识别异常", exception);
|
||||
return build(HttpStatus.INTERNAL_SERVER_ERROR, "系统内部错误");
|
||||
}
|
||||
|
||||
/** 创建 HTTP 状态与响应体状态一致的错误响应。 */
|
||||
private ResponseEntity<ErpResult<Void>> build(HttpStatus status, String message) {
|
||||
return ResponseEntity.status(status).body(ErpResult.error(status.value(), message));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package cn.code.nl.module.lms.controller.erp;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* ERP 接口统一响应。
|
||||
*
|
||||
* @param <T> 响应数据类型
|
||||
*/
|
||||
@Data
|
||||
public class ErpResult<T> {
|
||||
|
||||
/** HTTP 语义状态码 */
|
||||
private Integer code;
|
||||
/** 响应消息 */
|
||||
private String msg;
|
||||
/** 响应数据 */
|
||||
private T data;
|
||||
|
||||
/**
|
||||
* 创建成功响应。
|
||||
*
|
||||
* @param data 响应数据
|
||||
* @param <T> 响应数据类型
|
||||
* @return ERP 成功响应
|
||||
*/
|
||||
public static <T> ErpResult<T> success(T data) {
|
||||
ErpResult<T> result = new ErpResult<>();
|
||||
result.setCode(200);
|
||||
result.setMsg("");
|
||||
result.setData(data);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建错误响应。
|
||||
*
|
||||
* @param code HTTP 语义状态码
|
||||
* @param message 响应消息
|
||||
* @return ERP 错误响应
|
||||
*/
|
||||
public static ErpResult<Void> error(int code, String message) {
|
||||
ErpResult<Void> result = new ErpResult<>();
|
||||
result.setCode(code);
|
||||
result.setMsg(message);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -106,8 +106,14 @@ public class SecurityConfiguration {
|
||||
.requestMatchers("/webjars/**").permitAll()
|
||||
.requestMatchers("/swagger-ui").permitAll()
|
||||
.requestMatchers("/swagger-ui/**").permitAll();
|
||||
// ERP 日计划接口使用独立令牌鉴权
|
||||
registry.requestMatchers(HttpMethod.POST, "/lms/daily-plan/erpPush").permitAll();
|
||||
// ERP 入站接口使用独立令牌鉴权
|
||||
registry.requestMatchers(HttpMethod.POST,
|
||||
"/lms/erp/raw-foil-roll/push",
|
||||
"/lms/daily-plan/erpPush",
|
||||
"/lms/erp/sub-roll/sync",
|
||||
"/lms/erp/sub-roll/quality-result",
|
||||
"/lms/erp/finished-box/inbound")
|
||||
.permitAll();
|
||||
// Spring Boot Actuator 的安全配置
|
||||
registry.requestMatchers("/actuator").permitAll()
|
||||
.requestMatchers("/actuator/**").permitAll();
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package cn.code.nl.module.lms.service.erp;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/** ERP 入站鉴权和固定上下文执行服务。 */
|
||||
public interface ErpAccessService {
|
||||
|
||||
/**
|
||||
* 校验 ERP 鉴权并在固定租户和审计操作人上下文中执行业务。
|
||||
*
|
||||
* @param authorization ERP 鉴权请求头
|
||||
* @param supplier 业务逻辑
|
||||
* @param <T> 业务返回类型
|
||||
* @return 业务结果
|
||||
*/
|
||||
<T> T execute(String authorization, Supplier<T> supplier);
|
||||
|
||||
/**
|
||||
* 获取当前 ERP 审计操作人。
|
||||
*
|
||||
* @return 当前操作人,非 ERP 执行上下文返回 null
|
||||
*/
|
||||
String getCurrentOperator();
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package cn.code.nl.module.lms.service.erp;
|
||||
|
||||
import cn.code.nl.framework.common.pojo.CommonResult;
|
||||
import cn.code.nl.framework.tenant.core.util.TenantUtils;
|
||||
import cn.code.nl.module.infra.api.config.ConfigApi;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/** ERP 入站鉴权和固定上下文执行服务实现。 */
|
||||
@Service
|
||||
@Slf4j
|
||||
public class ErpAccessServiceImpl implements ErpAccessService {
|
||||
|
||||
/** ERP 鉴权配置键 */
|
||||
private static final String AUTHORIZATION_CONFIG_KEY = "lms.erp.authorization";
|
||||
/** ERP 固定租户 */
|
||||
private static final Long ERP_TENANT_ID = 1L;
|
||||
/** ERP 固定审计操作人 */
|
||||
private static final String ERP_OPERATOR = "user4";
|
||||
/** 当前 ERP 审计操作人 */
|
||||
private static final ThreadLocal<String> OPERATOR = new ThreadLocal<>();
|
||||
|
||||
/** 配置管理接口 */
|
||||
@Resource
|
||||
private ConfigApi configApi;
|
||||
|
||||
/** 校验 ERP 鉴权并在固定上下文中执行业务。 */
|
||||
@Override
|
||||
public <T> T execute(String authorization, Supplier<T> supplier) {
|
||||
if (ObjectUtils.isEmpty(authorization)) {
|
||||
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "Authorization不能为空");
|
||||
}
|
||||
CommonResult<String> configResult = configApi.getConfigValueByKey(AUTHORIZATION_CONFIG_KEY);
|
||||
if (configResult == null || !configResult.isSuccess() || ObjectUtils.isEmpty(configResult.getData())) {
|
||||
if (configResult == null) {
|
||||
log.error("读取ERP鉴权配置失败,配置接口未返回结果");
|
||||
} else {
|
||||
log.error("读取ERP鉴权配置失败,code={},msg={}", configResult.getCode(), configResult.getMsg());
|
||||
}
|
||||
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "ERP鉴权配置缺失");
|
||||
}
|
||||
if (!MessageDigest.isEqual(authorization.getBytes(StandardCharsets.UTF_8),
|
||||
configResult.getData().getBytes(StandardCharsets.UTF_8))) {
|
||||
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "Authorization错误");
|
||||
}
|
||||
String oldOperator = OPERATOR.get();
|
||||
AtomicReference<T> result = new AtomicReference<>();
|
||||
try {
|
||||
OPERATOR.set(ERP_OPERATOR);
|
||||
TenantUtils.execute(ERP_TENANT_ID, () -> result.set(supplier.get()));
|
||||
return result.get();
|
||||
} finally {
|
||||
if (oldOperator == null) {
|
||||
OPERATOR.remove();
|
||||
} else {
|
||||
OPERATOR.set(oldOperator);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** 获取当前 ERP 审计操作人。 */
|
||||
@Override
|
||||
public String getCurrentOperator() {
|
||||
return OPERATOR.get();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package cn.code.nl.module.lms.controller.erp;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
class ErpExceptionHandlerTest {
|
||||
|
||||
private final ErpExceptionHandler exceptionHandler = new ErpExceptionHandler();
|
||||
|
||||
@Test
|
||||
void success_返回ERP成功码() {
|
||||
ErpResult<String> result = ErpResult.success("成功");
|
||||
|
||||
Assertions.assertEquals(200, result.getCode());
|
||||
Assertions.assertEquals("", result.getMsg());
|
||||
Assertions.assertEquals("成功", result.getData());
|
||||
}
|
||||
|
||||
@Test
|
||||
void handleBadRequest_HTTP状态与响应码均为400() {
|
||||
ResponseEntity<ErpResult<Void>> response = exceptionHandler.handleBadRequest(
|
||||
new BindException(new Object(), "request"));
|
||||
|
||||
assertStatus(response, 400);
|
||||
}
|
||||
|
||||
@Test
|
||||
void handleResponseStatus_保持401和403状态() {
|
||||
assertStatus(exceptionHandler.handleResponseStatus(
|
||||
new ResponseStatusException(HttpStatus.UNAUTHORIZED, "未认证")), 401);
|
||||
assertStatus(exceptionHandler.handleResponseStatus(
|
||||
new ResponseStatusException(HttpStatus.FORBIDDEN, "禁止访问")), 403);
|
||||
}
|
||||
|
||||
@Test
|
||||
void handleNotFound_HTTP状态与响应码均为404() {
|
||||
assertStatus(exceptionHandler.handleNotFound(new NoSuchElementException()), 404);
|
||||
}
|
||||
|
||||
@Test
|
||||
void handleException_HTTP状态与响应码均为500() {
|
||||
assertStatus(exceptionHandler.handleException(new IllegalStateException("测试异常")), 500);
|
||||
}
|
||||
|
||||
/** 校验 HTTP 状态和 ERP 响应码一致。 */
|
||||
private void assertStatus(ResponseEntity<ErpResult<Void>> response, int expectedStatus) {
|
||||
Assertions.assertEquals(expectedStatus, response.getStatusCode().value());
|
||||
Assertions.assertNotNull(response.getBody());
|
||||
Assertions.assertEquals(expectedStatus, response.getBody().getCode());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package cn.code.nl.module.lms.service.erp;
|
||||
|
||||
import cn.code.nl.framework.common.pojo.CommonResult;
|
||||
import cn.code.nl.framework.tenant.core.context.TenantContextHolder;
|
||||
import cn.code.nl.module.infra.api.config.ConfigApi;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
@SpringJUnitConfig
|
||||
@ContextConfiguration(classes = {
|
||||
ErpAccessServiceLocalSpringTest.TestConfiguration.class,
|
||||
ErpAccessServiceImpl.class
|
||||
})
|
||||
class ErpAccessServiceLocalSpringTest {
|
||||
|
||||
@Resource
|
||||
private ErpAccessService erpAccessService;
|
||||
@Resource
|
||||
private TestConfigApi configApi;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
configApi.setResult(CommonResult.success("user4"));
|
||||
TenantContextHolder.clear();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void cleanUp() {
|
||||
TenantContextHolder.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
void execute_正确鉴权后使用租户一和固定操作人() {
|
||||
Long tenantId = erpAccessService.execute("user4", () -> {
|
||||
Assertions.assertEquals("user4", erpAccessService.getCurrentOperator());
|
||||
return TenantContextHolder.getRequiredTenantId();
|
||||
});
|
||||
|
||||
Assertions.assertEquals(1L, tenantId);
|
||||
Assertions.assertNull(TenantContextHolder.getTenantId());
|
||||
Assertions.assertNull(erpAccessService.getCurrentOperator());
|
||||
}
|
||||
|
||||
@Test
|
||||
void execute_缺少Authorization返回未认证() {
|
||||
ResponseStatusException exception = Assertions.assertThrows(ResponseStatusException.class,
|
||||
() -> erpAccessService.execute(null, () -> "不会执行"));
|
||||
|
||||
Assertions.assertEquals(401, exception.getStatusCode().value());
|
||||
}
|
||||
|
||||
@Test
|
||||
void execute_错误Authorization返回禁止访问() {
|
||||
ResponseStatusException exception = Assertions.assertThrows(ResponseStatusException.class,
|
||||
() -> erpAccessService.execute("wrong", () -> "不会执行"));
|
||||
|
||||
Assertions.assertEquals(403, exception.getStatusCode().value());
|
||||
}
|
||||
|
||||
@Test
|
||||
void execute_配置缺失返回服务异常() {
|
||||
configApi.setResult(CommonResult.success(null));
|
||||
|
||||
ResponseStatusException exception = Assertions.assertThrows(ResponseStatusException.class,
|
||||
() -> erpAccessService.execute("user4", () -> "不会执行"));
|
||||
|
||||
Assertions.assertEquals(500, exception.getStatusCode().value());
|
||||
}
|
||||
|
||||
@Test
|
||||
void execute_业务异常后可靠清理上下文() {
|
||||
Assertions.assertThrows(IllegalStateException.class,
|
||||
() -> erpAccessService.execute("user4", () -> {
|
||||
throw new IllegalStateException("测试异常");
|
||||
}));
|
||||
|
||||
Assertions.assertNull(TenantContextHolder.getTenantId());
|
||||
Assertions.assertNull(erpAccessService.getCurrentOperator());
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
static class TestConfiguration {
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
TestConfigApi configApi() {
|
||||
return new TestConfigApi();
|
||||
}
|
||||
}
|
||||
|
||||
static class TestConfigApi implements ConfigApi {
|
||||
|
||||
private CommonResult<String> result;
|
||||
|
||||
@Override
|
||||
public CommonResult<String> getConfigValueByKey(String key) {
|
||||
return result;
|
||||
}
|
||||
|
||||
void setResult(CommonResult<String> result) {
|
||||
this.result = result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package cn.code.nl.module.wms.controller.erp;
|
||||
|
||||
import jakarta.validation.ConstraintViolationException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.MissingRequestHeaderException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
/** ERP 接口异常处理器。 */
|
||||
@Slf4j
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
@RestControllerAdvice(basePackages = "cn.code.nl.module.wms.controller.erp")
|
||||
public class ErpExceptionHandler {
|
||||
|
||||
/** 将请求参数异常转换为 400 响应。 */
|
||||
@ExceptionHandler({MethodArgumentNotValidException.class, ConstraintViolationException.class,
|
||||
HttpMessageNotReadableException.class, BindException.class})
|
||||
public ResponseEntity<ErpResult<Void>> handleBadRequest(Exception exception) {
|
||||
return build(HttpStatus.BAD_REQUEST, "请求参数错误");
|
||||
}
|
||||
|
||||
/** 将 Authorization 请求头缺失转换为 401 响应。 */
|
||||
@ExceptionHandler(MissingRequestHeaderException.class)
|
||||
public ResponseEntity<ErpResult<Void>> handleMissingAuthorization(MissingRequestHeaderException exception) {
|
||||
return build(HttpStatus.UNAUTHORIZED, "Authorization不能为空");
|
||||
}
|
||||
|
||||
/** 将带状态的 ERP 异常转换为对应响应。 */
|
||||
@ExceptionHandler(ResponseStatusException.class)
|
||||
public ResponseEntity<ErpResult<Void>> handleResponseStatus(ResponseStatusException exception) {
|
||||
int code = exception.getStatusCode().value();
|
||||
String message = exception.getReason() == null ? "请求处理失败" : exception.getReason();
|
||||
return ResponseEntity.status(code).body(ErpResult.error(code, message));
|
||||
}
|
||||
|
||||
/** 将业务对象不存在转换为 404 响应。 */
|
||||
@ExceptionHandler(NoSuchElementException.class)
|
||||
public ResponseEntity<ErpResult<Void>> handleNotFound(NoSuchElementException exception) {
|
||||
return build(HttpStatus.NOT_FOUND, "业务对象不存在");
|
||||
}
|
||||
|
||||
/** 将未识别异常转换为 500 响应。 */
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<ErpResult<Void>> handleException(Exception exception) {
|
||||
log.error("ERP接口处理发生未识别异常", exception);
|
||||
return build(HttpStatus.INTERNAL_SERVER_ERROR, "系统内部错误");
|
||||
}
|
||||
|
||||
/** 创建 HTTP 状态与响应体状态一致的错误响应。 */
|
||||
private ResponseEntity<ErpResult<Void>> build(HttpStatus status, String message) {
|
||||
return ResponseEntity.status(status).body(ErpResult.error(status.value(), message));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package cn.code.nl.module.wms.controller.erp;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/** ERP 接口统一响应。 */
|
||||
@Data
|
||||
public class ErpResult<T> {
|
||||
/** HTTP 语义状态码 */
|
||||
private Integer code;
|
||||
/** 响应消息 */
|
||||
private String msg;
|
||||
/** 响应数据 */
|
||||
private T data;
|
||||
|
||||
/** 创建成功响应。 */
|
||||
public static <T> ErpResult<T> success(T data) {
|
||||
ErpResult<T> result = new ErpResult<>();
|
||||
result.setCode(200);
|
||||
result.setMsg("");
|
||||
result.setData(data);
|
||||
return result;
|
||||
}
|
||||
|
||||
/** 创建错误响应。 */
|
||||
public static ErpResult<Void> error(int code, String message) {
|
||||
ErpResult<Void> result = new ErpResult<>();
|
||||
result.setCode(code);
|
||||
result.setMsg(message);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package cn.code.nl.module.wms.framework.security.config;
|
||||
import cn.code.nl.framework.security.config.AuthorizeRequestsCustomizer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configurers.AuthorizeHttpRequestsConfigurer;
|
||||
|
||||
@@ -104,6 +105,8 @@ public class SecurityConfiguration {
|
||||
.requestMatchers("/webjars/**").permitAll()
|
||||
.requestMatchers("/swagger-ui").permitAll()
|
||||
.requestMatchers("/swagger-ui/**").permitAll();
|
||||
// ERP 入站接口使用独立令牌鉴权
|
||||
registry.requestMatchers(HttpMethod.POST, "/wms/erp/finished-box/outbound").permitAll();
|
||||
// Spring Boot Actuator 的安全配置
|
||||
registry.requestMatchers("/actuator").permitAll()
|
||||
.requestMatchers("/actuator/**").permitAll();
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package cn.code.nl.module.wms.service.erp;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/** ERP 入站鉴权和固定上下文执行服务。 */
|
||||
public interface ErpAccessService {
|
||||
/** 校验 ERP 鉴权并在固定租户和审计操作人上下文中执行业务。 */
|
||||
<T> T execute(String authorization, Supplier<T> supplier);
|
||||
|
||||
/** 获取当前 ERP 审计操作人。 */
|
||||
String getCurrentOperator();
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package cn.code.nl.module.wms.service.erp;
|
||||
|
||||
import cn.code.nl.framework.common.pojo.CommonResult;
|
||||
import cn.code.nl.framework.tenant.core.util.TenantUtils;
|
||||
import cn.code.nl.module.infra.api.config.ConfigApi;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/** ERP 入站鉴权和固定上下文执行服务实现。 */
|
||||
@Service
|
||||
@Slf4j
|
||||
public class ErpAccessServiceImpl implements ErpAccessService {
|
||||
/** ERP 鉴权配置键 */
|
||||
private static final String AUTHORIZATION_CONFIG_KEY = "lms.erp.authorization";
|
||||
/** ERP 固定租户 */
|
||||
private static final Long ERP_TENANT_ID = 1L;
|
||||
/** ERP 固定审计操作人 */
|
||||
private static final String ERP_OPERATOR = "user4";
|
||||
/** 当前 ERP 审计操作人 */
|
||||
private static final ThreadLocal<String> OPERATOR = new ThreadLocal<>();
|
||||
|
||||
/** 配置管理接口 */
|
||||
@Resource
|
||||
private ConfigApi configApi;
|
||||
|
||||
/** 校验 ERP 鉴权并在固定上下文中执行业务。 */
|
||||
@Override
|
||||
public <T> T execute(String authorization, Supplier<T> supplier) {
|
||||
if (ObjectUtils.isEmpty(authorization)) {
|
||||
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "Authorization不能为空");
|
||||
}
|
||||
CommonResult<String> configResult = configApi.getConfigValueByKey(AUTHORIZATION_CONFIG_KEY);
|
||||
if (configResult == null || !configResult.isSuccess() || ObjectUtils.isEmpty(configResult.getData())) {
|
||||
if (configResult == null) {
|
||||
log.error("读取ERP鉴权配置失败,配置接口未返回结果");
|
||||
} else {
|
||||
log.error("读取ERP鉴权配置失败,code={},msg={}", configResult.getCode(), configResult.getMsg());
|
||||
}
|
||||
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "ERP鉴权配置缺失");
|
||||
}
|
||||
if (!MessageDigest.isEqual(authorization.getBytes(StandardCharsets.UTF_8),
|
||||
configResult.getData().getBytes(StandardCharsets.UTF_8))) {
|
||||
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "Authorization错误");
|
||||
}
|
||||
String oldOperator = OPERATOR.get();
|
||||
AtomicReference<T> result = new AtomicReference<>();
|
||||
try {
|
||||
OPERATOR.set(ERP_OPERATOR);
|
||||
TenantUtils.execute(ERP_TENANT_ID, () -> result.set(supplier.get()));
|
||||
return result.get();
|
||||
} finally {
|
||||
if (oldOperator == null) {
|
||||
OPERATOR.remove();
|
||||
} else {
|
||||
OPERATOR.set(oldOperator);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** 获取当前 ERP 审计操作人。 */
|
||||
@Override
|
||||
public String getCurrentOperator() {
|
||||
return OPERATOR.get();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user