Compare commits
8 Commits
feature/ne
...
feature/se
Author | SHA1 | Date | |
---|---|---|---|
53f31602d2 | |||
1c4b0631f9 | |||
5ba888a9f2 | |||
1976f7d828 | |||
cc10f11b37 | |||
ce62bdcdc6 | |||
94d34faffd | |||
eba31a93f3 |
13
pom.xml
13
pom.xml
@@ -1,7 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>xyz.zhouxy.plusone</groupId>
|
||||
@@ -10,6 +9,7 @@
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>8</java.version>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<jackson.version>2.13.5</jackson.version>
|
||||
@@ -17,6 +17,7 @@
|
||||
<guava.version>32.0.1-jre</guava.version>
|
||||
<google-jsr305.version>3.0.2</google-jsr305.version>
|
||||
<joda-time.version>2.12.5</joda-time.version>
|
||||
<jbcrypt.version>0.4</jbcrypt.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
@@ -51,6 +52,14 @@
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mindrot</groupId>
|
||||
<artifactId>jbcrypt</artifactId>
|
||||
<version>${jbcrypt.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- test use only -->
|
||||
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
|
@@ -5,7 +5,15 @@ import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
// TODO 添加 Javadoc
|
||||
/**
|
||||
* ReaderMethod
|
||||
*
|
||||
* <p>
|
||||
* 标识方法是读方法,如 getter。
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @since 1.0
|
||||
*/
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface ReaderMethod {
|
||||
|
@@ -5,7 +5,15 @@ import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
// TODO 添加 Javadoc
|
||||
/**
|
||||
* WriterMethod
|
||||
*
|
||||
* <p>
|
||||
* 标识方法是写方法,如 setter。
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @since 1.0
|
||||
*/
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface WriterMethod {
|
||||
|
@@ -22,9 +22,21 @@ import java.util.function.Function;
|
||||
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.base.JRE;
|
||||
import xyz.zhouxy.plusone.commons.util.ConcurrentHashMapUtil;
|
||||
|
||||
// TODO 添加文档注释
|
||||
/**
|
||||
* SafeConcurrentHashMap
|
||||
*
|
||||
* <p>
|
||||
* Java 8 的 {@link ConcurrentHashMap#computeIfAbsent(Object, Function)} 方法有 bug,
|
||||
* 使用 Java 8 时,可使用这个类进行替换。
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @since 1.0
|
||||
* @see ConcurrentHashMap
|
||||
* @see ConcurrentHashMapUtil#computeIfAbsentForJava8(ConcurrentHashMap, Object, Function)
|
||||
*/
|
||||
@ThreadSafe
|
||||
public class SafeConcurrentHashMap<K, V> extends ConcurrentHashMap<K, V> {
|
||||
|
||||
@@ -102,6 +114,8 @@ public class SafeConcurrentHashMap<K, V> extends ConcurrentHashMap<K, V> {
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
|
||||
return ConcurrentHashMapUtil.computeIfAbsentForJava8(this, key, mappingFunction);
|
||||
return JRE.isJava8()
|
||||
? ConcurrentHashMapUtil.computeIfAbsentForJava8(this, key, mappingFunction)
|
||||
: super.computeIfAbsent(key, mappingFunction);
|
||||
}
|
||||
}
|
||||
|
@@ -31,7 +31,8 @@ import xyz.zhouxy.plusone.commons.util.RegexUtil;
|
||||
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public abstract class ValidatableStringRecord {
|
||||
public abstract class ValidatableStringRecord
|
||||
implements Comparable<ValidatableStringRecord> {
|
||||
private final String value;
|
||||
|
||||
protected ValidatableStringRecord(String value, Pattern pattern) {
|
||||
@@ -51,6 +52,11 @@ public abstract class ValidatableStringRecord {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(ValidatableStringRecord o) {
|
||||
return this.value.compareTo(o.value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.value();
|
||||
|
@@ -27,7 +27,7 @@ import java.util.Objects;
|
||||
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
||||
*/
|
||||
public abstract class BaseException
|
||||
extends RuntimeException
|
||||
extends Exception
|
||||
implements IWithCode<String> {
|
||||
|
||||
private static final long serialVersionUID = -2546365325001947203L;
|
||||
|
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright 2022-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package xyz.zhouxy.plusone.commons.exception;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.base.IWithCode;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 带错误码的异常。
|
||||
*
|
||||
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
||||
*/
|
||||
public abstract class BaseRuntimeException
|
||||
extends RuntimeException
|
||||
implements IWithCode<String> {
|
||||
|
||||
private static final long serialVersionUID = -6345888403567792664L;
|
||||
|
||||
@Nonnull
|
||||
private final String code;
|
||||
|
||||
protected BaseRuntimeException(String code, String msg) {
|
||||
super(msg);
|
||||
this.code = Objects.requireNonNull(code);
|
||||
}
|
||||
|
||||
protected BaseRuntimeException(String code, Throwable cause) {
|
||||
super(cause);
|
||||
this.code = Objects.requireNonNull(code);
|
||||
}
|
||||
|
||||
protected BaseRuntimeException(String code, String msg, Throwable cause) {
|
||||
super(msg, cause);
|
||||
this.code = Objects.requireNonNull(code);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public final String getCode() {
|
||||
return this.code;
|
||||
}
|
||||
}
|
@@ -0,0 +1,60 @@
|
||||
package xyz.zhouxy.plusone.commons.security;
|
||||
|
||||
import com.google.common.hash.Hasher;
|
||||
import com.google.common.hash.Hashing;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 使用摘要算法推荐使用 guava 的 hash 包。
|
||||
* </p>
|
||||
* <p>
|
||||
* 密码加密推荐使用 {@link org.mindrot.jbcrypt.BCrypt}。
|
||||
* </p>
|
||||
*/
|
||||
public class Hashers {
|
||||
|
||||
/**
|
||||
* 返回 guava 的 {@link Hasher}
|
||||
*/
|
||||
public static Hasher sha256() {
|
||||
return Hashing.sha256().newHasher();
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回 guava 的 {@link Hasher}
|
||||
*/
|
||||
public static Hasher sha384() {
|
||||
return Hashing.sha384().newHasher();
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回 guava 的 {@link Hasher}
|
||||
*/
|
||||
public static Hasher sha512() {
|
||||
return Hashing.sha512().newHasher();
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回 guava 的 {@link Hasher}
|
||||
*
|
||||
* @deprecated 该算法已弃用
|
||||
*/
|
||||
@Deprecated
|
||||
public static Hasher sha1() {
|
||||
return Hashing.sha1().newHasher();
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回 guava 的 {@link Hasher}
|
||||
*
|
||||
* @deprecated 该算法已弃用
|
||||
*/
|
||||
@Deprecated
|
||||
public static Hasher md5() {
|
||||
return Hashing.md5().newHasher();
|
||||
}
|
||||
|
||||
private Hashers() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
}
|
93
src/main/java/xyz/zhouxy/plusone/commons/security/RSA.java
Normal file
93
src/main/java/xyz/zhouxy/plusone/commons/security/RSA.java
Normal file
@@ -0,0 +1,93 @@
|
||||
package xyz.zhouxy.plusone.commons.security;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.KeyPair;
|
||||
import java.security.KeyPairGenerator;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.PublicKey;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
|
||||
import com.google.common.annotations.Beta;
|
||||
|
||||
/**
|
||||
* RSA 非对称加密。
|
||||
*/
|
||||
@Beta
|
||||
public class RSA {
|
||||
private static final int DEFAULT_KEY_SIZE = 2048;
|
||||
public static final String NAME = "RSA";
|
||||
private static final String TRANSFORMATION = "RSA/ECB/OAEPWithSHA-256AndMGF1Padding";
|
||||
|
||||
private final PublicKey publicKey;
|
||||
private final PrivateKey privateKey;
|
||||
|
||||
private RSA(final PublicKey publicKey, final PrivateKey privateKey) {
|
||||
this.publicKey = publicKey;
|
||||
this.privateKey = privateKey;
|
||||
}
|
||||
|
||||
public static RSA of(final PublicKey publicKey, final PrivateKey privateKey) {
|
||||
return new RSA(publicKey, privateKey);
|
||||
|
||||
}
|
||||
|
||||
public static RSA of(final KeyPair keyPair) {
|
||||
return new RSA(keyPair.getPublic(), keyPair.getPrivate());
|
||||
|
||||
}
|
||||
|
||||
public static RSA withKeySize(int keySize) {
|
||||
try {
|
||||
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(NAME);
|
||||
keyPairGenerator.initialize(keySize);
|
||||
final KeyPair keyPair = keyPairGenerator.generateKeyPair();
|
||||
return new RSA(keyPair.getPublic(), keyPair.getPrivate());
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new SecurityException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static RSA withDefaultKeySize() {
|
||||
return withKeySize(DEFAULT_KEY_SIZE);
|
||||
}
|
||||
|
||||
public PrivateKey getPrivateKey() {
|
||||
return privateKey;
|
||||
}
|
||||
|
||||
public PublicKey getPublicKey() {
|
||||
return publicKey;
|
||||
}
|
||||
|
||||
public byte[] encrypt(byte[] input) throws GeneralSecurityException {
|
||||
Cipher encryptModeCipher = Cipher.getInstance(TRANSFORMATION);
|
||||
encryptModeCipher.init(Cipher.ENCRYPT_MODE, publicKey);
|
||||
return encryptModeCipher.doFinal(input);
|
||||
}
|
||||
|
||||
public byte[] encrypt(String input) throws GeneralSecurityException {
|
||||
return encrypt(input.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
public String encryptToString(String input) throws GeneralSecurityException {
|
||||
return new BigInteger(1, encrypt(input)).toString(16);
|
||||
}
|
||||
|
||||
public byte[] decrypt(byte[] input) throws GeneralSecurityException {
|
||||
Cipher decryptModeCipher = Cipher.getInstance(TRANSFORMATION);
|
||||
decryptModeCipher.init(Cipher.DECRYPT_MODE, privateKey);
|
||||
return decryptModeCipher.doFinal(input);
|
||||
}
|
||||
|
||||
public byte[] decrypt(String input) throws GeneralSecurityException {
|
||||
return decrypt(new BigInteger(input, 16).toByteArray());
|
||||
}
|
||||
|
||||
public String decryptToString(String input) throws GeneralSecurityException {
|
||||
return new String(decrypt(input));
|
||||
}
|
||||
}
|
@@ -21,12 +21,28 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Function;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.base.JRE;
|
||||
import xyz.zhouxy.plusone.commons.collection.SafeConcurrentHashMap;
|
||||
|
||||
public class ConcurrentHashMapUtil { // TODO 添加文档注释
|
||||
/**
|
||||
* ConcurrentHashMapUtil
|
||||
*
|
||||
* <p>
|
||||
* Java 8 的 {@link ConcurrentHashMap#computeIfAbsent(Object, Function)} 方法有 bug,
|
||||
* 可使用这个工具类的 {@link computeIfAbsentForJava8} 进行替换。
|
||||
*
|
||||
* <p>
|
||||
* <b>NOTE: 方法来自Dubbo,见:issues#2349</b>
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @since 1.0
|
||||
* @see ConcurrentHashMap
|
||||
* @see SafeConcurrentHashMap
|
||||
*/
|
||||
public class ConcurrentHashMapUtil {
|
||||
|
||||
public static <K, V> V computeIfAbsent(ConcurrentHashMap<K, V> map, final K key,
|
||||
final Function<? super K, ? extends V> mappingFunction) {
|
||||
|
||||
Objects.requireNonNull(map, "map");
|
||||
return JRE.isJava8()
|
||||
? computeIfAbsentForJava8(map, key, mappingFunction)
|
||||
: map.computeIfAbsent(key, mappingFunction);
|
||||
@@ -34,6 +50,7 @@ public class ConcurrentHashMapUtil { // TODO 添加文档注释
|
||||
|
||||
public static <K, V> V computeIfAbsentForJava8(ConcurrentHashMap<K, V> map, final K key,
|
||||
final Function<? super K, ? extends V> mappingFunction) {
|
||||
Objects.requireNonNull(key);
|
||||
Objects.requireNonNull(mappingFunction);
|
||||
V v = map.get(key);
|
||||
if (null == v) {
|
||||
|
23
src/test/java/xyz/zhouxy/plusone/commons/SerialTests.java
Normal file
23
src/test/java/xyz/zhouxy/plusone/commons/SerialTests.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package xyz.zhouxy.plusone.commons;
|
||||
|
||||
import java.io.ObjectStreamClass;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import xyz.zhouxy.plusone.commons.exception.BaseRuntimeException;
|
||||
|
||||
@Slf4j
|
||||
class SerialTests {
|
||||
|
||||
@Test
|
||||
void testSerialVersionUID() {
|
||||
long uid = getSerialVersionUID(BaseRuntimeException.class);
|
||||
log.info("\n private static final long serialVersionUID = {}L;", uid);
|
||||
}
|
||||
|
||||
private long getSerialVersionUID(Class<?> cl) {
|
||||
ObjectStreamClass c = ObjectStreamClass.lookup(cl);
|
||||
return c.getSerialVersionUID();
|
||||
}
|
||||
}
|
@@ -10,6 +10,11 @@ import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
||||
import xyz.zhouxy.plusone.commons.annotation.ValueObject;
|
||||
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
class ValidatableStringRecordTests {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(ValidatableStringRecordTests.class);
|
||||
@@ -21,6 +26,16 @@ class ValidatableStringRecordTests {
|
||||
String usernameStr = username.value();
|
||||
assertNotNull(usernameStr);
|
||||
log.info("usernameStr: {}", usernameStr);
|
||||
|
||||
List<Username> usernames = Arrays.asList(
|
||||
Username.of("ZhouXY108"),
|
||||
Username.of("code_108"),
|
||||
Username.of("Luquan"),
|
||||
Username.of("Code108")
|
||||
);
|
||||
log.info("{}", Collections.max(usernames));
|
||||
log.info("{}", Collections.max(usernames,
|
||||
Comparator.<Username, String>comparing(o -> o.value().toLowerCase())));
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user