mirror of
https://gitee.com/dromara/MaxKey.git
synced 2026-05-15 04:52:09 +08:00
SmsOtpAuthnYunxin
This commit is contained in:
@@ -33,7 +33,7 @@ public abstract class AbstractOptAuthn {
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
|
||||
public static final class OPT_TYPES {
|
||||
public static final class OptTypes {
|
||||
// 手机
|
||||
public static int MOBILE = 2;
|
||||
// 短信
|
||||
|
||||
@@ -37,7 +37,7 @@ public class MailOtpAuthn extends AbstractOptAuthn {
|
||||
_logger.debug(
|
||||
"token " + token + " send to user +" + userInfo.getUsername()
|
||||
+ ", email " + userInfo.getEmail());
|
||||
this.insertDataBase(userInfo, token, userInfo.getUsername(), OPT_TYPES.EMAIL);
|
||||
this.insertDataBase(userInfo, token, userInfo.getUsername(), OptTypes.EMAIL);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
@@ -47,7 +47,7 @@ public class MailOtpAuthn extends AbstractOptAuthn {
|
||||
|
||||
@Override
|
||||
public boolean validate(UserInfo userInfo, String token) {
|
||||
return this.validateDataBase(userInfo, token, OPT_TYPES.EMAIL);
|
||||
return this.validateDataBase(userInfo, token, OptTypes.EMAIL);
|
||||
}
|
||||
|
||||
public void setEmailConfig(EmailConfig emailConfig) {
|
||||
|
||||
@@ -15,13 +15,13 @@ public class SmsOtpAuthn extends AbstractOptAuthn {
|
||||
String token = this.genToken(userInfo);
|
||||
// TODO:You must add send sms code here
|
||||
|
||||
this.insertDataBase(userInfo, token, userInfo.getUsername(), OPT_TYPES.SMS);
|
||||
this.insertDataBase(userInfo, token, userInfo.getUsername(), OptTypes.SMS);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean validate(UserInfo userInfo, String token) {
|
||||
return this.validateDataBase(userInfo, token, OPT_TYPES.SMS);
|
||||
return this.validateDataBase(userInfo, token, OptTypes.SMS);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
package org.maxkey.crypto.password.opt.impl.sms;
|
||||
|
||||
public interface SendSms {
|
||||
|
||||
public String sendSms();
|
||||
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package org.maxkey.crypto.password.opt.impl.sms.netease;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
|
||||
public class SendSmsYunxinCheckSumBuilder {
|
||||
public class SmsOtpAuthnCheckSumBuilder {
|
||||
// 计算并获取CheckSum
|
||||
public static String getCheckSum(String appSecret, String nonce, String curTime) {
|
||||
return encode("sha1", appSecret + nonce + curTime);
|
||||
@@ -11,7 +11,8 @@ import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.maxkey.crypto.password.opt.impl.sms.SendSms;
|
||||
import org.maxkey.crypto.password.opt.impl.SmsOtpAuthn;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
/**
|
||||
* 网易云信的短信验证.
|
||||
@@ -19,7 +20,12 @@ import org.maxkey.crypto.password.opt.impl.sms.SendSms;
|
||||
*
|
||||
*/
|
||||
|
||||
public class SendSmsYunxin implements SendSms {
|
||||
public class SmsOtpAuthnYunxin extends SmsOtpAuthn {
|
||||
public SmsOtpAuthnYunxin(JdbcTemplate jdbcTemplate) {
|
||||
super(jdbcTemplate);
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
//发送验证码的请求路径URL
|
||||
private static final String
|
||||
SERVER_URL = "https://api.netease.im/sms/sendcode.action";
|
||||
@@ -49,7 +55,7 @@ public class SendSmsYunxin implements SendSms {
|
||||
/*
|
||||
* 参考计算CheckSum的java代码,在上述文档的参数列表中,有CheckSum的计算文档示例
|
||||
*/
|
||||
String checkSum = SendSmsYunxinCheckSumBuilder
|
||||
String checkSum = SmsOtpAuthnCheckSumBuilder
|
||||
.getCheckSum(APP_SECRET, NONCE, curTime);
|
||||
|
||||
// 设置请求的header
|
||||
@@ -93,11 +99,5 @@ public class SendSmsYunxin implements SendSms {
|
||||
public static void main(String[] args) throws Exception {
|
||||
sendSms(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String sendSms() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
package org.maxkey.crypto.password.opt.impl.sms;
|
||||
@@ -7,133 +7,148 @@ import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Random;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.maxkey.crypto.Base64Utils;
|
||||
|
||||
public class StringGenerator {
|
||||
|
||||
private static final int MAX_PID = 65536;
|
||||
|
||||
private char[] DEFAULT_CODEC = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
|
||||
|
||||
public static final char[] DEFAULT_CODE_NUMBER = "1234567890".toCharArray();
|
||||
|
||||
public static final char[] DEFAULT_CODE_LOWERCASE = "abcdefghijklmnopqrstuvwxyz".toCharArray();
|
||||
|
||||
public static final char[] DEFAULT_CODE_UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
|
||||
|
||||
public static final char[] DEFAULT_CODE_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
|
||||
|
||||
public static final char[] DEFAULT_CODE_NUMBER_LETTERS = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
|
||||
private static final int MAX_PID = 65536;
|
||||
|
||||
public static int processId;
|
||||
private static final char[] DEFAULT_CODEC =
|
||||
"1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
|
||||
|
||||
private static final String uuidRegex = "^[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}$";
|
||||
public static final char[] DEFAULT_CODE_NUMBER = "1234567890".toCharArray();
|
||||
|
||||
|
||||
private Random random = new SecureRandom();
|
||||
public static final char[] DEFAULT_CODE_LOWERCASE = "abcdefghijklmnopqrstuvwxyz".toCharArray();
|
||||
|
||||
private int length;
|
||||
|
||||
static {
|
||||
final String jvmName = ManagementFactory.getRuntimeMXBean().getName();
|
||||
int index = jvmName.indexOf('@');
|
||||
if (index < 1)
|
||||
public static final char[] DEFAULT_CODE_UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
|
||||
|
||||
public static final char[] DEFAULT_CODE_LETTERS =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
|
||||
|
||||
public static final char[] DEFAULT_CODE_NUMBER_LETTERS =
|
||||
"1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
|
||||
|
||||
public static int processId;
|
||||
|
||||
private static final String uuidRegex =
|
||||
"^[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}$";
|
||||
|
||||
private Random random = new SecureRandom();
|
||||
|
||||
private int length;
|
||||
|
||||
private char[] codec = DEFAULT_CODEC;
|
||||
|
||||
static {
|
||||
final String jvmName = ManagementFactory.getRuntimeMXBean().getName();
|
||||
int index = jvmName.indexOf('@');
|
||||
if (index < 1) {
|
||||
throw new RuntimeException("Could not get PID");
|
||||
|
||||
}
|
||||
try {
|
||||
processId= Integer.parseInt(jvmName.substring(0, index)) % MAX_PID;
|
||||
processId = Integer.parseInt(jvmName.substring(0, index)) % MAX_PID;
|
||||
} catch (NumberFormatException e) {
|
||||
throw new RuntimeException("Could not get PID");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a generator with the default length (6).
|
||||
*/
|
||||
public StringGenerator() {
|
||||
this(6);
|
||||
}
|
||||
/**
|
||||
* Create a generator with the default length (6).
|
||||
*/
|
||||
public StringGenerator() {
|
||||
this(6);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a generator of random strings of the length provided
|
||||
*
|
||||
* @param length the length of the strings generated
|
||||
*/
|
||||
public StringGenerator(int length) {
|
||||
this.length = length;
|
||||
}
|
||||
/**
|
||||
* Create a generator of random strings of the length provided.
|
||||
*
|
||||
* @param length the length of the strings generated
|
||||
*/
|
||||
public StringGenerator(int length) {
|
||||
this.length = length;
|
||||
}
|
||||
|
||||
public StringGenerator(char[] defaultCode,int length) {
|
||||
this.DEFAULT_CODEC=defaultCode;
|
||||
this.length = length;
|
||||
}
|
||||
|
||||
public String randomGenerate() {
|
||||
byte[] verifierBytes = new byte[length];
|
||||
random.nextBytes(verifierBytes);
|
||||
return getString(verifierBytes);
|
||||
}
|
||||
|
||||
public String uuidGenerate() {
|
||||
return UUID.randomUUID().toString().toLowerCase();
|
||||
}
|
||||
|
||||
public String uniqueGenerate() {
|
||||
StringBuffer uniqueString=new StringBuffer("");
|
||||
public StringGenerator(char[] defaultCode, int length) {
|
||||
this.codec = defaultCode;
|
||||
this.length = length;
|
||||
}
|
||||
|
||||
this.length=9;
|
||||
String randomString =randomGenerate();
|
||||
uniqueString.append(randomString.subSequence(0, 4));
|
||||
|
||||
Date currentDate=new Date();
|
||||
DateFormat dateFormat = new SimpleDateFormat("ddMMyyyyHHmmssSSS");
|
||||
String dateString=Base64Utils.encodeBase64(dateFormat.format(currentDate).getBytes());
|
||||
dateString=dateString.substring(0, dateString.length()-1);
|
||||
uniqueString.append(dateString);
|
||||
|
||||
uniqueString.append(randomString.subSequence(5, 8));
|
||||
|
||||
return uniqueString.toString();
|
||||
}
|
||||
/**
|
||||
* randomGenerate.
|
||||
* @return
|
||||
*/
|
||||
public String randomGenerate() {
|
||||
byte[] verifierBytes = new byte[length];
|
||||
random.nextBytes(verifierBytes);
|
||||
return getString(verifierBytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert these random bytes to a verifier string. The length of the byte array can be
|
||||
* {@link #setLength(int) configured}. The default implementation mods the bytes to fit into the
|
||||
* ASCII letters 1-9, A-Z, a-z .
|
||||
*
|
||||
* @param verifierBytes The bytes.
|
||||
* @return The string.
|
||||
*/
|
||||
protected String getString(byte[] verifierBytes) {
|
||||
char[] chars = new char[verifierBytes.length];
|
||||
for (int i = 0; i < verifierBytes.length; i++) {
|
||||
chars[i] = DEFAULT_CODEC[((verifierBytes[i] & 0xFF) % DEFAULT_CODEC.length)];
|
||||
}
|
||||
return new String(chars);
|
||||
}
|
||||
public String uuidGenerate() {
|
||||
return UUID.randomUUID().toString().toLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* uniqueGenerate.
|
||||
* @return
|
||||
*/
|
||||
public String uniqueGenerate() {
|
||||
StringBuffer uniqueString = new StringBuffer("");
|
||||
|
||||
this.length = 9;
|
||||
String randomString = randomGenerate();
|
||||
uniqueString.append(randomString.subSequence(0, 4));
|
||||
|
||||
Date currentDate = new Date();
|
||||
DateFormat dateFormat = new SimpleDateFormat("ddMMyyyyHHmmssSSS");
|
||||
String dateString = Base64Utils.encodeBase64(dateFormat.format(currentDate).getBytes());
|
||||
dateString = dateString.substring(0, dateString.length() - 1);
|
||||
uniqueString.append(dateString);
|
||||
|
||||
uniqueString.append(randomString.subSequence(5, 8));
|
||||
|
||||
return uniqueString.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert these random bytes to a verifier string. The length of the byte array
|
||||
* can be {@link #setLength(int) configured}. The default implementation mods
|
||||
* the bytes to fit into the ASCII letters 1-9, A-Z, a-z .
|
||||
*
|
||||
* @param verifierBytes The bytes.
|
||||
* @return The string.
|
||||
*/
|
||||
protected String getString(byte[] verifierBytes) {
|
||||
char[] chars = new char[verifierBytes.length];
|
||||
for (int i = 0; i < verifierBytes.length; i++) {
|
||||
chars[i] = codec[((verifierBytes[i] & 0xFF) % codec.length)];
|
||||
}
|
||||
return new String(chars);
|
||||
}
|
||||
|
||||
/**
|
||||
* The random value generator used to create token secrets.
|
||||
*
|
||||
* @param random The random value generator used to create token secrets.
|
||||
*/
|
||||
public void setRandom(Random random) {
|
||||
this.random = random;
|
||||
}
|
||||
|
||||
/**
|
||||
* The length of string to generate.
|
||||
*
|
||||
* @param length the length to set
|
||||
*/
|
||||
public void setLength(int length) {
|
||||
this.length = length;
|
||||
}
|
||||
|
||||
public void setCodec(char[] codec) {
|
||||
this.codec = codec;
|
||||
}
|
||||
|
||||
public static boolean uuidMatches(String uuidString) {
|
||||
return uuidString.matches(uuidRegex);
|
||||
}
|
||||
|
||||
/**
|
||||
* The random value generator used to create token secrets.
|
||||
*
|
||||
* @param random The random value generator used to create token secrets.
|
||||
*/
|
||||
public void setRandom(Random random) {
|
||||
this.random = random;
|
||||
}
|
||||
|
||||
/**
|
||||
* The length of string to generate.
|
||||
*
|
||||
* @param length the length to set
|
||||
*/
|
||||
public void setLength(int length) {
|
||||
this.length = length;
|
||||
}
|
||||
|
||||
|
||||
public static boolean uuidMatches(String uuidString) {
|
||||
return uuidString.matches(uuidRegex);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user