优化

pull/82/head^2
awenes 2024-04-04 21:52:03 +08:00
parent fd6c83bd98
commit 69fd16f7d5
10 changed files with 0 additions and 4877 deletions

View File

@ -1,56 +0,0 @@
/*
* eiam-common - Employee Identity and Access Management
* Copyright © 2022-Present Jinan Yuanchuang Network Technology Co., Ltd. (support@topiam.cn)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cn.topiam.employee.common.constant;
import org.apache.commons.lang3.RandomStringUtils;
/**
*
*
* @author TopIAM
* Created by support@topiam.cn on 2022/4/3 23:37
*/
public final class CommonConstants {
/**
*
*/
public static final String TYPE = "@type";
/**
*
*/
public static final String CALLBACK_URL = "callbackUrl";
/**
*
*/
public static final String PATH_SEPARATOR = "/";
/**
*
*/
public static final String SYSTEM_DEFAULT_USER_NAME = "topiam";
/**
*
*/
public static final String RANDOM_AVATAR = "https://api.multiavatar.com/";
public static String getRandomAvatar() {
return RANDOM_AVATAR + RandomStringUtils.randomAlphanumeric(6) + ".svg";
}
}

View File

@ -1,193 +0,0 @@
/*
* eiam-common - Employee Identity and Access Management
* Copyright © 2022-Present Jinan Yuanchuang Network Technology Co., Ltd. (support@topiam.cn)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cn.topiam.employee.common.util;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.imageio.ImageIO;
import org.apache.commons.codec.binary.Base64;
import org.springframework.core.io.ClassPathResource;
/**
*
* @author TopIAM
* Created by support@topiam.cn on 2023/3/30 22:41
*/
public class ImageAvatarUtils {
/**
* 100*100
*
*
* @param name {@link String}
*/
public static BufferedImage generateAvatarImg(String name) {
try {
int width = 100;
int height = 100;
int nameLen = name.length();
String nameWritten;
// 如果用户输入的姓名少于等于2个字符不用截取
if (nameLen <= 2) {
nameWritten = name;
} else {
// 如果用户输入的姓名大于等于3个字符截取后面两位
String first = name.substring(0, 1);
if (isChinese(first)) {
// 截取倒数两位汉字
nameWritten = name.substring(nameLen - 2);
} else {
// 截取前面的两个英文字母
nameWritten = name.substring(0, 2).toUpperCase();
}
}
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D) bi.getGraphics();
//消除文字锯齿
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
//消除画图锯
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setBackground(getRandomColor());
g2.clearRect(0, 0, width, height);
g2.setPaint(Color.WHITE);
//加载外部字体文件
ClassPathResource resource = new ClassPathResource(
"/fonts/AlibabaPuHuiTi-2-55-Regular.ttf");
InputStream inputStream = resource.getInputStream();
Font font = Font.createFont(java.awt.Font.TRUETYPE_FONT, inputStream);
// 两个字及以上
if (nameWritten.length() >= 2) {
font = font.deriveFont(Font.BOLD, 30);
g2.setFont(font);
String firstWritten = nameWritten.substring(0, 1);
String secondWritten = nameWritten.substring(1, 2);
// 两个中文 如 张三
if (isChinese(firstWritten) && isChinese(secondWritten)) {
g2.drawString(nameWritten, 20, 60);
}
// 首中次英 如 张S
else if (isChinese(firstWritten) && !isChinese(secondWritten)) {
g2.drawString(nameWritten, 27, 60);
}
// 首英,如 ZS
else {
nameWritten = nameWritten.substring(0, 1);
}
}
// 一个字
if (nameWritten.length() == 1) {
// 中文
if (isChinese(nameWritten)) {
font = font.deriveFont(Font.PLAIN, 50);
g2.setFont(font);
g2.drawString(nameWritten, 25, 70);
}
// 英文
else {
font = font.deriveFont(Font.PLAIN, 55);
g2.setFont(font);
g2.drawString(nameWritten.toUpperCase(), 33, 67);
}
}
return makeRoundedCorner(bi, 99);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
*
* @param str {@link String}
* @return {@link Boolean}
*/
public static boolean isChinese(String str) {
String regEx = "[\\u4e00-\\u9fa5]+";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
return m.find();
}
/**
*
* @return {@link Color}
*/
private static Color getRandomColor() {
String[] beautifulColors = new String[] { "22,119,255" };
int len = beautifulColors.length;
Random random = new Random();
String[] color = beautifulColors[random.nextInt(len)].split(",");
return new Color(Integer.parseInt(color[0]), Integer.parseInt(color[1]),
Integer.parseInt(color[2]));
}
/**
*
* @param image {@link BufferedImage}
* @param cornerRadius {@link Integer}
* @return {@link BufferedImage}
*/
public static BufferedImage makeRoundedCorner(BufferedImage image, int cornerRadius) {
int w = image.getWidth();
int h = image.getHeight();
BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = output.createGraphics();
g2.setComposite(AlphaComposite.Src);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(Color.WHITE);
g2.fill(new RoundRectangle2D.Float(0, 0, w, h, cornerRadius, cornerRadius));
g2.setComposite(AlphaComposite.SrcAtop);
g2.drawImage(image, 0, 0, null);
g2.dispose();
return output;
}
/**
* BufferedImage base64
* @param bufferedImage {@link BufferedImage}
* @return {@link String}
*/
public static String bufferedImageToBase64(BufferedImage bufferedImage) {
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", stream);
Base64 base = new Base64();
String base64 = base.encodeToString(stream.toByteArray());
return "data:image/png;base64," + base64;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -1,64 +0,0 @@
/*
* eiam-common - Employee Identity and Access Management
* Copyright © 2022-Present Jinan Yuanchuang Network Technology Co., Ltd. (support@topiam.cn)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cn.topiam.employee.common.util;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import jakarta.servlet.http.HttpServletRequest;
/**
* @author TopIAM
* Created by support@topiam.cn on 2022-03-02 22:57
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class RequestUtils {
/**
**requestxml
*/
public static <T> T getXml(String body, Class<T> clazz) throws JAXBException {
StringReader reader = new StringReader(body);
return (T) JAXBContext.newInstance(clazz).createUnmarshaller().unmarshal(reader);
}
/**
**requestmap
*/
public static Map<String, Object> getParams(HttpServletRequest request) {
Map<String, Object> params = new HashMap<>(16);
Map<String, String[]> requestParams = request.getParameterMap();
for (Map.Entry<String, String[]> entry : requestParams.entrySet()) {
String[] values = entry.getValue();
String valueStr = "";
for (int i = 0; i < values.length; i++) {
valueStr = (i == values.length - 1) ? valueStr + values[i]
: valueStr + values[i] + ",";
}
params.put(entry.getKey(), valueStr);
}
return params;
}
}

View File

@ -1,138 +0,0 @@
/*
* eiam-common - Employee Identity and Access Management
* Copyright © 2022-Present Jinan Yuanchuang Network Technology Co., Ltd. (support@topiam.cn)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cn.topiam.employee.common.util;
import java.io.ByteArrayInputStream;
import java.io.CharArrayReader;
import java.io.IOException;
import java.security.*;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.security.interfaces.RSAPrivateKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import javax.xml.bind.DatatypeConverter;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openssl.PEMDecryptorProvider;
import org.bouncycastle.openssl.PEMEncryptedKeyPair;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
import org.bouncycastle.openssl.jcajce.JcePEMDecryptorProviderBuilder;
/**
*
*
* @author TopIAM
*/
public class X509Utils {
public static final String LINE_SEPARATOR = System.getProperty("line.separator");
public static final String BEGIN_CERT = "-----BEGIN CERTIFICATE-----" + LINE_SEPARATOR;
public static final String END_CERT = "-----END CERTIFICATE-----";
public static final String BEGIN_KEY = "-----BEGIN RSA PRIVATE KEY-----" + LINE_SEPARATOR;
public static final String END_KEY = "-----END RSA PRIVATE KEY-----";
static {
Security.addProvider(new BouncyCastleProvider());
}
public static byte[] getDer(String combinedKeyAndCertPem, String begin, String end) {
String[] tokens = combinedKeyAndCertPem.split(begin);
tokens = tokens[0].split(end);
return getDer(tokens[0]);
}
public static byte[] getDer(String pem) {
String data = keyCleanup(pem);
return DatatypeConverter.parseBase64Binary(data);
}
public static String keyCleanup(String pem) {
return pem.replace(BEGIN_CERT, "").replace(END_CERT, "").replace(BEGIN_KEY, "")
.replace(END_KEY, "").replace("\n", "").trim();
}
public static X509Certificate getCertificate(byte[] der) throws CertificateException {
CertificateFactory factory = CertificateFactory.getInstance("X.509");
return (X509Certificate) factory.generateCertificate(new ByteArrayInputStream(der));
}
public static RSAPrivateKey getPrivateKey(byte[] der,
String algorithm) throws InvalidKeySpecException,
NoSuchAlgorithmException {
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(der);
KeyFactory factory = KeyFactory.getInstance(algorithm);
return (RSAPrivateKey) factory.generatePrivate(spec);
}
public static PrivateKey readPrivateKey(String pem, String passphrase) throws IOException {
PEMParser parser = new PEMParser(new CharArrayReader(pem.toCharArray()));
Object obj = parser.readObject();
parser.close();
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
KeyPair kp;
if (obj == null) {
throw new NullPointerException("Unable to decode PEM key:" + pem);
} else if (obj instanceof PEMEncryptedKeyPair ckp) {
// Encrypted key - we will use provided password
PEMDecryptorProvider decProv = new JcePEMDecryptorProviderBuilder()
.build(passphrase.toCharArray());
kp = converter.getKeyPair(ckp.decryptKeyPair(decProv));
} else if (obj instanceof PrivateKeyInfo privateKeyInfo) {
return converter.getPrivateKey(privateKeyInfo);
} else {
// Unencrypted key - no password needed
PEMKeyPair ukp = (PEMKeyPair) obj;
kp = converter.getKeyPair(ukp);
}
return kp.getPrivate();
}
public static PublicKey readPublicKey(String pem, String passphrase) throws IOException {
PEMParser parser = new PEMParser(new CharArrayReader(pem.toCharArray()));
Object obj = parser.readObject();
parser.close();
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
KeyPair kp;
if (obj == null) {
throw new NullPointerException("Unable to decode PEM key:" + pem);
} else if (obj instanceof PEMEncryptedKeyPair ckp) {
// Encrypted key - we will use provided password
PEMDecryptorProvider decProv = new JcePEMDecryptorProviderBuilder()
.build(passphrase.toCharArray());
kp = converter.getKeyPair(ckp.decryptKeyPair(decProv));
} else if (obj instanceof SubjectPublicKeyInfo privateKeyInfo) {
return converter.getPublicKey(privateKeyInfo);
} else {
// Unencrypted key - no password needed
PEMKeyPair ukp = (PEMKeyPair) obj;
kp = converter.getKeyPair(ukp);
}
return kp.getPublic();
}
}

View File

@ -1,24 +0,0 @@
/*
* eiam-console - Employee Identity and Access Management
* Copyright © 2022-Present Jinan Yuanchuang Network Technology Co., Ltd. (support@topiam.cn)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
*
*
* @author TopIAM
* Created by support@topiam.cn on 2020/10/29 23:12
*/
package cn.topiam.employee.console.security.handler;

View File

@ -1,18 +0,0 @@
/*
* eiam-console - Employee Identity and Access Management
* Copyright © 2022-Present Jinan Yuanchuang Network Technology Co., Ltd. (support@topiam.cn)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cn.topiam.employee.console.security.listener;

View File

@ -1,18 +0,0 @@
/*
* eiam-console - Employee Identity and Access Management
* Copyright © 2022-Present Jinan Yuanchuang Network Technology Co., Ltd. (support@topiam.cn)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cn.topiam.employee.console.security;

View File

@ -1,24 +0,0 @@
/*
* eiam-portal - Employee Identity and Access Management
* Copyright © 2022-Present Jinan Yuanchuang Network Technology Co., Ltd. (support@topiam.cn)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
*
*
* @author TopIAM
* Created by support@topiam.cn on 2020/10/29 23:12
*/
package cn.topiam.employee.portal.security.handler;

View File

@ -1,18 +0,0 @@
/*
* eiam-portal - Employee Identity and Access Management
* Copyright © 2022-Present Jinan Yuanchuang Network Technology Co., Ltd. (support@topiam.cn)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cn.topiam.employee.portal.security;