init project

This commit is contained in:
loujf
2022-06-27 14:45:37 +08:00
parent 165c805b58
commit 77e6873082
1105 changed files with 122904 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
package com.auto_deploy;
import org.apache.commons.codec.binary.Base64;
import org.nl.config.RsaProperties;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import javax.crypto.Cipher;
public class App {
public static void main(String[] args) throws NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException, InvalidKeySpecException, InvalidKeyException {
PKCS8EncodedKeySpec pkcs8EncodedKeySpec5 = new PKCS8EncodedKeySpec(Base64.decodeBase64(RsaProperties.privateKey));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec5);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(2, privateKey);
byte[] result = cipher.doFinal(Base64.decodeBase64("$2a$10$Egp1/gvFlt7zhlXVfEFw4OfWQCGPw0ClmMcc6FjTnvXNRVf9zdMRa"));
System.out.println(new String(result));
}
}

View File

@@ -0,0 +1,136 @@
package com.auto_deploy;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import org.apache.commons.codec.binary.Base64;
import org.nl.utils.RsaUtils;
public class Rutil {
private static final String SRC = "123456";
public Rutil() {
}
public static void main(String[] args) throws Exception {
System.out.println("\n");
RsaUtils.RsaKeyPair keyPair = generateKeyPair();
System.out.println("公钥:" + keyPair.getPublicKey());
System.out.println("私钥:" + keyPair.getPrivateKey());
System.out.println("\n");
test1(keyPair);
System.out.println("\n");
test2(keyPair);
System.out.println("\n");
}
private static void test1(RsaUtils.RsaKeyPair keyPair) throws Exception {
System.out.println("***************** 公钥加密私钥解密开始 *****************");
String text1 = encryptByPublicKey(keyPair.getPublicKey(), "123456");
String text2 = decryptByPrivateKey(keyPair.getPrivateKey(), text1);
System.out.println("加密前123456");
System.out.println("加密后:" + text1);
System.out.println("解密后:" + text2);
if ("123456".equals(text2)) {
System.out.println("解密字符串和原始字符串一致,解密成功");
} else {
System.out.println("解密字符串和原始字符串不一致,解密失败");
}
System.out.println("***************** 公钥加密私钥解密结束 *****************");
}
private static void test2(RsaUtils.RsaKeyPair keyPair) throws Exception {
System.out.println("***************** 私钥加密公钥解密开始 *****************");
String text1 = encryptByPrivateKey(keyPair.getPrivateKey(), "123456");
String text2 = decryptByPublicKey(keyPair.getPublicKey(), text1);
System.out.println("加密前123456");
System.out.println("加密后:" + text1);
System.out.println("解密后:" + text2);
if ("123456".equals(text2)) {
System.out.println("解密字符串和原始字符串一致,解密成功");
} else {
System.out.println("解密字符串和原始字符串不一致,解密失败");
}
System.out.println("***************** 私钥加密公钥解密结束 *****************");
}
public static String decryptByPublicKey(String publicKeyText, String text) throws Exception {
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyText));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(2, publicKey);
byte[] result = cipher.doFinal(Base64.decodeBase64(text));
return new String(result);
}
public static String encryptByPrivateKey(String privateKeyText, String text) throws Exception {
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyText));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(1, privateKey);
byte[] result = cipher.doFinal(text.getBytes());
return Base64.encodeBase64String(result);
}
public static String decryptByPrivateKey(String privateKeyText, String text) throws Exception {
PKCS8EncodedKeySpec pkcs8EncodedKeySpec5 = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyText));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec5);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(2, privateKey);
byte[] result = cipher.doFinal(Base64.decodeBase64(text));
return new String(result);
}
public static String encryptByPublicKey(String publicKeyText, String text) throws Exception {
X509EncodedKeySpec x509EncodedKeySpec2 = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyText));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec2);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(1, publicKey);
byte[] result = cipher.doFinal(text.getBytes());
return Base64.encodeBase64String(result);
}
public static RsaUtils.RsaKeyPair generateKeyPair() throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(1024);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
String publicKeyString = Base64.encodeBase64String(rsaPublicKey.getEncoded());
String privateKeyString = Base64.encodeBase64String(rsaPrivateKey.getEncoded());
return new RsaUtils.RsaKeyPair(publicKeyString, privateKeyString);
}
public static class RsaKeyPair {
private final String publicKey;
private final String privateKey;
public RsaKeyPair(String publicKey, String privateKey) {
this.publicKey = publicKey;
this.privateKey = privateKey;
}
public String getPublicKey() {
return this.publicKey;
}
public String getPrivateKey() {
return this.privateKey;
}
}
}

View File

@@ -0,0 +1,23 @@
package com.java8;
// 定义一个公式接口
interface Formula {
// 计算
double calculate(int a);
// 求平方根
default double sqrt(int a) {
return Math.sqrt(a);
}
public static void main(String[] args) {
Formula formula = new Formula() {
@Override
public double calculate(int a) {
return sqrt(a * 100);
}
};
System.out.println(formula.calculate(100)); // 100.0
System.out.println(formula.sqrt(16));
}
}

