parent
8b6804b888
commit
9c6db53e13
24 changed files with 464 additions and 57 deletions
@ -0,0 +1,25 @@ |
||||
package com.daqing.financial.hrauth.model; |
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName; |
||||
import io.swagger.annotations.ApiModelProperty; |
||||
import lombok.Data; |
||||
|
||||
import java.io.Serializable; |
||||
|
||||
/** |
||||
* <p> |
||||
* 访问记录表 |
||||
* </p> |
||||
* |
||||
* @author Qyq |
||||
* @since 2021-01-12 |
||||
*/ |
||||
@Data |
||||
public class XccLoginReq implements Serializable { |
||||
|
||||
private String encryptedData;//明文,加密数据
|
||||
private String iv;//加密算法的初始向量
|
||||
private String code;//用户允许登录后,回调内容会带上 code(有效期五分钟),开发者需要将 code 发送到开发者服务器后台
|
||||
// ,使用code 换取 session_key api,将 code 换成 openid 和 session_key
|
||||
|
||||
} |
@ -0,0 +1,92 @@ |
||||
package com.daqing.financial.hrauth.util; |
||||
|
||||
import org.apache.commons.codec.binary.Base64; |
||||
import org.bouncycastle.jce.provider.BouncyCastleProvider; |
||||
|
||||
import javax.crypto.BadPaddingException; |
||||
import javax.crypto.Cipher; |
||||
import javax.crypto.IllegalBlockSizeException; |
||||
import javax.crypto.NoSuchPaddingException; |
||||
import javax.crypto.spec.IvParameterSpec; |
||||
import javax.crypto.spec.SecretKeySpec; |
||||
import java.io.UnsupportedEncodingException; |
||||
import java.security.*; |
||||
import java.security.spec.InvalidParameterSpecException; |
||||
|
||||
/** |
||||
* Created by yfs on 2018/3/25. |
||||
* <p> |
||||
* AES-128-CBC 加密方式 |
||||
* 注: |
||||
* AES-128-CBC可以自己定义“密钥”和“偏移量“。 |
||||
* AES-128是jdk自动生成的“密钥”。 |
||||
*/ |
||||
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"); |
||||
//Cipher cipher = Cipher.getInstance("AES/ECB/ZeroBytePadding");
|
||||
|
||||
SecretKeySpec spec = new SecretKeySpec(keyByte, "AES"); |
||||
|
||||
AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES"); |
||||
parameters.init(new IvParameterSpec(ivByte)); |
||||
|
||||
cipher.init(Cipher.DECRYPT_MODE, spec, parameters);// 初始化
|
||||
//cipher.init(Cipher.ENCRYPT_MODE,key,new IvParameterSpec( java.util.Base64.Decoder.decode(IV.getBytes("UTF-8"))));
|
||||
|
||||
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; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,137 @@ |
||||
package com.daqing.financial.hrauth.util; |
||||
|
||||
import java.io.BufferedReader; |
||||
import java.io.IOException; |
||||
import java.io.InputStreamReader; |
||||
import java.io.PrintWriter; |
||||
import java.net.URL; |
||||
import java.net.URLConnection; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
public class HttpRequest { |
||||
|
||||
public static void main(String[] args) { |
||||
//发送 GET 请求
|
||||
String s=HttpRequest.sendGet("http://v.qq.com/x/cover/kvehb7okfxqstmc.html?vid=e01957zem6o", ""); |
||||
System.out.println(s); |
||||
|
||||
// //发送 POST 请求
|
||||
// String sr=HttpRequest.sendPost("http://www.toutiao.com/stream/widget/local_weather/data/?city=%E4%B8%8A%E6%B5%B7", "");
|
||||
// JSONObject json = JSONObject.fromObject(sr);
|
||||
// System.out.println(json.get("data"));
|
||||
} |
||||
|
||||
/** |
||||
* 向指定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; |
||||
} |
||||
} |
@ -1,13 +1,34 @@ |
||||
//package com.daqing.financial.hrauth;
|
||||
//
|
||||
//import org.junit.Test;
|
||||
//import org.springframework.boot.test.context.SpringBootTest;
|
||||
//
|
||||
//@SpringBootTest
|
||||
//class DqFinancialHrmsAuthApplicationTests {
|
||||
//
|
||||
// @Test
|
||||
// void contextLoads() {
|
||||
// }
|
||||
//
|
||||
//}
|
||||
package com.daqing.financial.hrauth; |
||||
|
||||
import com.daqing.financial.hrauth.util.AesCbcUtil; |
||||
import org.junit.Test; |
||||
import org.springframework.boot.test.context.SpringBootTest; |
||||
|
||||
/*@SpringBootTest |
||||
class DqFinancialHrmsAuthApplicationTests { |
||||
|
||||
@Test |
||||
void contextLoads() throws Exception { |
||||
|
||||
String encryptedData="m7CF698f222KiEse8aFLIwSiXjxojbFesVpHltvrKPqKH3OclT7Qc+UhOw+MbxRAOSKgMb+erVYXYcrPwQ6TSF4VBt9Lzv0GdxJf82876SgANFURjVZBXKSwp0snxw/9wdfTGV2t7nIbpbUfBFD0QEi4rrsD8hZdPr4s/WLtHcpcvRkJvb3KDRw+PsnTyBtFOnXnv5sOJW84uy9OGuTTV8s0miqZCy//xZU2DJ3EZsg3Rp9E5xe0jMLtLOfvQEf5tM/pPw9hCkWbbBJEBSUn5IkSgkt3jAasjoWOSuDzw+MAJvnT7byJ7bvOSbEY14RAZzX6sZYw8Rd/n9fOssZivfF+F2HIfHhWuBNcGhNYTIYXtJsvmhtT5Wo4mMnq0j/TElZOoAjc385xkluHNRB/T+PVo+FEkRZgiugAOzOPn24WjZU7nbPrtoEYH5Bcdfcf3quP4hQAIQd6EvkfdnRti+Mfn925llWSS+fa+AV3hx0mwtFkk1VFMBuLPjL2nvvvUl4NkoMVzw0NRGPpeiS+DA=="; |
||||
String iv="jh3Io5uYeMSbU903QXpewA=="; |
||||
String session_key="LKv54UbAoCTx4yz5mi6S1A=="; |
||||
|
||||
|
||||
String result = AesCbcUtil.decrypt(encryptedData, session_key, iv, "UTF-8"); |
||||
System.out.println(result); |
||||
} |
||||
|
||||
}*/ |
||||
|
||||
class DqFinancialHrmsAuthApplicationTests{ |
||||
public static void main(String[] args) throws Exception { |
||||
String encryptedData="m7CF698f222KiEse8aFLIwSiXjxojbFesVpHltvrKPqKH3OclT7Qc+UhOw+MbxRAOSKgMb+erVYXYcrPwQ6TSF4VBt9Lzv0GdxJf82876SgANFURjVZBXKSwp0snxw/9wdfTGV2t7nIbpbUfBFD0QEi4rrsD8hZdPr4s/WLtHcpcvRkJvb3KDRw+PsnTyBtFOnXnv5sOJW84uy9OGuTTV8s0miqZCy//xZU2DJ3EZsg3Rp9E5xe0jMLtLOfvQEf5tM/pPw9hCkWbbBJEBSUn5IkSgkt3jAasjoWOSuDzw+MAJvnT7byJ7bvOSbEY14RAZzX6sZYw8Rd/n9fOssZivfF+F2HIfHhWuBNcGhNYTIYXtJsvmhtT5Wo4mMnq0j/TElZOoAjc385xkluHNRB/T+PVo+FEkRZgiugAOzOPn24WjZU7nbPrtoEYH5Bcdfcf3quP4hQAIQd6EvkfdnRti+Mfn925llWSS+fa+AV3hx0mwtFkk1VFMBuLPjL2nvvvUl4NkoMVzw0NRGPpeiS+DA=="; |
||||
String iv="jh3Io5uYeMSbU903QXpewA=="; |
||||
String session_key="LKv54UbAoCTx4yz5mi6S1A=="; |
||||
|
||||
|
||||
String result = AesCbcUtil.decrypt(encryptedData, session_key, iv, "UTF-8"); |
||||
System.out.println(result); |
||||
} |
||||
} |
Loading…
Reference in new issue