完善c端登录
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
package com.ruoyi.system.config;
|
||||
|
||||
import com.aliyun.dysmsapi20170525.Client;
|
||||
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
|
||||
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
|
||||
import com.aliyun.teaopenapi.models.Config;
|
||||
import com.ruoyi.common.core.domain.entity.ShopUser;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import static com.alibaba.fastjson2.JSONObject.toJSONString;
|
||||
|
||||
public class AliConfig {
|
||||
private static final Logger log = LoggerFactory.getLogger(AliConfig.class);
|
||||
public static Client createClient() {
|
||||
Config config = new Config()
|
||||
// 配置 AccessKey ID,请确保代码运行环境配置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
||||
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
|
||||
// 配置 AccessKey Secret,请确保代码运行环境配置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
||||
.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
|
||||
// System.getenv()方法表示获取系统环境变量,不要直接在getenv()中填入AccessKey信息。
|
||||
|
||||
// 配置 Endpoint。中国站请使用dysmsapi.aliyuncs.com
|
||||
config.endpoint = "dysmsapi.aliyuncs.com";
|
||||
|
||||
try {
|
||||
return new Client(config);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static String sendMsg(ShopUser shopUser) {
|
||||
// 初始化请求客户端
|
||||
Client client = AliConfig.createClient();
|
||||
|
||||
// 1. 生成6位验证码
|
||||
String code = String.valueOf((int)((Math.random()*9+1)*100000));
|
||||
|
||||
String templateParam = "{\"name\":\"" + shopUser.getUsername() + "\",\"number\":\""+ shopUser.getPhone() +"\"}";
|
||||
|
||||
// 构造API请求对象,请替换请求参数值
|
||||
SendSmsRequest sendSmsRequest = new SendSmsRequest()
|
||||
.setPhoneNumbers(shopUser.getPhone())
|
||||
.setSignName("music")
|
||||
.setTemplateCode(code)
|
||||
.setTemplateParam(templateParam); // TemplateParam为序列化后的JSON字符串。
|
||||
|
||||
// 获取响应对象
|
||||
SendSmsResponse sendSmsResponse;
|
||||
try {
|
||||
sendSmsResponse = client.sendSms(sendSmsRequest);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
log.info("aliyun 发送验证码返回信息:{}", toJSONString(sendSmsResponse));
|
||||
|
||||
if (sendSmsResponse.getStatusCode() == 200){
|
||||
return code;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
package com.ruoyi.system.config;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.ruoyi.common.utils.uuid.UUID;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
public class UmengConfig {
|
||||
|
||||
public CloseableHttpResponse send(String token){
|
||||
String umAppkey = "xxxx";
|
||||
String appKey = "5df88f83570df3b8d40012337";
|
||||
String appSecret = "xxxx";
|
||||
// 下面的url要和阿里云云市场购买的商品对应
|
||||
String url = "https://verify5.market.alicloudapi.com/api/v1/mobile/info?appkey=" + umAppkey;
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
/**
|
||||
* body
|
||||
*/
|
||||
JSONObject object = new JSONObject();
|
||||
object.put("token", token);
|
||||
StringEntity stringEntity = new StringEntity(object.toJSONString(), StandardCharsets.UTF_8);
|
||||
httpPost.setEntity(stringEntity);
|
||||
/**
|
||||
* header
|
||||
*/
|
||||
httpPost.setHeader("Content-Type", "application/json; charset=UTF-8");
|
||||
httpPost.setHeader("Accept", "application/json");
|
||||
httpPost.setHeader("X-Ca-Version", "1");
|
||||
httpPost.setHeader("X-Ca-Signature-Headers", "X-Ca-Version,X-Ca-Stage,X-Ca-Key,X-Ca-Timestamp");
|
||||
httpPost.setHeader("X-Ca-Stage", "RELEASE");
|
||||
httpPost.setHeader("X-Ca-Key", appKey);
|
||||
httpPost.setHeader("X-Ca-Timestamp", String.valueOf(System.currentTimeMillis()));
|
||||
httpPost.setHeader("X-Ca-Nonce", UUID.randomUUID().toString());
|
||||
httpPost.setHeader("Content-MD5", Base64.encodeBase64String(DigestUtils.md5(object.toJSONString())));
|
||||
/**
|
||||
* sign
|
||||
*/
|
||||
String stringToSign = getSignString(httpPost);
|
||||
Mac hmacSha256;
|
||||
try {
|
||||
hmacSha256 = Mac.getInstance("HmacSHA256");
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
byte[] keyBytes = appSecret.getBytes(StandardCharsets.UTF_8);
|
||||
try {
|
||||
hmacSha256.init(new SecretKeySpec(keyBytes, 0, keyBytes.length, "HmacSHA256"));
|
||||
} catch (InvalidKeyException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
String sign = new String(Base64.encodeBase64(hmacSha256.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8))));
|
||||
httpPost.setHeader("X-Ca-Signature", sign);
|
||||
/**
|
||||
* execute
|
||||
*/
|
||||
CloseableHttpClient httpclient = HttpClients.createDefault();
|
||||
try {
|
||||
CloseableHttpResponse response = httpclient.execute(httpPost);
|
||||
return response;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException {
|
||||
|
||||
}
|
||||
|
||||
private static String getSignString(HttpPost httpPost) {
|
||||
Header[] headers = httpPost.getAllHeaders();
|
||||
Map<String, String> map = new HashMap<>();
|
||||
for (Header header : headers) {
|
||||
map.put(header.getName(), header.getValue());
|
||||
}
|
||||
return httpPost.getMethod() + "\n" +
|
||||
map.get("Accept") + "\n" +
|
||||
map.get("Content-MD5") + "\n" +
|
||||
map.get("Content-Type") + "\n\n" +
|
||||
"X-Ca-Key:" + map.get("X-Ca-Key") + "\n" +
|
||||
"X-Ca-Stage:" + map.get("X-Ca-Stage") + "\n" +
|
||||
"X-Ca-Timestamp:" + map.get("X-Ca-Timestamp") + "\n" +
|
||||
"X-Ca-Version:" + map.get("X-Ca-Version") + "\n" +
|
||||
httpPost.getURI().getPath() + "?" + httpPost.getURI().getQuery();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user