BaseController.java 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package com.fdkankan.manage.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.auth0.jwt.JWT;
  4. import com.auth0.jwt.interfaces.DecodedJWT;
  5. import com.fdkankan.manage.common.ResultData;
  6. import com.fdkankan.manage.util.RsaUtils;
  7. import com.fdkankan.manage.util.RtkUtils;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.stereotype.Component;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12. import java.util.Date;
  13. @Component
  14. public class BaseController {
  15. @Autowired
  16. protected HttpServletRequest request;
  17. @Autowired
  18. protected HttpServletResponse response;
  19. protected String getToken(){
  20. return request.getHeader("token");
  21. }
  22. protected String getSign(){
  23. return request.getHeader("sign");
  24. }
  25. protected String getRtkSign(){
  26. return request.getHeader("rtk-sign");
  27. }
  28. protected Long getUserId(){
  29. String token = request.getHeader("token");
  30. DecodedJWT jwt = JWT.decode(token);
  31. return jwt.getClaim("userId").asLong();
  32. }
  33. public Boolean checkSign(){
  34. try {
  35. String sign = getSign();
  36. Long time = new Date().getTime();
  37. Long signLong = Long.valueOf(sign);
  38. if(time -signLong >1000 * 10){
  39. return false;
  40. }
  41. }catch (Exception e){
  42. return false;
  43. }
  44. return true;
  45. }
  46. public JSONObject checkRtkSign(){
  47. try {
  48. String sign = getRtkSign();
  49. //{"time":1739007343379,"rtkSnCode":"2113213123","account":"111"}
  50. String decipher = RsaUtils.decipher(sign, RtkUtils.privateKey);
  51. JSONObject jsonObject = JSONObject.parseObject(decipher);
  52. if(!jsonObject.containsKey("time") || !jsonObject.containsKey("rtkSnCode") || !jsonObject.containsKey("account")){
  53. return null;
  54. }
  55. if(jsonObject.get("time") == null || jsonObject.get("rtkSnCode") == null || jsonObject.get("account") == null){
  56. return null;
  57. }
  58. Long time = jsonObject.getLong("time");
  59. if((new Date().getTime() - time) > 1000 * 10){
  60. return null;
  61. }
  62. return jsonObject;
  63. }catch (Exception e){
  64. return null;
  65. }
  66. }
  67. public static void main(String[] args) {
  68. JSONObject jsonObject = new JSONObject();
  69. jsonObject.put("account","111");
  70. jsonObject.put("time",new Date().getTime());
  71. jsonObject.put("rtkSnCode","2113213123");
  72. System.out.println(jsonObject.toJSONString());
  73. String decipher = RsaUtils.encipher(jsonObject.toJSONString(), RtkUtils.publicKey);
  74. System.out.println(decipher);
  75. String d = RsaUtils.decipher("hX2Gdrhtx21w5AixvCQu3Iq8vQsVGQ5s5Krr1VRYdZw4bXRhxKKxuZHPCmAjroVi5HUvg9jNcAuGwv9kO1EJKwqP6+/zsR2ixRSV8AEv4fMfO+HkEprRiSeHn0WMfS7qtCnLiSnx1WLsb7tXzR4qSEDE7UWQweuyZCq4X1oSBnP7cpqSzWJgs2zbKZPwM5TbvbsTtkR9ddxyAVoUMl9slIooebUmtbgMSvlj/Eh04vIyJ9BigZasyIDGQhW/PREN/mRsLLkuA3jS/FXAjSL9RfB4mlWLowvEYcMmmrz3LGGBCeZmzIwagRGGtt0XdYx9vIsOSIW4t9MNlQkxug6Quw==", RtkUtils.privateKey);
  76. System.out.println(d);
  77. }
  78. }