View File

@@ -0,0 +1,26 @@
package com.java8;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Test2 {
public static void main(String[] args) {
List<String> names = Arrays.asList("peter", "anna", "mike", "xenia");
Collections.sort(names, new Comparator<String>() {
@Override
public int compare(String a, String b) {
return b.compareTo(a);
}
});
Collections.sort(names, (String a, String b) -> {
return b.compareTo(a);
});
Collections.sort(names, (String a, String b) -> b.compareTo(a));
names.sort((a, b) -> b.compareTo(a));
}
}

View File

@@ -0,0 +1,51 @@
package com.java8;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
public class Test3 {
public static void main(String args[]) {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
// Predicate<Integer> predicate = n -> true
// n 是一个参数传递到 Predicate 接口的 test 方法
// n 如果存在则 test 方法返回 true
System.out.println("输出所有数据:");
// 传递参数 n
eval(list, n -> true);
// Predicate<Integer> predicate1 = n -> n%2 == 0
// n 是一个参数传递到 Predicate 接口的 test 方法
// 如果 n%2 为 0 test 方法返回 true
System.out.println("输出所有偶数:");
eval(list, n -> n % 2 == 0);
// Predicate<Integer> predicate2 = n -> n > 3
// n 是一个参数传递到 Predicate 接口的 test 方法
// 如果 n 大于 3 test 方法返回 true
System.out.println("输出大于 3 的所有数字:");
eval(list, n -> n > 3);
}
public static void eval2(List<Integer> list, Predicate<Integer> predicate) {
for (Integer n : list) {
if (predicate.test(n)) {
System.out.println(n + " ");
}
}
}
public static void eval(List<Integer> list, Predicate<Integer> predicate) {
for (Integer n : list) {
if (predicate.test(n)) {
System.out.println(n + " ");
}
}
}
}

View File

@@ -0,0 +1,37 @@
package com.java8;
import java.util.function.Consumer;
public class Test4 {
public static void main(String[] args) {
//原始写法
Consumer<Integer> consumer = new Consumer<Integer>() {
@Override
public void accept(Integer i) {
System.out.println("买物品需:" + i + "");
}
};
buySomething(999, consumer);
//匿名实现类的匿名对象写法
buySomething(555, new Consumer<Integer>() {
@Override
public void accept(Integer integer) {
System.out.println("买球鞋需:" + integer + "");
}
});
Consumer<String> consumer1 = (s) -> System.out.println(s);
consumer1.accept(String.valueOf(111));
//Lambda表达式写法
buySomething(666, i -> System.out.println("买乳清蛋白粉需:" + i + ""));
}
public static void buySomething(int money, Consumer<Integer> consumer) {
consumer.accept(money);
}
}

View File

@@ -0,0 +1,14 @@
package com.java8;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
public class Test5 {
public static void main(String[] args) {
Predicate<Integer> predicate = t -> t > 0;
boolean test = predicate.test(1);
System.out.println(test);
}
}

View File

@@ -0,0 +1,26 @@
package com.java8;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class Test6 {
public static void main(String[] args) {
List<String> stringCollection = new ArrayList<>();
stringCollection.add("ddd2");
stringCollection.add("aaa2");
stringCollection.add("bbb1");
stringCollection.add("aaa1");
stringCollection.add("bbb3");
stringCollection.add("ccc");
stringCollection.add("bbb2");
stringCollection.add("ddd1");
boolean anyStartsWithA =
stringCollection
.stream()
.anyMatch((s) -> s.startsWith("a"));
System.out.println(anyStartsWithA); // true
}
}

View File

@@ -0,0 +1,40 @@
package com.test;
import org.springframework.cglib.beans.BeanGenerator;
import org.springframework.cglib.beans.BeanMap;
import java.util.Map;
public class DynamicBean {
private Object target;
private BeanMap beanMap;
public DynamicBean(Class superclass, Map<String, Class> propertyMap) {
this.target = generateBean(superclass, propertyMap);
this.beanMap = BeanMap.create(this.target);
}
public void setValue(String property, Object value) {
beanMap.put(property, value);
}
public Object getValue(String property) {
return beanMap.get(property);
}
public Object getTarget() {
return this.target;
}
/**
* 根据属性生成对象
*/
private Object generateBean(Class superclass, Map<String, Class> propertyMap) {
BeanGenerator generator = new BeanGenerator();
if (null != superclass) {
generator.setSuperclass(superclass);
}
BeanGenerator.addProperties(generator, propertyMap);
return generator.create();
}
}

View File

