发表于: 2019-12-05 22:28:34

1 1188


今天做了什么:

前台-登录:

解密前台传输的encryptedData:

@RequestMapping(value = "/decodeUserInfo",method = RequestMethod.POST)
   public JSONObject decodeUserInfo(String encryptedData,String iv,String code){
       JSONObject object = new JSONObject();
       if (code ==null || code.length()==0){
           object.put("code","0");
           object.put("message","code为空");
           return object;
       }
       String Appid ="xxx";
       String AppSecret="xxxx";
       String grant_type="authorization_code";
       String params = "appid=" + Appid + "&secret=" + AppSecret + "&js_code=" + code + "&grant_type=" + grant_type;
       //发送请求
       String string = HttpRequest.sendGet("https://api.weixin.qq.com/sns/jscode2session", params);
       //解析相应内容(转换成json对象)
       JSONObject json = JSONObject.parseObject(string);
       //获取会话密钥(session_key)
       String session_key = json.get("session_key").toString();
       //用户的唯一标识(openid)
       String openid = (String) json.get("openid");
       try {
           String result = AesCbcUtil.decrypt(encryptedData, session_key, iv, "UTF-8");
           if (null != result && result.length() > 0) {
               object.put("code", 1);
               object.put("message", "解密成功");
               JSONObject userInfoJSON = JSONObject.parseObject(result);

               JSONObject userInfo = new JSONObject();
               userInfo.put("openId", userInfoJSON.get("openId"));
               userInfo.put("nickName", userInfoJSON.get("nickName"));
               userInfo.put("gender", userInfoJSON.get("gender"));
               userInfo.put("city", userInfoJSON.get("city"));
               userInfo.put("province", userInfoJSON.get("province"));
               userInfo.put("country", userInfoJSON.get("country"));
               userInfo.put("avatarUrl", userInfoJSON.get("avatarUrl"));
               userInfo.put("unionId", userInfoJSON.get("unionId"));
               object.put("userInfo", userInfo);
               return object;
           }
       } catch (Exception e) {
           e.printStackTrace();
       }
       object.put("code", 0);
       object.put("message", "解密失败");
       return object;
   }

