From 6b33590553a3548e775cf7f135b1a15413a755a5 Mon Sep 17 00:00:00 2001 From: wanglf3 Date: Wed, 28 Feb 2024 19:56:16 +0800 Subject: [PATCH] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0=E8=AE=A2=E5=8D=95=E6=98=8E?= =?UTF-8?q?=E7=BB=86=E8=AE=A4=E8=AF=81=E6=8E=A5=E5=8F=A3=E5=92=8C=E8=AE=A2?= =?UTF-8?q?=E5=8D=95=E6=98=8E=E7=BB=86=E8=AE=A4=E8=AF=81=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/dtos/AuthenticationDto.java | 12 +++ .../application/dtos/OrderSearchResult.java | 10 +++ .../out/OrderDetailController.java | 74 +++++++++++++++++++ .../chint/domain/aggregates/order/Client.java | 18 +++++ .../domain/repository/ClientRepository.java | 16 ++++ .../service/auth/AuthenticateService.java | 4 + .../service/auth/AuthenticateServiceImpl.java | 32 ++++++++ .../constant/AuthMessageConstant.java | 8 ++ .../repository/ClientRepositoryImpl.java | 32 ++++++++ .../repository/jdbc/JdbcClientRepository.java | 17 +++++ .../webconfig/JwtTokenAdminInterceptor.java | 49 +++++++----- 11 files changed, 254 insertions(+), 18 deletions(-) create mode 100644 src/main/java/com/chint/application/dtos/AuthenticationDto.java create mode 100644 src/main/java/com/chint/application/dtos/OrderSearchResult.java create mode 100644 src/main/java/com/chint/application/out/OrderDetailController.java create mode 100644 src/main/java/com/chint/domain/aggregates/order/Client.java create mode 100644 src/main/java/com/chint/domain/repository/ClientRepository.java create mode 100644 src/main/java/com/chint/infrastructure/repository/ClientRepositoryImpl.java create mode 100644 src/main/java/com/chint/infrastructure/repository/jdbc/JdbcClientRepository.java diff --git a/src/main/java/com/chint/application/dtos/AuthenticationDto.java b/src/main/java/com/chint/application/dtos/AuthenticationDto.java new file mode 100644 index 00000000..4eecdeaf --- /dev/null +++ b/src/main/java/com/chint/application/dtos/AuthenticationDto.java @@ -0,0 +1,12 @@ +package com.chint.application.dtos; + + +import com.chint.domain.value_object.BaseQuery; +import com.chint.domain.value_object.LegData; +import lombok.Data; + +@Data +public class AuthenticationDto extends BaseQuery { + private String clientid ; + private String secretkey; +} diff --git a/src/main/java/com/chint/application/dtos/OrderSearchResult.java b/src/main/java/com/chint/application/dtos/OrderSearchResult.java new file mode 100644 index 00000000..b65d8c3b --- /dev/null +++ b/src/main/java/com/chint/application/dtos/OrderSearchResult.java @@ -0,0 +1,10 @@ +package com.chint.application.dtos; + + +import com.chint.infrastructure.util.Token; +import lombok.Data; + +@Data +public class OrderSearchResult { + private Token token; +} diff --git a/src/main/java/com/chint/application/out/OrderDetailController.java b/src/main/java/com/chint/application/out/OrderDetailController.java new file mode 100644 index 00000000..dd1a5b10 --- /dev/null +++ b/src/main/java/com/chint/application/out/OrderDetailController.java @@ -0,0 +1,74 @@ +package com.chint.application.out; + +import cn.hutool.core.bean.BeanUtil; +import com.chint.application.dtos.AuthenticationDto; +import com.chint.application.dtos.AuthenticationSignDto; +import com.chint.application.dtos.LocationParam; +import com.chint.application.dtos.OrderSearchResult; +import com.chint.application.dtos.response.LocationRes; +import com.chint.domain.aggregates.order.Location; +import com.chint.domain.repository.ClientRepository; +import com.chint.domain.repository.LocationRepository; +import com.chint.domain.service.LocationDomainService; +import com.chint.domain.service.auth.AuthenticateService; +import com.chint.infrastructure.util.Digest; +import com.chint.infrastructure.util.PageResult; +import com.chint.infrastructure.util.Result; +import com.chint.infrastructure.util.StringCheck; +import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Calendar; +import java.util.List; + +import static com.chint.infrastructure.constant.CommonMessageConstant.SUCCESS; + +@RestController +@RequestMapping("/OrderDetail") +public class OrderDetailController { + +// @Autowired +// private LocationRepository locationRepository; +// +// @Autowired +// private LocationDomainService locationDomainService; + + @Autowired + private AuthenticateService authenticateService; + + @Autowired + private ClientRepository clientRepository; + + @ApiOperation("订单明细认证接口") + @PostMapping("/pubilc/authentication") + public Result queryAuthentication(@RequestBody AuthenticationDto authenticationDto) { +// Calendar calendar = Calendar.getInstance(); +// long timestamp = calendar.getTimeInMillis(); + OrderSearchResult orderSearchResult = authenticateService.authenticateClient(authenticationDto); + return Result.Success(SUCCESS, orderSearchResult); + } + + @ApiOperation("订单明细查询接口") + @PostMapping("/query") + public Result query(@RequestBody AuthenticationSignDto authenticationDto) { + + String systemType = authenticationDto.getSystemType(); + String startTime = authenticationDto.getStartTime(); + String endTime = authenticationDto.getEndTime(); + String pageSize = authenticationDto.getEndTime().toString(); + String pageNum = authenticationDto.getPageNum().toString(); + String orgsign = authenticationDto.getSign(); + String sign = Digest.md5(systemType + startTime + endTime + pageSize + pageNum); + System.out.println(sign); + + if (orgsign.equals(sign)) { + return Result.Success(SUCCESS,sign); + } else { + return Result.error("签名错误"); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/chint/domain/aggregates/order/Client.java b/src/main/java/com/chint/domain/aggregates/order/Client.java new file mode 100644 index 00000000..36918efc --- /dev/null +++ b/src/main/java/com/chint/domain/aggregates/order/Client.java @@ -0,0 +1,18 @@ +package com.chint.domain.aggregates.order; + +import lombok.Data; +import org.springframework.data.relational.core.mapping.Table; + +/** + * @author wanglf3 + * @date 2024-02-28 + * @vsrsion 1.0 + **/ + +@Data +@Table("client") +public class Client { + private Long id; + private String clientId; + private String clientSecret; +} diff --git a/src/main/java/com/chint/domain/repository/ClientRepository.java b/src/main/java/com/chint/domain/repository/ClientRepository.java new file mode 100644 index 00000000..c8c2fb35 --- /dev/null +++ b/src/main/java/com/chint/domain/repository/ClientRepository.java @@ -0,0 +1,16 @@ +package com.chint.domain.repository; + +import com.chint.application.dtos.AuthenticationDto; +import com.chint.domain.aggregates.order.Client; + +/** + * @author wanglf3 + * @date 2024-02-28 + * @vsrsion 1.0 + **/ + +public interface ClientRepository { + Client save(Client client); + + Client findByClientId(String clientId); +} diff --git a/src/main/java/com/chint/domain/service/auth/AuthenticateService.java b/src/main/java/com/chint/domain/service/auth/AuthenticateService.java index e1597e14..e0089bc1 100644 --- a/src/main/java/com/chint/domain/service/auth/AuthenticateService.java +++ b/src/main/java/com/chint/domain/service/auth/AuthenticateService.java @@ -1,8 +1,12 @@ package com.chint.domain.service.auth; +import com.chint.application.dtos.AuthenticationDto; +import com.chint.application.dtos.OrderSearchResult; import com.chint.domain.value_object.UserLoginParam; import com.chint.domain.value_object.UserLoginResult; public interface AuthenticateService { UserLoginResult authenticateEmployeeNo(UserLoginParam userLoginParam); + + OrderSearchResult authenticateClient(AuthenticationDto authenticationDto); } diff --git a/src/main/java/com/chint/domain/service/auth/AuthenticateServiceImpl.java b/src/main/java/com/chint/domain/service/auth/AuthenticateServiceImpl.java index 89da197c..13ca6b71 100644 --- a/src/main/java/com/chint/domain/service/auth/AuthenticateServiceImpl.java +++ b/src/main/java/com/chint/domain/service/auth/AuthenticateServiceImpl.java @@ -1,7 +1,11 @@ package com.chint.domain.service.auth; +import com.chint.application.dtos.AuthenticationDto; +import com.chint.application.dtos.OrderSearchResult; +import com.chint.domain.aggregates.order.Client; import com.chint.domain.aggregates.user.User; import com.chint.domain.factoriy.user.UserFactory; +import com.chint.domain.repository.ClientRepository; import com.chint.domain.repository.UserRepository; import com.chint.domain.value_object.UserLoginParam; import com.chint.domain.value_object.UserLoginResult; @@ -23,6 +27,9 @@ public class AuthenticateServiceImpl implements AuthenticateService { private final UserRepository userRepository; private final UserHttpRequest httpRequest; + @Autowired + private ClientRepository clientRepository; + @Autowired private UserFactory userFactory; @@ -74,4 +81,29 @@ public class AuthenticateServiceImpl implements AuthenticateService { return authenticateEmployeeNo(userLoginParam); } } + + @Override + public OrderSearchResult authenticateClient(AuthenticationDto authenticationDto) { + + Client client = clientRepository.findByClientId(authenticationDto.getClientid()); + + if (client != null){ + Map claims = new HashMap<>(); + claims.put(AuthMessageConstant.CLIENT_ID, client.getClientId()); + claims.put(AuthMessageConstant.CLIENT_SECRET,client.getClientSecret()); + + String jwt = JWTUtil.createJWT(AuthMessageConstant.SECRET,AuthMessageConstant.EXPIRATION_CLIENT_TIME_MS, + AuthMessageConstant.SUBJECT,claims); + + + OrderSearchResult result = new OrderSearchResult(); + result.setToken(Token.of(jwt)); + return result; + + }else { + + } + + return null; + } } \ No newline at end of file diff --git a/src/main/java/com/chint/infrastructure/constant/AuthMessageConstant.java b/src/main/java/com/chint/infrastructure/constant/AuthMessageConstant.java index dc5e0a50..05af1979 100644 --- a/src/main/java/com/chint/infrastructure/constant/AuthMessageConstant.java +++ b/src/main/java/com/chint/infrastructure/constant/AuthMessageConstant.java @@ -35,4 +35,12 @@ public class AuthMessageConstant { public static final long EXPIRATION_TIME_MS = 3600000L; // 1小时过期时间 public static final String HEADER_TOKEN = "token"; public static final String HEADER_TOKEN_UP_CASE = "Token"; + + public static final String CLIENT_ID = "有效的 CLIENT_id "; + + public static final String CLIENT_SECRET = "有效的 CLIENT_SECRET"; + + public static final String HEADER_CLIENT_CASE = "Client"; + public static final long EXPIRATION_CLIENT_TIME_MS = 10800000L; // 3小时过期时间 + } diff --git a/src/main/java/com/chint/infrastructure/repository/ClientRepositoryImpl.java b/src/main/java/com/chint/infrastructure/repository/ClientRepositoryImpl.java new file mode 100644 index 00000000..58537a60 --- /dev/null +++ b/src/main/java/com/chint/infrastructure/repository/ClientRepositoryImpl.java @@ -0,0 +1,32 @@ +package com.chint.infrastructure.repository; + +import com.chint.application.dtos.AuthenticationDto; +import com.chint.domain.aggregates.order.Client; +import com.chint.domain.repository.ClientRepository; +import com.chint.infrastructure.repository.jdbc.JdbcClientRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Repository; + +/** + * @author wanglf3 + * @date 2024-02-28 + * @vsrsion 1.0 + **/ + +@Repository +public class ClientRepositoryImpl implements ClientRepository { + + @Autowired + private JdbcClientRepository jdbcClientRepository; + @Override + public Client save(Client client) { + return jdbcClientRepository.save(client); + } + + @Override + public Client findByClientId(String clientId){ + return jdbcClientRepository.findByClientId(clientId); + } + + +} diff --git a/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcClientRepository.java b/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcClientRepository.java new file mode 100644 index 00000000..05362686 --- /dev/null +++ b/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcClientRepository.java @@ -0,0 +1,17 @@ +package com.chint.infrastructure.repository.jdbc; + +import com.chint.domain.aggregates.order.Client; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +/** + * @author wanglf3 + * @date 2024-02-28 + * @vsrsion 1.0 + **/ + +@Repository +public interface JdbcClientRepository extends CrudRepository { + + Client findByClientId(String clientId); +} diff --git a/src/main/java/com/chint/infrastructure/webconfig/JwtTokenAdminInterceptor.java b/src/main/java/com/chint/infrastructure/webconfig/JwtTokenAdminInterceptor.java index b2ce91aa..05f281ed 100644 --- a/src/main/java/com/chint/infrastructure/webconfig/JwtTokenAdminInterceptor.java +++ b/src/main/java/com/chint/infrastructure/webconfig/JwtTokenAdminInterceptor.java @@ -16,29 +16,42 @@ public class JwtTokenAdminInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { - String token = request.getHeader(AuthMessageConstant.HEADER_TOKEN); - - if (token == null) { - token = request.getHeader(AuthMessageConstant.HEADER_TOKEN_UP_CASE); - } - if (request.getRequestURI().contains("/pubilc")) { - return true; - } - - if (token == null) { - throw new AuthException(AuthMessageConstant.JWT_REQUIRED); - } - try { - JWTUtil.verifyJWT(AuthMessageConstant.SECRET, token); - var parseJWT = JWTUtil.parseJWT(AuthMessageConstant.SECRET, token); - var withJwt = User.withJwt(parseJWT); - BaseContext.setCurrentUser(withJwt); - return true; // If verification succeeds, continue processing the request + String token = request.getHeader(AuthMessageConstant.HEADER_TOKEN); + if (token == null) { + token = request.getHeader(AuthMessageConstant.HEADER_TOKEN_UP_CASE); + if (token != null){ + return dealWithTokenInfo(token); + } + } + if (token == null){ + token = request.getHeader(AuthMessageConstant.HEADER_CLIENT_CASE); + if (token != null){ + return dealWithClientInfo(token); + } + } + if (request.getRequestURI().contains("/pubilc")) { + return true; + } + if (token == null) { + throw new AuthException(AuthMessageConstant.JWT_REQUIRED); + } } catch (TokenExpiredException e) { throw new JwtExpiredException(AuthMessageConstant.JWT_EXPIRED); } catch (Exception e) { throw new AuthException(AuthMessageConstant.JWT_INVALID); } + return false; + } + private boolean dealWithTokenInfo(String token){ + JWTUtil.verifyJWT(AuthMessageConstant.SECRET, token); + var parseJWT = JWTUtil.parseJWT(AuthMessageConstant.SECRET, token); + var withJwt = User.withJwt(parseJWT); + BaseContext.setCurrentUser(withJwt); + return true; // If verification succeeds, continue processing the request + } + private boolean dealWithClientInfo(String token){ + JWTUtil.verifyJWT(AuthMessageConstant.SECRET, token); + return true; // If verification succeeds, continue processing the request } } \ No newline at end of file