时间戳服务BUG修复
This commit is contained in:
@ -9,11 +9,11 @@ import com.chinaunicom.mall.ebtp.extend.timeService.tsa.SystemTime;
|
|||||||
import com.chinaunicom.mall.ebtp.extend.timeService.tsa.TtsAgent;
|
import com.chinaunicom.mall.ebtp.extend.timeService.tsa.TtsAgent;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.codec.binary.Base64;
|
import org.apache.commons.codec.binary.Base64;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.core.io.ClassPathResource;
|
import org.springframework.core.io.ClassPathResource;
|
||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Calendar;
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
@ -22,6 +22,9 @@ public class TimeServiceConstant {
|
|||||||
|
|
||||||
public static TSAClient client;
|
public static TSAClient client;
|
||||||
|
|
||||||
|
@Value("${spring.redis.sentinel.master}")
|
||||||
|
private String redis;
|
||||||
|
|
||||||
public TimeServiceConstant(){
|
public TimeServiceConstant(){
|
||||||
try{
|
try{
|
||||||
Properties prop = new Properties();
|
Properties prop = new Properties();
|
||||||
@ -44,7 +47,11 @@ public class TimeServiceConstant {
|
|||||||
* @return 时间戳加密原文
|
* @return 时间戳加密原文
|
||||||
*/
|
*/
|
||||||
public String signTimeStamp(String data){
|
public String signTimeStamp(String data){
|
||||||
|
if(redis.equals("eshop-redis-sim")){
|
||||||
|
return "";
|
||||||
|
}
|
||||||
try{
|
try{
|
||||||
|
|
||||||
data = data!=null&&!"".equals(data)?data:"system";
|
data = data!=null&&!"".equals(data)?data:"system";
|
||||||
//第三步:申请时间戳,向时间戳服务器发起申请时间戳请求,返回时间戳结果
|
//第三步:申请时间戳,向时间戳服务器发起申请时间戳请求,返回时间戳结果
|
||||||
TSASignedResult result = client.signTimeStamp("SHA1", data.getBytes("UTF-8"));
|
TSASignedResult result = client.signTimeStamp("SHA1", data.getBytes("UTF-8"));
|
||||||
@ -57,7 +64,7 @@ public class TimeServiceConstant {
|
|||||||
}catch (Exception e){
|
}catch (Exception e){
|
||||||
log.error("申请时间戳异常", e);
|
log.error("申请时间戳异常", e);
|
||||||
}
|
}
|
||||||
return null;
|
return "";
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* 解析时间戳
|
* 解析时间戳
|
||||||
@ -65,21 +72,28 @@ public class TimeServiceConstant {
|
|||||||
* @return 时间戳对象
|
* @return 时间戳对象
|
||||||
*/
|
*/
|
||||||
public TtsAgent.TtsParseResult verifyTimeStamp2(String timestamp){
|
public TtsAgent.TtsParseResult verifyTimeStamp2(String timestamp){
|
||||||
|
String systemtime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
|
||||||
|
TtsAgent.TtsParseResult ttsParseResult = new TtsAgent.TtsParseResult(systemtime,"","");
|
||||||
|
if(redis.equals("eshop-redis-sim")){
|
||||||
|
return ttsParseResult;
|
||||||
|
}
|
||||||
try{
|
try{
|
||||||
|
|
||||||
log.info("timestamp -->"+timestamp);
|
log.info("timestamp -->"+timestamp);
|
||||||
TSAVerifyResult verifyResult = this.verifyTimeStamp(timestamp);
|
TSAVerifyResult verifyResult = this.verifyTimeStamp(timestamp);
|
||||||
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(verifyResult.getSignedTime());
|
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(verifyResult.getSignedTime());
|
||||||
TtsAgent.TtsParseResult ttsParseResult = new TtsAgent.TtsParseResult(time, HelperUtil.bytesToHexString(verifyResult.getData()),
|
ttsParseResult = new TtsAgent.TtsParseResult(time, HelperUtil.bytesToHexString(verifyResult.getData()),
|
||||||
verifyResult.getSignerSubject());
|
verifyResult.getSignerSubject());
|
||||||
return ttsParseResult;
|
|
||||||
}catch (Exception e){
|
}catch (Exception e){
|
||||||
log.error("解析时间戳", e);
|
log.error("解析时间戳", e);
|
||||||
}
|
}
|
||||||
return null;
|
return ttsParseResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
private TSAVerifyResult verifyTimeStamp(String timestamp){
|
private TSAVerifyResult verifyTimeStamp(String timestamp){
|
||||||
|
|
||||||
try{
|
try{
|
||||||
|
|
||||||
log.info("timestamp -->"+timestamp);
|
log.info("timestamp -->"+timestamp);
|
||||||
TSAVerifyResult verifyResult = client.verifyTimeStamp(Base64.decodeBase64(timestamp));
|
TSAVerifyResult verifyResult = client.verifyTimeStamp(Base64.decodeBase64(timestamp));
|
||||||
return verifyResult;
|
return verifyResult;
|
||||||
@ -95,14 +109,19 @@ public class TimeServiceConstant {
|
|||||||
* @return 时间戳对象
|
* @return 时间戳对象
|
||||||
*/
|
*/
|
||||||
public TtsAgent.TtsParseResult getServiceTimeObj(String data){
|
public TtsAgent.TtsParseResult getServiceTimeObj(String data){
|
||||||
try{
|
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
|
||||||
String timestamp = this.signTimeStamp(data);
|
TtsAgent.TtsParseResult ttsParseResult = new TtsAgent.TtsParseResult(time,"","");
|
||||||
TtsAgent.TtsParseResult ttsParseResult = this.verifyTimeStamp2(timestamp);
|
if(redis.equals("eshop-redis-sim")){
|
||||||
return ttsParseResult;
|
return ttsParseResult;
|
||||||
|
}
|
||||||
|
try{
|
||||||
|
|
||||||
|
String timestamp = this.signTimeStamp(data);
|
||||||
|
ttsParseResult = this.verifyTimeStamp2(timestamp);
|
||||||
}catch (Exception e){
|
}catch (Exception e){
|
||||||
log.error("获取时间戳异常", e);
|
log.error("获取时间戳异常", e);
|
||||||
}
|
}
|
||||||
return null;
|
return ttsParseResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -111,16 +130,19 @@ public class TimeServiceConstant {
|
|||||||
* @return 时间
|
* @return 时间
|
||||||
*/
|
*/
|
||||||
public String getServiceTime(String data){
|
public String getServiceTime(String data){
|
||||||
|
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
|
||||||
|
if(redis.equals("eshop-redis-sim")){
|
||||||
|
return time;
|
||||||
|
}
|
||||||
try{
|
try{
|
||||||
|
|
||||||
String timestamp = this.signTimeStamp(data);
|
String timestamp = this.signTimeStamp(data);
|
||||||
TSAVerifyResult verifyResult = this.verifyTimeStamp(timestamp);
|
TSAVerifyResult verifyResult = this.verifyTimeStamp(timestamp);
|
||||||
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(verifyResult.getSignedTime());
|
time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(verifyResult.getSignedTime());
|
||||||
|
|
||||||
return time;
|
|
||||||
}catch (Exception e){
|
}catch (Exception e){
|
||||||
log.error("获取时间戳异常", e);
|
log.error("获取时间戳异常", e);
|
||||||
}
|
}
|
||||||
return null;
|
return time;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -130,7 +152,11 @@ public class TimeServiceConstant {
|
|||||||
* @return date时间
|
* @return date时间
|
||||||
*/
|
*/
|
||||||
public Date getServiceDate(String data){
|
public Date getServiceDate(String data){
|
||||||
|
if(redis.equals("eshop-redis-sim")){
|
||||||
|
return new Date();
|
||||||
|
}
|
||||||
try{
|
try{
|
||||||
|
|
||||||
String timestamp = this.signTimeStamp(data);
|
String timestamp = this.signTimeStamp(data);
|
||||||
TSAVerifyResult verifyResult = this.verifyTimeStamp(timestamp);
|
TSAVerifyResult verifyResult = this.verifyTimeStamp(timestamp);
|
||||||
Date date = verifyResult.getSignedTime();
|
Date date = verifyResult.getSignedTime();
|
||||||
@ -138,7 +164,7 @@ public class TimeServiceConstant {
|
|||||||
}catch (Exception e){
|
}catch (Exception e){
|
||||||
log.error("获取时间戳异常", e);
|
log.error("获取时间戳异常", e);
|
||||||
}
|
}
|
||||||
return null;
|
return new Date();
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* 获取年月日时分秒分割存储对象
|
* 获取年月日时分秒分割存储对象
|
||||||
@ -146,11 +172,9 @@ public class TimeServiceConstant {
|
|||||||
* @return SystemTime
|
* @return SystemTime
|
||||||
*/
|
*/
|
||||||
public SystemTime getServiceSystemTime(String data){
|
public SystemTime getServiceSystemTime(String data){
|
||||||
try{
|
Date date = new Date();
|
||||||
String timestamp = this.signTimeStamp(data);
|
if(redis.equals("eshop-redis-sim")){
|
||||||
TSAVerifyResult verifyResult = this.verifyTimeStamp(timestamp);
|
return new SystemTime()
|
||||||
Date date = verifyResult.getSignedTime();
|
|
||||||
SystemTime systemTime = new SystemTime()
|
|
||||||
.setYear(date.getYear()+1900)
|
.setYear(date.getYear()+1900)
|
||||||
.setMonth(date.getMonth()+1)
|
.setMonth(date.getMonth()+1)
|
||||||
.setDate(date.getDate())
|
.setDate(date.getDate())
|
||||||
@ -158,20 +182,40 @@ public class TimeServiceConstant {
|
|||||||
.setMinute(date.getMinutes())
|
.setMinute(date.getMinutes())
|
||||||
.setSecond(date.getSeconds())
|
.setSecond(date.getSeconds())
|
||||||
.setTimestamp(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
|
.setTimestamp(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
|
||||||
return systemTime;
|
}
|
||||||
|
try{
|
||||||
|
|
||||||
|
String timestamp = this.signTimeStamp(data);
|
||||||
|
TSAVerifyResult verifyResult = this.verifyTimeStamp(timestamp);
|
||||||
|
date = verifyResult.getSignedTime();
|
||||||
|
|
||||||
}catch (Exception e){
|
}catch (Exception e){
|
||||||
log.error("获取时间戳异常", e);
|
log.error("获取时间戳异常", e);
|
||||||
}
|
}
|
||||||
return null;
|
SystemTime systemTime = new SystemTime()
|
||||||
|
.setYear(date.getYear()+1900)
|
||||||
|
.setMonth(date.getMonth()+1)
|
||||||
|
.setDate(date.getDate())
|
||||||
|
.setHour(date.getHours())
|
||||||
|
.setMinute(date.getMinutes())
|
||||||
|
.setSecond(date.getSeconds())
|
||||||
|
.setTimestamp(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
|
||||||
|
return systemTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String arge[]){
|
public static void main(String arge[]){
|
||||||
Date date = new Date();
|
try{
|
||||||
System.out.println("-----------"+(date.getYear()+1900));
|
|
||||||
System.out.println("-----------"+(date.getMonth()+1));
|
Date date = new Date();
|
||||||
System.out.println("-----------"+date.getDate());
|
System.out.println("-----------"+(date.getYear()+1900));
|
||||||
System.out.println("-----------"+date.getHours());
|
System.out.println("-----------"+(date.getMonth()+1));
|
||||||
System.out.println("-----------"+date.getMinutes());
|
System.out.println("-----------"+date.getDate());
|
||||||
System.out.println("-----------"+date.getSeconds());
|
System.out.println("-----------"+date.getHours());
|
||||||
|
System.out.println("-----------"+date.getMinutes());
|
||||||
|
System.out.println("-----------"+date.getSeconds());
|
||||||
|
|
||||||
|
}catch (Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user