12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- package com.fdkankan.manage.controller;
- import com.alibaba.fastjson.JSONObject;
- import com.auth0.jwt.JWT;
- import com.auth0.jwt.interfaces.DecodedJWT;
- import com.fdkankan.manage.common.ResultData;
- import com.fdkankan.manage.util.RsaUtils;
- import com.fdkankan.manage.util.RtkUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.util.Date;
- @Component
- public class BaseController {
- @Autowired
- protected HttpServletRequest request;
- @Autowired
- protected HttpServletResponse response;
- protected String getToken(){
- return request.getHeader("token");
- }
- protected String getSign(){
- return request.getHeader("sign");
- }
- protected String getRtkSign(){
- return request.getHeader("rtk-sign");
- }
- protected Long getUserId(){
- String token = request.getHeader("token");
- DecodedJWT jwt = JWT.decode(token);
- return jwt.getClaim("userId").asLong();
- }
- public Boolean checkSign(){
- try {
- String sign = getSign();
- Long time = new Date().getTime();
- Long signLong = Long.valueOf(sign);
- if(time -signLong >1000 * 10){
- return false;
- }
- }catch (Exception e){
- return false;
- }
- return true;
- }
- public JSONObject checkRtkSign(){
- try {
- String sign = getRtkSign();
- //{"time":1739007343379,"rtkSnCode":"2113213123","account":"111"}
- String decipher = RsaUtils.decipher(sign, RtkUtils.privateKey);
- JSONObject jsonObject = JSONObject.parseObject(decipher);
- if(!jsonObject.containsKey("time") || !jsonObject.containsKey("rtkSnCode") || !jsonObject.containsKey("account")){
- return null;
- }
- if(jsonObject.get("time") == null || jsonObject.get("rtkSnCode") == null || jsonObject.get("account") == null){
- return null;
- }
- Long time = jsonObject.getLong("time");
- if((new Date().getTime() - time) > 1000 * 10){
- return null;
- }
- return jsonObject;
- }catch (Exception e){
- return null;
- }
- }
- public static void main(String[] args) {
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("account","111");
- jsonObject.put("time",new Date().getTime());
- jsonObject.put("rtkSnCode","2113213123");
- System.out.println(jsonObject.toJSONString());
- String decipher = RsaUtils.encipher(jsonObject.toJSONString(), RtkUtils.publicKey);
- System.out.println(decipher);
- String d = RsaUtils.decipher("hX2Gdrhtx21w5AixvCQu3Iq8vQsVGQ5s5Krr1VRYdZw4bXRhxKKxuZHPCmAjroVi5HUvg9jNcAuGwv9kO1EJKwqP6+/zsR2ixRSV8AEv4fMfO+HkEprRiSeHn0WMfS7qtCnLiSnx1WLsb7tXzR4qSEDE7UWQweuyZCq4X1oSBnP7cpqSzWJgs2zbKZPwM5TbvbsTtkR9ddxyAVoUMl9slIooebUmtbgMSvlj/Eh04vIyJ9BigZasyIDGQhW/PREN/mRsLLkuA3jS/FXAjSL9RfB4mlWLowvEYcMmmrz3LGGBCeZmzIwagRGGtt0XdYx9vIsOSIW4t9MNlQkxug6Quw==", RtkUtils.privateKey);
- System.out.println(d);
- }
- }
|