用到的Http请求类:

 
public class HttpRequest {
   /**
    * 向指定URL发送GET方法的请求
    *
    * @param url
    *            发送请求的URL
    * @param param
    *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
    * @return URL 所代表远程资源的响应结果
    */
   public static String sendGet(String url, String param) {
       String result = "";
       BufferedReader in = null;
       try {
           String urlNameString = url + "?" + param;
           URL realUrl = new URL(urlNameString);
           // 打开和URL之间的连接
           URLConnection connection = realUrl.openConnection();
           // 设置通用的请求属性
           connection.setRequestProperty("accept", "*/*");
           connection.setRequestProperty("connection", "Keep-Alive");
           connection.setRequestProperty("user-agent",
                   "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
           // 建立实际的连接
           connection.connect();
           // 获取所有响应头字段
           Map<String, List<String>> map = connection.getHeaderFields();
           // 遍历所有的响应头字段
           for (String key : map.keySet()) {
               System.out.println(key + "--->" + map.get(key));
           }
           // 定义 BufferedReader输入流来读取URL的响应
           in = new BufferedReader(new InputStreamReader(
                   connection.getInputStream()));
           String line;
           while ((line = in.readLine()) != null) {
               result += line;
           }
       } catch (Exception e) {
           System.out.println("发送GET请求出现异常!" + e);
           e.printStackTrace();
       }
       // 使用finally块来关闭输入流
       finally {
           try {
               if (in != null) {
                   in.close();
               }
           } catch (Exception e2) {
               e2.printStackTrace();
           }
       }
       return result;
   }

   /**
    * 向指定 URL 发送POST方法的请求
    *
    * @param url
    *            发送请求的 URL
    * @param param
    *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
    * @return 所代表远程资源的响应结果
    */
   public static String sendPost(String url, String param) {
       PrintWriter out = null;
       BufferedReader in = null;
       String result = "";
       try {
           URL realUrl = new URL(url);
           // 打开和URL之间的连接
           URLConnection conn = realUrl.openConnection();
           // 设置通用的请求属性
           conn.setRequestProperty("accept", "*/*");
           conn.setRequestProperty("connection", "Keep-Alive");
           conn.setRequestProperty("user-agent",
                   "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
           // 发送POST请求必须设置如下两行
           conn.setDoOutput(true);
           conn.setDoInput(true);
           // 获取URLConnection对象对应的输出流
           out = new PrintWriter(conn.getOutputStream());
           // 发送请求参数
           out.print(param);
           // flush输出流的缓冲
           out.flush();
           // 定义BufferedReader输入流来读取URL的响应
           in = new BufferedReader(
                   new InputStreamReader(conn.getInputStream()));
           String line;
           while ((line = in.readLine()) != null) {
               result += line;
           }
       } catch (Exception e) {
           System.out.println("发送 POST 请求出现异常!"+e);
           e.printStackTrace();
       }
       //使用finally块来关闭输出流、输入流
       finally{
           try{
               if(out!=null){
                   out.close();
               }
               if(in!=null){
                   in.close();
               }
           }
           catch(IOException ex){
               ex.printStackTrace();
           }
       }
       return result;
   }
}

加密工具类:

public class AesCbcUtil {
   static {
       //BouncyCastle是一个开源的加解密解决方案,主页在http://www.bouncycastle.org/
       Security.addProvider(new BouncyCastleProvider());
   }

   /**
    * AES解密
    *
    * @param data           //密文,被加密的数据
    * @param key            //秘钥
    * @param iv             //偏移量
    * @param encodingFormat //解密后的结果需要进行的编码
    * @return
    * @throws Exception
    */
   public static String decrypt(String data, String key, String iv, String encodingFormat) throws Exception {
//        initialize();

       //被加密的数据
       byte[] dataByte = Base64.decodeBase64(data);
       //加密秘钥
       byte[] keyByte = Base64.decodeBase64(key);
       //偏移量
       byte[] ivByte = Base64.decodeBase64(iv);


       try {
           Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");

           SecretKeySpec spec = new SecretKeySpec(keyByte, "AES");

           AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES");
           parameters.init(new IvParameterSpec(ivByte));

           cipher.init(Cipher.DECRYPT_MODE, spec, parameters);// 初始化

           byte[] resultByte = cipher.doFinal(dataByte);
           if (null != resultByte && resultByte.length > 0) {
               String result = new String(resultByte, encodingFormat);
               return result;
           }
           return null;
       } catch (NoSuchAlgorithmException e) {
           e.printStackTrace();
       } catch (NoSuchPaddingException e) {
           e.printStackTrace();
       } catch (InvalidParameterSpecException e) {
           e.printStackTrace();
       } catch (InvalidKeyException e) {
           e.printStackTrace();
       } catch (InvalidAlgorithmParameterException e) {
           e.printStackTrace();
       } catch (IllegalBlockSizeException e) {
           e.printStackTrace();
       } catch (BadPaddingException e) {
           e.printStackTrace();
       } catch (UnsupportedEncodingException e) {
           e.printStackTrace();
       }

       return null;
   }
}

收获:

spring cloud gateway:

<!--gateway与spring-boot-starter-web冲突-->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
server:
 port: 21041

spring:
 application:
   name: spring-cloud-gateway
#配置路由
 cloud:
   gateway:
     routes:
       - id: video_route
         uri: http://localhost:21040/video
         predicates:
           - Path=/video/**
         #id:路由名称
         #uri:被转发路径
         #path:拦截规则

问题:

  1. 小程序前端调用第三方接口需要htpps.  在解决如何给域名添加ssl的问题

明天的计划

完成前台登录 ,

前台学生证


返回列表 返回列表
评论

    分享到