項目在windows中請求接口正常,換到linux服務器上就提示錯誤,跟斷點看了下原來是token驗證被擋住了,兩者時間相差8小時。懷疑是linux時間有問題,使用date查看服務器時間顯示:
確實是本地時間。
當然還有linux服務器本身時間不是CST,而是UTC格式的,可百度搜索:修改區時UTC改為CST
后來經過排查和百度發現是docker在搗蛋,這里直接貼出解決方案
1、在項目中新增時間轉化類

public class TimeUtil { public static DateTime GetCstDateTime() { Instant now = SystemClock.Instance.GetCurrentInstant(); var shanghaiZone = DateTimeZoneProviders.Tzdb["Asia/Shanghai"]; return now.InZone(shanghaiZone).ToDateTimeUnspecified(); } } public static class DateTimeExtentions { public static DateTime ToCstTime(this DateTime time) { return TimeUtil.GetCstDateTime(); } }
使用:DateTime.Now.ToCstTime();
傳送門:https://www.cnblogs.com/1175429393wljblog/p/11692610.html
ps:此種方式沒有實踐
2、在docker中做文章,將linux服務器時間拷貝到docker容器中
a、在dockerfile中添加時間拷貝語句 【此方法是我選擇的,一勞永逸】
#時區設置 RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ && echo ‘Asia/Shanghai’ >/etc/timezone
還有b和c方法,沒有測試可在博主原文中查看試用
傳送門:https://www.skyfinder.cc/2018/12/14/dockerandhostdatetimenotsame