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