1.添加订单明细认证接口和订单明细认证接口

This commit is contained in:
wanglf3 2024-02-28 19:56:16 +08:00
parent 3dca884467
commit 6b33590553
11 changed files with 254 additions and 18 deletions

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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<OrderSearchResult> 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("签名错误");
}
}
}

View File

@ -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;
}

View File

@ -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);
}

View File

@ -1,8 +1,12 @@
package com.chint.domain.service.auth; 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.UserLoginParam;
import com.chint.domain.value_object.UserLoginResult; import com.chint.domain.value_object.UserLoginResult;
public interface AuthenticateService { public interface AuthenticateService {
UserLoginResult authenticateEmployeeNo(UserLoginParam userLoginParam); UserLoginResult authenticateEmployeeNo(UserLoginParam userLoginParam);
OrderSearchResult authenticateClient(AuthenticationDto authenticationDto);
} }

View File

@ -1,7 +1,11 @@
package com.chint.domain.service.auth; 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.aggregates.user.User;
import com.chint.domain.factoriy.user.UserFactory; import com.chint.domain.factoriy.user.UserFactory;
import com.chint.domain.repository.ClientRepository;
import com.chint.domain.repository.UserRepository; import com.chint.domain.repository.UserRepository;
import com.chint.domain.value_object.UserLoginParam; import com.chint.domain.value_object.UserLoginParam;
import com.chint.domain.value_object.UserLoginResult; import com.chint.domain.value_object.UserLoginResult;
@ -23,6 +27,9 @@ public class AuthenticateServiceImpl implements AuthenticateService {
private final UserRepository userRepository; private final UserRepository userRepository;
private final UserHttpRequest httpRequest; private final UserHttpRequest httpRequest;
@Autowired
private ClientRepository clientRepository;
@Autowired @Autowired
private UserFactory userFactory; private UserFactory userFactory;
@ -74,4 +81,29 @@ public class AuthenticateServiceImpl implements AuthenticateService {
return authenticateEmployeeNo(userLoginParam); return authenticateEmployeeNo(userLoginParam);
} }
} }
@Override
public OrderSearchResult authenticateClient(AuthenticationDto authenticationDto) {
Client client = clientRepository.findByClientId(authenticationDto.getClientid());
if (client != null){
Map<String, Object> 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;
}
} }

View File

@ -35,4 +35,12 @@ public class AuthMessageConstant {
public static final long EXPIRATION_TIME_MS = 3600000L; // 1小时过期时间 public static final long EXPIRATION_TIME_MS = 3600000L; // 1小时过期时间
public static final String HEADER_TOKEN = "token"; public static final String HEADER_TOKEN = "token";
public static final String HEADER_TOKEN_UP_CASE = "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小时过期时间
} }

View File

@ -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);
}
}

View File

@ -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,Long> {
Client findByClientId(String clientId);
}

View File

@ -16,29 +16,42 @@ public class JwtTokenAdminInterceptor implements HandlerInterceptor {
@Override @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { 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 { try {
JWTUtil.verifyJWT(AuthMessageConstant.SECRET, token); String token = request.getHeader(AuthMessageConstant.HEADER_TOKEN);
var parseJWT = JWTUtil.parseJWT(AuthMessageConstant.SECRET, token); if (token == null) {
var withJwt = User.withJwt(parseJWT); token = request.getHeader(AuthMessageConstant.HEADER_TOKEN_UP_CASE);
BaseContext.setCurrentUser(withJwt); if (token != null){
return true; // If verification succeeds, continue processing the request 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) { } catch (TokenExpiredException e) {
throw new JwtExpiredException(AuthMessageConstant.JWT_EXPIRED); throw new JwtExpiredException(AuthMessageConstant.JWT_EXPIRED);
} catch (Exception e) { } catch (Exception e) {
throw new AuthException(AuthMessageConstant.JWT_INVALID); 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
} }
} }