@@ -0,0 +1,91 @@
package com.test;
import com.alibaba.fastjson.JSON;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
//参考链接:https://blog.csdn.net/weixin_43714266/article/details/105428702
//https://blog.csdn.net/weixin_37475858/article/details/80522922
public class Person {
private int id = 1;
private String name = "张三";
public Person(int id) {
this.id = id;
}
public Person(int id, String name) {
this.id = id;
this.name = name;
}
public static void main(String[] args) {
User user = new User();
user.setName("Daisy");
System.out.println(user);
System.out.println("User" + JSON.toJSONString(user));
Map<String, Object> propertiesMap = new HashMap<String, Object>();
propertiesMap.put("age", 18);
User obj = (User) ReflectUtil.getObject(user, propertiesMap);
System.out.println(obj);
System.out.println("动态为User添加age之后User" + JSON.toJSONString(obj));
}
public static <T> void setV(T entity, String setName, String setValue, String format2) {
Class<T> clazz = (Class<T>) entity.getClass();
//获取所有的bean中所有的成员变量
Field[] fields = clazz.getDeclaredFields();
try {
for (int j = 0; j < fields.length; j++) { // 遍历所有属性
String name = fields[j].getName(); // 获取属性的名字
if (name.equals(setName)) {
name = name.substring(0, 1).toUpperCase() + name.substring(1); // 将属性的首字符大写方便构造getset方法
String type = fields[j].getGenericType().toString(); // 获取属性的类型
if (type.equals("class java.lang.String")) { // 如果type是类类型则前面包含"class ",后面跟类名
Method m = entity.getClass().getMethod("set" + name, String.class);
m.invoke(entity, setValue);
}
if (type.equals("class java.util.Date")) {
Method m = entity.getClass().getMethod("set" + name, Date.class);
//Date date = DateUtil.parse(setValue,format);
// m.invoke(entity, date);
}
if (type.equals("class java.lang.Integer")) {
Method m = entity.getClass().getMethod("set" + name, Integer.class);
m.invoke(entity, Integer.valueOf(setValue));
}
if (type.equals("class java.lang.Double")) {
Method m = entity.getClass().getMethod("set" + name, Double.class);
m.invoke(entity, Double.valueOf(setValue));
}
if (type.equals("class java.lang.Long")) {
Method m = entity.getClass().getMethod("set" + name, Long.class);
m.invoke(entity, Long.valueOf(setValue));
}
if (type.equals("class java.lang.Boolean")) {
Method m = entity.getClass().getMethod("set" + name, Boolean.class);
m.invoke(entity, Boolean.valueOf(setValue));
}
}
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,54 @@
package com.test;
import com.google.common.collect.Maps;
import org.apache.commons.beanutils.PropertyUtilsBean;
import java.beans.PropertyDescriptor;
import java.util.Date;
import java.util.Map;
public class ReflectUtil {
public static Object getObject(Object dest, Map<String, Object> addProperties) {
PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(dest);
Map<String, Class> propertyMap = Maps.newHashMap();
for (PropertyDescriptor d : descriptors) {
if (!"class".equalsIgnoreCase(d.getName())) {
// propertyMap.put(d.getName(), d.getPropertyType());
}
}
addProperties.forEach((k, v) -> {
String sclass = v.getClass().toString();
if (sclass.equals("class java.util.Date")) {//对日期进行处理
propertyMap.put(k, Long.class);
} else {
propertyMap.put(k, v.getClass());
}
});
DynamicBean dynamicBean = new DynamicBean(dest.getClass(), propertyMap);
propertyMap.forEach((k, v) -> {
try {
if (!addProperties.containsKey(k)) {
dynamicBean.setValue(k, propertyUtilsBean.getNestedProperty(dest, k));
}
} catch (Exception e) {
}
});
addProperties.forEach((k, v) -> {
try {
String sclass = v.getClass().toString();
if (sclass.equals("class java.util.Date")) {//动态添加的字段为date类型需要进行处理
Date date = (Date) v;
dynamicBean.setValue(k, date.getTime());
} else {
dynamicBean.setValue(k, v);
}
} catch (Exception e) {
}
});
Object obj = dynamicBean.getTarget();
return obj;
}
}

View File

@@ -0,0 +1,14 @@
package com.test;
public class User {
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@@ -0,0 +1,45 @@
package com.test;
import cn.hutool.core.collection.CollUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import java.util.List;
import java.util.Set;
public class uTest {
public static void main(String[] args) {
JSONArray arr = new JSONArray();
JSONObject o1 = new JSONObject();
o1.put("id", "123");
o1.put("name", "456");
JSONObject o2 = new JSONObject();
o1.put("id", "555");
o1.put("name", "6666");
arr.add(o1);
arr.add(o2);
List list = toList(arr, "id");
}
public static List toList(JSONArray jsonArray, String needProperties) {
String[] col = new String[]{"a", "b", "c", "d", "e"};
final Set<String> propertSet = CollUtil.newHashSet(needProperties);
//List<BaseDto> result = new ArrayList<>();
for (Object obj : jsonArray) {
JSONObject json = (JSONObject) obj;
for (String field : propertSet) {
if (json.has(field)) {
//BaseDto dto = new BaseDto();
//通过CGLIB返回代理对象
// BaseDto object = (BaseDto) ReflectUtil.getObject(dto, json);
//result.add(object);
}
}
}
// return result;
return null;
}
}