This commit is contained in:
zz 2021-12-02 16:28:31 +08:00
parent b332702b8b
commit 31a9e30808
3 changed files with 68 additions and 62 deletions

View File

@ -70,6 +70,7 @@ public class EvcsFilter extends OncePerRequestFilter {
Scanner scanner = new Scanner(requestWrapper.getInputStream(), "UTF-8").useDelimiter("\\A"); Scanner scanner = new Scanner(requestWrapper.getInputStream(), "UTF-8").useDelimiter("\\A");
String bodyString = scanner.hasNext() ? scanner.next() : null; String bodyString = scanner.hasNext() ? scanner.next() : null;
log.debug("in.enc: {}", bodyString); log.debug("in.enc: {}", bodyString);
CommonResponse resp = new CommonResponse();
if (!ObjectUtils.isEmpty(bodyString)) { if (!ObjectUtils.isEmpty(bodyString)) {
String servletPath = request.getServletPath(); String servletPath = request.getServletPath();
log.debug("servletPath: " + servletPath); log.debug("servletPath: " + servletPath);
@ -77,22 +78,23 @@ public class EvcsFilter extends OncePerRequestFilter {
String operatorId = commonRequest.getOperatorId(); String operatorId = commonRequest.getOperatorId();
String authorization = request.getHeader("Authorization"); String authorization = request.getHeader("Authorization");
log.debug("Authorization: {}", authorization); log.debug("Authorization: {}", authorization);
AuthSecretToken authSecretTokenIn; AuthSecretToken authSecretTokenIn = null;
Date now = Calendar.getInstance().getTime(); Date now = Calendar.getInstance().getTime();
ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response); ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
if (servletPath.endsWith("query_token")) { if (servletPath.endsWith("query_token")) {
if (!handleQueryToken(request, response, chain, requestWrapper, bodyString, commonRequest, operatorId, authSecretTokenIn = authSecretTokenRepository.findByOperatorId3irdptyAndSecretTokenType(operatorId,
responseWrapper)) return; AuthSecretToken.SECRET_TOKEN_TYPE_IN).orElse(null);
} handleQueryToken(request, response, chain, requestWrapper, bodyString, commonRequest, operatorId,
responseWrapper, authSecretTokenIn);
} else {
if (authorization != null && authorization.startsWith("Bearer ")) { if (authorization != null && authorization.startsWith("Bearer ")) {
String token = authorization.replace("Bearer ", ""); String token = authorization.replace("Bearer ", "");
authSecretTokenIn = authSecretTokenIn =
authSecretTokenRepository.findByOperatorId3irdptyAndSecretTokenTypeAndTokenExpiryGreaterThan( authSecretTokenRepository.findByOperatorId3irdptyAndSecretTokenTypeAndTokenExpiryGreaterThan(
operatorId, AuthSecretToken.SECRET_TOKEN_TYPE_IN, now).orElse(null); operatorId, AuthSecretToken.SECRET_TOKEN_TYPE_IN, now).orElse(null);
if (authSecretTokenIn == null) { if (authSecretTokenIn == null) {
CommonResponse resp = new CommonResponse(); resp.setRet("4002");
resp.setRet("4003"); resp.setMsg("Invalid token(db)");
resp.setMsg("Invalid token");
String data = JSONUtil.toJSONString(resp); String data = JSONUtil.toJSONString(resp);
response.getOutputStream().write(data.getBytes(StandardCharsets.UTF_8)); response.getOutputStream().write(data.getBytes(StandardCharsets.UTF_8));
responseWrapper = new ContentCachingResponseWrapper(response); responseWrapper = new ContentCachingResponseWrapper(response);
@ -100,11 +102,17 @@ public class EvcsFilter extends OncePerRequestFilter {
return; return;
} else if (!token.equals(authSecretTokenIn.getToken())) { } else if (!token.equals(authSecretTokenIn.getToken())) {
log.error("op[{}] Invalid auth: {}", operatorId, authorization); log.error("op[{}] Invalid auth: {}", operatorId, authorization);
resp.setRet("4002"); // todo YBD...
resp.setMsg("Invalid token(inequal)");
String data = JSONUtil.toJSONString(resp);
response.getOutputStream().write(data.getBytes(StandardCharsets.UTF_8));
// responseWrapper = new ContentCachingResponseWrapper(response);
// chain.doFilter(requestWrapper, responseWrapper);
return;
} }
} else { } else {
log.error("op[{}] Invalid auth: {}", operatorId, authorization); log.error("op[{}] Invalid auth: {}", operatorId, authorization);
CommonResponse resp = new CommonResponse(); resp.setRet("4002");
resp.setRet("4003");
resp.setMsg("Authorization header is not present or invalid"); resp.setMsg("Authorization header is not present or invalid");
String data = JSONUtil.toJSONString(resp); String data = JSONUtil.toJSONString(resp);
response.getOutputStream().write(data.getBytes(StandardCharsets.UTF_8)); response.getOutputStream().write(data.getBytes(StandardCharsets.UTF_8));
@ -115,7 +123,6 @@ public class EvcsFilter extends OncePerRequestFilter {
//decrypt request //decrypt request
byte[] decryptedReq = null; byte[] decryptedReq = null;
String erroMsg = "Decryption error"; String erroMsg = "Decryption error";
CommonResponse resp = new CommonResponse();
try { try {
// if (authSecretTokenIn.isEncrypt() && !"false".equals(encin)) { // test code // if (authSecretTokenIn.isEncrypt() && !"false".equals(encin)) { // test code
decryptedReq = decrypt(request, authSecretTokenIn, commonRequest, bodyString); decryptedReq = decrypt(request, authSecretTokenIn, commonRequest, bodyString);
@ -140,6 +147,7 @@ public class EvcsFilter extends OncePerRequestFilter {
chain.doFilter(requestWrapper, responseWrapper); chain.doFilter(requestWrapper, responseWrapper);
return; return;
} }
}
//encrypt response //encrypt response
final String encout = request.getHeader("enc.out"); final String encout = request.getHeader("enc.out");
@ -156,7 +164,7 @@ public class EvcsFilter extends OncePerRequestFilter {
// AuthSecretToken authSecretTokenOut = authSecretTokenRepository.findByOperatorId3irdptyAndSecretTokenType // AuthSecretToken authSecretTokenOut = authSecretTokenRepository.findByOperatorId3irdptyAndSecretTokenType
// (operatorId, // (operatorId,
// AuthSecretToken.SECRET_TOKEN_TYPE_OUT).orElse(null); // AuthSecretToken.SECRET_TOKEN_TYPE_OUT).orElse(null);
if (encout == null && authSecretTokenIn != null) { if (encout == null) {
encryptedData = encryptRespOut(authSecretTokenIn.getDataSecret(), authSecretTokenIn.getDataSecretIV(), encryptedData = encryptRespOut(authSecretTokenIn.getDataSecret(), authSecretTokenIn.getDataSecretIV(),
authSecretTokenIn.getSigSecret(), buf).toString(); authSecretTokenIn.getSigSecret(), buf).toString();
log.debug("out.enc: {}", encryptedData); log.debug("out.enc: {}", encryptedData);
@ -164,24 +172,23 @@ public class EvcsFilter extends OncePerRequestFilter {
encryptedData.getBytes(StandardCharsets.UTF_8)); encryptedData.getBytes(StandardCharsets.UTF_8));
} else if ("false".equals(encout)) { } else if ("false".equals(encout)) {
response.getOutputStream().write(buf); response.getOutputStream().write(buf);
} else { // } else {
resp.setRet("4004"); // resp.setRet("4004");
resp.setMsg("Encryption error"); // resp.setMsg("Encryption error");
String data = JSONUtil.toJSONString(resp); // String data = JSONUtil.toJSONString(resp);
response.getOutputStream().write(data.getBytes(StandardCharsets.UTF_8)); // response.getOutputStream().write(data.getBytes(StandardCharsets.UTF_8));
// chain.doFilter(requestWrapper, responseWrapper); //// chain.doFilter(requestWrapper, responseWrapper);
} }
} }
} }
private boolean handleQueryToken(HttpServletRequest request, HttpServletResponse response, FilterChain chain, private boolean handleQueryToken(HttpServletRequest request, HttpServletResponse response, FilterChain chain,
ServletRequest requestWrapper, String bodyString, CommonRequest commonRequest, ServletRequest requestWrapper, String bodyString, CommonRequest commonRequest,
String operatorId, ContentCachingResponseWrapper responseWrapper) throws IOException, String operatorId, ContentCachingResponseWrapper responseWrapper,
AuthSecretToken authSecretToken) throws IOException,
ServletException { ServletException {
AuthSecretToken authSecretTokenIn = authSecretTokenRepository.findByOperatorId3irdptyAndSecretTokenType(operatorId, if (authSecretToken == null) {
AuthSecretToken.SECRET_TOKEN_TYPE_IN).orElse(null);
if (authSecretTokenIn == null) {
CommonResponse resp = new CommonResponse(); CommonResponse resp = new CommonResponse();
resp.setRet("4003"); resp.setRet("4003");
resp.setMsg("Invalid OperatorID"); resp.setMsg("Invalid OperatorID");
@ -194,7 +201,7 @@ public class EvcsFilter extends OncePerRequestFilter {
} else { } else {
final byte[] decrypt; final byte[] decrypt;
try { try {
decrypt = decrypt(request, authSecretTokenIn, commonRequest, bodyString); decrypt = decrypt(request, authSecretToken, commonRequest, bodyString);
} catch (BadPaddingException | InvalidAlgorithmParameterException | NoSuchAlgorithmException | IllegalBlockSizeException | NoSuchPaddingException | InvalidKeyException e) { } catch (BadPaddingException | InvalidAlgorithmParameterException | NoSuchAlgorithmException | IllegalBlockSizeException | NoSuchPaddingException | InvalidKeyException e) {
e.printStackTrace(); e.printStackTrace();
CommonResponse resp = new CommonResponse(); CommonResponse resp = new CommonResponse();

View File

@ -7,7 +7,6 @@ import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.QueryByExampleExecutor; import org.springframework.data.repository.query.QueryByExampleExecutor;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional; import java.util.Optional;
/** /**
@ -27,7 +26,7 @@ public interface XhpcTerminalRepository extends JpaRepository<XhpcTerminal, Inte
Optional<XhpcTerminal> findOneBySerialNumber(String serialNumber); Optional<XhpcTerminal> findOneBySerialNumber(String serialNumber);
@Query("select t.pileSerialNumber from XhpcTerminal as t where t.serialNumber = ?1") @Query("select t.pileSerialNumber from XhpcTerminal as t where t.delFlag = 0 and t.serialNumber = ?1")
String selectBySql(String serialNumber); String selectBySql(String serialNumber);
} }

View File

@ -57,7 +57,7 @@ public class XhpcTerminal extends BaseEntity {
private Integer status; private Integer status;
/** 删除标志0代表存在 2代表删除 */ /** 删除标志0代表存在 2代表删除 */
@Column(name = "del_falg", nullable = true) @Column(name = "del_flag", nullable = true)
private Integer delFlag; private Integer delFlag;
/** 费率模型id */ /** 费率模型id */