刘力
3 years ago
21 changed files with 904 additions and 19 deletions
-
2common/pom.xml
-
12common/src/main/java/com/canvas/web/config/MessageConfig.java
-
50common/src/main/java/com/canvas/web/config/SubMailMsgConfig.java
-
54common/src/main/java/com/canvas/web/utils/ConfigLoader.java
-
33system/pom.xml
-
23system/src/main/java/com/canvas/web/modules/security/controller/AuthorizationController.java
-
44system/src/main/java/com/canvas/web/modules/security/msglib/MESSAGEXsend.java
-
94system/src/main/java/com/canvas/web/modules/security/msglib/Message.java
-
38system/src/main/java/com/canvas/web/modules/security/msglib/base/ISender.java
-
358system/src/main/java/com/canvas/web/modules/security/msglib/base/Sender.java
-
10system/src/main/java/com/canvas/web/modules/security/msglib/base/SenderWapper.java
-
84system/src/main/java/com/canvas/web/modules/security/msglib/entity/DataStore.java
-
7system/src/main/java/com/canvas/web/modules/security/security/TokenConfigurer.java
-
1system/src/main/java/com/canvas/web/modules/security/service/UserDetailsServiceImpl.java
-
4system/src/main/java/com/canvas/web/modules/security/service/dto/AuthUserDto.java
-
4system/src/main/java/com/canvas/web/modules/system/domain/User.java
-
7system/src/main/java/com/canvas/web/modules/system/service/UserService.java
-
6system/src/main/java/com/canvas/web/modules/system/service/impl/UserServiceImpl.java
-
23system/src/main/java/com/canvas/web/modules/utils/HttpDeleteUtil.java
-
53system/src/main/java/com/canvas/web/modules/utils/RequestEncoder.java
-
16system/src/main/resources/app_config.properties
@ -0,0 +1,12 @@ |
|||
package com.canvas.web.config; |
|||
|
|||
import com.canvas.web.config.SubMailMsgConfig; |
|||
|
|||
public class MessageConfig extends SubMailMsgConfig { |
|||
|
|||
public static final String APP_ID = "msg_appid"; |
|||
|
|||
public static final String APP_KEY = "msg_appkey"; |
|||
|
|||
public static final String APP_SIGNTYPE = "msg_signtype"; |
|||
} |
@ -0,0 +1,50 @@ |
|||
package com.canvas.web.config; |
|||
|
|||
|
|||
//短信验证码配置类 |
|||
public class SubMailMsgConfig{ |
|||
|
|||
protected String appId = null; |
|||
|
|||
protected String appKey = null; |
|||
|
|||
protected String signType=null; |
|||
|
|||
//授权模式 |
|||
public static final String TYPE_NORMAL = "normal"; |
|||
public static final String TYPE_MD5 = "md5"; |
|||
public static final String TYPE_SHA1 = "sha1"; |
|||
|
|||
public String getAppId() { |
|||
return appId; |
|||
} |
|||
|
|||
public void setAppId(String appId) { |
|||
this.appId = appId; |
|||
} |
|||
|
|||
public String getAppKey() { |
|||
return appKey; |
|||
} |
|||
|
|||
public void setAppKey(String appKey) { |
|||
this.appKey = appKey; |
|||
} |
|||
|
|||
|
|||
public String getSignType() { |
|||
if (this.checkType(this.signType)) { |
|||
return this.signType; |
|||
} |
|||
return TYPE_NORMAL; |
|||
} |
|||
|
|||
public void setSignType(String signType) { |
|||
this.signType = signType; |
|||
} |
|||
|
|||
private boolean checkType(String signType) { |
|||
return TYPE_NORMAL.equals(signType) || TYPE_MD5.equals(signType) |
|||
|| TYPE_SHA1.equals(signType); |
|||
} |
|||
} |
@ -0,0 +1,54 @@ |
|||
package com.canvas.web.utils; |
|||
|
|||
import com.canvas.web.config.MessageConfig; |
|||
import com.canvas.web.config.SubMailMsgConfig; |
|||
|
|||
import java.io.IOException; |
|||
import java.util.Properties; |
|||
|
|||
public class ConfigLoader { |
|||
|
|||
private static Properties pros = null; |
|||
|
|||
/** |
|||
* 加载文件时,类载入,静态块内部的操作将被运行一次 |
|||
* */ |
|||
static { |
|||
pros = new Properties(); |
|||
try { |
|||
pros.load(ConfigLoader.class |
|||
.getResourceAsStream("/app_config.properties")); |
|||
} catch (IOException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
|
|||
public static enum ConfigType { |
|||
Mail,Message,Voice,Internationalsms,Mobiledata |
|||
}; |
|||
|
|||
public static SubMailMsgConfig load(ConfigType type) { |
|||
switch (type) { |
|||
// case Mail: |
|||
// return createMailConfig(); |
|||
case Message: |
|||
return createMessageConfig(); |
|||
// case Voice: |
|||
// return createVoiceConfig(); |
|||
// case Internationalsms: |
|||
// return createInternationalsmsConfig(); |
|||
// case Mobiledata: |
|||
// return createMobiledataConfig(); |
|||
default: |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
private static SubMailMsgConfig createMessageConfig() { |
|||
SubMailMsgConfig config = new MessageConfig(); |
|||
config.setAppId(pros.getProperty(MessageConfig.APP_ID)); |
|||
config.setAppKey(pros.getProperty(MessageConfig.APP_KEY)); |
|||
config.setSignType(pros.getProperty(MessageConfig.APP_SIGNTYPE)); |
|||
return config; |
|||
} |
|||
} |
@ -0,0 +1,44 @@ |
|||
package com.canvas.web.modules.security.msglib; |
|||
|
|||
import com.canvas.web.config.SubMailMsgConfig; |
|||
import com.canvas.web.modules.security.msglib.base.ISender; |
|||
import com.canvas.web.modules.security.msglib.base.SenderWapper; |
|||
|
|||
public class MESSAGEXsend extends SenderWapper { |
|||
|
|||
protected SubMailMsgConfig config = null; |
|||
public static final String ADDRESSBOOK = "addressbook"; |
|||
public static final String TO = "to"; |
|||
public static final String PROJECT = "project"; |
|||
public static final String VARS = "vars"; |
|||
public static final String LINKS = "links"; |
|||
|
|||
public MESSAGEXsend(SubMailMsgConfig config) { |
|||
this.config = config; |
|||
} |
|||
|
|||
public void addTo(String address) { |
|||
requestData.addWithComma(TO, address); |
|||
} |
|||
|
|||
public void addAddressBook(String addressbook) { |
|||
requestData.addWithComma(ADDRESSBOOK, addressbook); |
|||
} |
|||
|
|||
public void setProject(String project) { |
|||
requestData.put(PROJECT, project); |
|||
} |
|||
|
|||
public void addVar(String key, String val) { |
|||
requestData.addWithJson(VARS, key, val); |
|||
} |
|||
|
|||
@Override |
|||
public ISender getSender() { |
|||
return new Message(this.config); |
|||
} |
|||
|
|||
public String xsend(){ |
|||
return getSender().xsend(requestData); |
|||
} |
|||
} |
@ -0,0 +1,94 @@ |
|||
package com.canvas.web.modules.security.msglib; |
|||
|
|||
import com.canvas.web.config.SubMailMsgConfig; |
|||
import com.canvas.web.modules.security.msglib.base.Sender; |
|||
|
|||
import java.util.Map; |
|||
|
|||
public class Message extends Sender { |
|||
|
|||
private static final String API_SEND = "http://api.submail.cn/message/send.json"; |
|||
private static final String API_XSEND = "http://api.submail.cn/message/xsend.json"; |
|||
private static final String API_MULTIXSEND = "http://api.submail.cn/message/multixsend.json"; |
|||
private static final String API_MULTISEND = "http://api.submail.cn/message/multisend.json"; |
|||
private static final String API_LOG = "http://api.submail.cn/log/message.json"; |
|||
private static final String API_TEMPLATE = "http://api.submail.cn/message/template.json"; |
|||
private static final String API_SUBSCRIBE = "http://api.submail.cn/addressbook/message/subscribe.json"; |
|||
private static final String API_UNSUBSCRIBE = "http://api.submail.cn/addressbook/message/unsubscribe.json"; |
|||
private static final String API_BALANCE = "http://api.mysubmail.com/balance/sms.json"; |
|||
|
|||
public Message(SubMailMsgConfig config) { |
|||
this.config = config; |
|||
} |
|||
|
|||
/** |
|||
* 发送请求数据到服务器,数据由两部分组成,其中一个是原始数据,另一个是签名 |
|||
*/ |
|||
@Override |
|||
public String send(Map<String, Object> data) { |
|||
return request(API_SEND, data); |
|||
} |
|||
|
|||
@Override |
|||
public String xsend(Map<String, Object> data) { |
|||
return request(API_XSEND, data); |
|||
} |
|||
|
|||
@Override |
|||
public String multixsend(Map<String, Object> data) { |
|||
return request(API_MULTIXSEND, data); |
|||
} |
|||
|
|||
@Override |
|||
public String multisend(Map<String, Object> data) { |
|||
return request(API_MULTISEND, data); |
|||
} |
|||
|
|||
@Override |
|||
public String subscribe(Map<String, Object> data) { |
|||
// TODO Auto-generated method stub |
|||
return request(API_SUBSCRIBE, data); |
|||
} |
|||
|
|||
@Override |
|||
public String unsubscribe(Map<String, Object> data) { |
|||
// TODO Auto-generated method stub |
|||
return request(API_UNSUBSCRIBE, data); |
|||
} |
|||
|
|||
@Override |
|||
public String log(Map<String, Object> data) { |
|||
// TODO Auto-generated method stub |
|||
return request(API_LOG, data); |
|||
} |
|||
|
|||
@Override |
|||
public String balance(Map<String, Object> data) { |
|||
// TODO Auto-generated method stub |
|||
return request(API_BALANCE, data); |
|||
} |
|||
|
|||
@Override |
|||
public String get(Map<String, Object> data) { |
|||
// TODO Auto-generated method stub |
|||
return getMethodRequest(API_TEMPLATE, data); |
|||
} |
|||
|
|||
@Override |
|||
public String post(Map<String, Object> data) { |
|||
// TODO Auto-generated method stub |
|||
return request(API_TEMPLATE, data); |
|||
} |
|||
|
|||
@Override |
|||
public String put(Map<String, Object> data) { |
|||
// TODO Auto-generated method stub |
|||
return putMethodRequest(API_TEMPLATE, data); |
|||
} |
|||
|
|||
@Override |
|||
public String delete(Map<String, Object> data) { |
|||
// TODO Auto-generated method stub |
|||
return deleteMethodRequest(API_TEMPLATE, data); |
|||
} |
|||
} |
@ -0,0 +1,38 @@ |
|||
package com.canvas.web.modules.security.msglib.base; |
|||
|
|||
import java.util.Map; |
|||
|
|||
public interface ISender { |
|||
|
|||
public String send(Map<String, Object> data); |
|||
|
|||
public String xsend(Map<String, Object> data); |
|||
|
|||
public String subscribe(Map<String, Object> data); |
|||
|
|||
public String unsubscribe(Map<String, Object> data); |
|||
|
|||
public String multixsend(Map<String, Object> data); |
|||
|
|||
public String multisend(Map<String, Object> data); |
|||
|
|||
public String verify(Map<String, Object> data); |
|||
|
|||
public String log(Map<String, Object> data); |
|||
|
|||
public String get(Map<String, Object> data); |
|||
|
|||
public String post(Map<String, Object> data); |
|||
|
|||
public String put(Map<String, Object> data); |
|||
|
|||
public String delete(Map<String, Object> data); |
|||
|
|||
public String selMobiledata(Map<String, Object> data); |
|||
|
|||
public String toService(Map<String, Object> data); |
|||
|
|||
public String charge(Map<String, Object> data); |
|||
|
|||
public String balance(Map<String, Object> data); |
|||
} |
@ -0,0 +1,358 @@ |
|||
package com.canvas.web.modules.security.msglib.base; |
|||
|
|||
import java.io.File; |
|||
import java.io.IOException; |
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
|
|||
import com.canvas.web.modules.utils.HttpDeleteUtil; |
|||
import com.canvas.web.modules.utils.RequestEncoder; |
|||
import net.sf.json.JSONObject; |
|||
import org.apache.http.HttpEntity; |
|||
import org.apache.http.HttpResponse; |
|||
import org.apache.http.NameValuePair; |
|||
import org.apache.http.client.ClientProtocolException; |
|||
import org.apache.http.client.entity.UrlEncodedFormEntity; |
|||
import org.apache.http.client.methods.HttpGet; |
|||
import org.apache.http.client.methods.HttpPost; |
|||
import org.apache.http.client.methods.HttpPut; |
|||
import org.apache.http.entity.ContentType; |
|||
import org.apache.http.entity.StringEntity; |
|||
import org.apache.http.impl.client.CloseableHttpClient; |
|||
import org.apache.http.impl.client.HttpClientBuilder; |
|||
import org.apache.http.message.BasicNameValuePair; |
|||
import org.apache.http.protocol.HTTP; |
|||
import org.apache.http.util.EntityUtils; |
|||
import com.canvas.web.config.SubMailMsgConfig; |
|||
import org.apache.http.impl.client.CloseableHttpClient; |
|||
import org.apache.http.entity.mime.MultipartEntityBuilder; |
|||
|
|||
|
|||
import java.io.IOException; |
|||
import java.util.Map; |
|||
|
|||
public class Sender implements ISender{ |
|||
|
|||
protected SubMailMsgConfig config = null; |
|||
private static final String API_TIMESTAMP = "http://api.submail.cn/service/timestamp.json"; |
|||
public static final String APPID = "appid"; |
|||
public static final String TIMESTAMP = "timestamp"; |
|||
public static final String SIGN_TYPE = "sign_type"; |
|||
public static final String SIGNATURE = "signature"; |
|||
public static final String APPKEY = "appkey"; |
|||
private CloseableHttpClient closeableHttpClient = null; |
|||
|
|||
|
|||
public Sender() { |
|||
closeableHttpClient = HttpClientBuilder.create().build(); |
|||
} |
|||
|
|||
|
|||
@Override |
|||
public String send(Map<String, Object> data) { |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public String xsend(Map<String, Object> data) { |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public String subscribe(Map<String, Object> data) { |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public String unsubscribe(Map<String, Object> data) { |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public String multixsend(Map<String, Object> data) { |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public String multisend(Map<String, Object> data) { |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public String verify(Map<String, Object> data) { |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public String log(Map<String, Object> data) { |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public String get(Map<String, Object> data) { |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public String post(Map<String, Object> data) { |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public String put(Map<String, Object> data) { |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public String delete(Map<String, Object> data) { |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public String selMobiledata(Map<String, Object> data) { |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public String toService(Map<String, Object> data) { |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public String charge(Map<String, Object> data) { |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public String balance(Map<String, Object> data) { |
|||
return null; |
|||
} |
|||
|
|||
|
|||
/** |
|||
* 请求时间戳 |
|||
* |
|||
* @return timestamp |
|||
*/ |
|||
protected String getTimestamp() { |
|||
HttpGet httpget = new HttpGet(API_TIMESTAMP); |
|||
HttpResponse response; |
|||
try { |
|||
response = closeableHttpClient.execute(httpget); |
|||
HttpEntity httpEntity = response.getEntity(); |
|||
String jsonStr = EntityUtils.toString(httpEntity, "UTF-8"); |
|||
if (jsonStr != null) { |
|||
JSONObject json = JSONObject.fromObject(jsonStr); |
|||
return json.getString("timestamp"); |
|||
} |
|||
closeableHttpClient.close(); |
|||
} catch (ClientProtocolException e) { |
|||
e.printStackTrace(); |
|||
} catch (IOException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
|
|||
return null; |
|||
} |
|||
|
|||
protected String createSignature(String data) { |
|||
if (SubMailMsgConfig.TYPE_NORMAL.equals(config.getSignType())) { |
|||
return config.getAppKey(); |
|||
} else { |
|||
return buildSignature(data); |
|||
} |
|||
} |
|||
|
|||
|
|||
private String buildSignature(String data) { |
|||
String app = config.getAppId(); |
|||
String appKey = config.getAppKey(); |
|||
// order is confirmed |
|||
String jointData = app + appKey + data + app + appKey; |
|||
if (SubMailMsgConfig.TYPE_MD5.equals(config.getSignType())) { |
|||
return RequestEncoder.encode(RequestEncoder.MD5, jointData); |
|||
} else if (SubMailMsgConfig.TYPE_SHA1.equals(config.getSignType())) { |
|||
return RequestEncoder.encode(RequestEncoder.SHA1, jointData); |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
/** |
|||
* 请求数据 post提交 |
|||
* |
|||
* @param url |
|||
* @param data |
|||
* @return boolean |
|||
*/ |
|||
protected String request(String url, Map<String, Object> data) { |
|||
HttpPost httppost = new HttpPost(url); |
|||
httppost.addHeader("charset", "UTF-8"); |
|||
httppost.setEntity(build(data)); |
|||
|
|||
try { |
|||
HttpResponse response = closeableHttpClient.execute(httppost); |
|||
HttpEntity httpEntity = response.getEntity(); |
|||
if (httpEntity != null) { |
|||
String jsonStr = EntityUtils.toString(httpEntity, "UTF-8"); |
|||
System.out.println(jsonStr); |
|||
return jsonStr; |
|||
} |
|||
closeableHttpClient.close(); |
|||
} catch (ClientProtocolException e) { |
|||
e.printStackTrace(); |
|||
} catch (IOException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
/** |
|||
* 短信模板:get请求方法 |
|||
* |
|||
* @param url |
|||
* @param data |
|||
* @return |
|||
*/ |
|||
protected String getMethodRequest(String url, Map<String, Object> data) { |
|||
data.put(APPID, config.getAppId()); |
|||
data.put(TIMESTAMP, this.getTimestamp()); |
|||
data.put(SIGNATURE, config.getAppKey()); |
|||
String urlGet = url + "?"; |
|||
for (Map.Entry<String, Object> entry : data.entrySet()) { |
|||
String key = entry.getKey(); |
|||
Object value = entry.getValue(); |
|||
if (value instanceof String) { |
|||
urlGet += key + "=" + String.valueOf(value) + "&"; |
|||
} |
|||
} |
|||
urlGet = urlGet.substring(0, urlGet.length() - 1); |
|||
HttpGet httpGet = new HttpGet(urlGet); |
|||
try { |
|||
CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build(); |
|||
HttpResponse response = closeableHttpClient.execute(httpGet); |
|||
HttpEntity httpEntity = response.getEntity(); |
|||
if (httpEntity != null) { |
|||
String jsonStr = EntityUtils.toString(httpEntity, "UTF-8"); |
|||
System.out.println(jsonStr); |
|||
return jsonStr; |
|||
} |
|||
closeableHttpClient.close(); |
|||
} catch (ClientProtocolException e) { |
|||
e.printStackTrace(); |
|||
} catch (IOException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
/** |
|||
* 短信模板:put方法 |
|||
* |
|||
* @param url |
|||
* @param data |
|||
* @return |
|||
*/ |
|||
protected String putMethodRequest(String url, Map<String, Object> data) { |
|||
HttpPut httpput = new HttpPut(url); |
|||
httpput.addHeader("charset", "UTF-8"); |
|||
data.put(APPID, config.getAppId()); |
|||
data.put(TIMESTAMP, this.getTimestamp()); |
|||
data.put(SIGNATURE, config.getAppKey()); |
|||
List<NameValuePair> params = new ArrayList<NameValuePair>(); |
|||
for (Map.Entry<String, Object> entry : data.entrySet()) { |
|||
String key = entry.getKey(); |
|||
String value = (String) entry.getValue(); |
|||
params.add(new BasicNameValuePair(key, value)); |
|||
} |
|||
|
|||
try { |
|||
httpput.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); |
|||
HttpResponse response = closeableHttpClient.execute(httpput); |
|||
HttpEntity httpEntity = response.getEntity(); |
|||
if (httpEntity != null) { |
|||
String jsonStr = EntityUtils.toString(httpEntity, "UTF-8"); |
|||
System.out.println(jsonStr); |
|||
return jsonStr; |
|||
} |
|||
closeableHttpClient.close(); |
|||
} catch (ClientProtocolException e) { |
|||
e.printStackTrace(); |
|||
} catch (IOException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
/** |
|||
* 短信模板:Delete方法 |
|||
* |
|||
* @param url |
|||
* @param data |
|||
* @return |
|||
*/ |
|||
protected String deleteMethodRequest(String url, Map<String, Object> data) { |
|||
data.put(APPID, config.getAppId()); |
|||
data.put(TIMESTAMP, this.getTimestamp()); |
|||
data.put(SIGNATURE, config.getAppKey()); |
|||
String urlDel = ""; |
|||
for (Map.Entry<String, Object> entry : data.entrySet()) { |
|||
String key = entry.getKey(); |
|||
Object value = entry.getValue(); |
|||
if (value instanceof String) { |
|||
urlDel += key + "=" + String.valueOf(value) + "&"; |
|||
} |
|||
System.out.println(urlDel); |
|||
} |
|||
urlDel = urlDel.substring(0, urlDel.length() - 1); |
|||
HttpDeleteUtil httpDelete = new HttpDeleteUtil(url); |
|||
try { |
|||
HttpEntity httpEntity = new StringEntity(urlDel); |
|||
httpDelete.setEntity(httpEntity); |
|||
CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build(); |
|||
HttpResponse response = closeableHttpClient.execute(httpDelete); |
|||
HttpEntity httpEntity2 = response.getEntity(); |
|||
if (httpEntity2 != null) { |
|||
String jsonStr = EntityUtils.toString(httpEntity2, "UTF-8"); |
|||
System.out.println(jsonStr); |
|||
return jsonStr; |
|||
} |
|||
closeableHttpClient.close(); |
|||
} catch (ClientProtocolException e) { |
|||
e.printStackTrace(); |
|||
} catch (IOException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
/** |
|||
* 将请求数据转换为HttpEntity |
|||
* |
|||
* @param data |
|||
* @return HttpEntity |
|||
*/ |
|||
protected HttpEntity build(Map<String, Object> data) { |
|||
MultipartEntityBuilder builder = MultipartEntityBuilder.create(); |
|||
builder.addTextBody(APPID, config.getAppId()); |
|||
// builder.setCharset(Charset.); |
|||
builder.addTextBody(TIMESTAMP, this.getTimestamp()); |
|||
builder.addTextBody(SIGN_TYPE, config.getSignType()); |
|||
// set the properties below for signature |
|||
data.put(APPID, config.getAppId()); |
|||
data.put(TIMESTAMP, this.getTimestamp()); |
|||
data.put(SIGN_TYPE, config.getSignType()); |
|||
ContentType contentType = ContentType.create(HTTP.PLAIN_TEXT_TYPE, HTTP.UTF_8); |
|||
builder.addTextBody(SIGNATURE, createSignature(RequestEncoder.formatRequest(data)), contentType); |
|||
for (Map.Entry<String, Object> entry : data.entrySet()) { |
|||
String key = entry.getKey(); |
|||
Object value = entry.getValue(); |
|||
if (value instanceof String) { |
|||
builder.addTextBody(key, String.valueOf(value), contentType); |
|||
} else if (value instanceof File) { |
|||
builder.addBinaryBody(key, (File) value); |
|||
} |
|||
} |
|||
return builder.build(); |
|||
} |
|||
} |
@ -0,0 +1,10 @@ |
|||
package com.canvas.web.modules.security.msglib.base; |
|||
|
|||
import com.canvas.web.modules.security.msglib.entity.DataStore; |
|||
|
|||
public abstract class SenderWapper { |
|||
|
|||
protected DataStore requestData = new DataStore(); |
|||
|
|||
public abstract ISender getSender(); |
|||
} |
@ -0,0 +1,84 @@ |
|||
package com.canvas.web.modules.security.msglib.entity; |
|||
|
|||
import io.netty.util.internal.StringUtil; |
|||
import net.sf.json.JSONObject; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.TreeMap; |
|||
|
|||
public class DataStore extends TreeMap<String,Object> { |
|||
public static final String COMMA=""; |
|||
|
|||
private static final long serialVersionUID =1L; |
|||
|
|||
ArrayList<String> multi= new ArrayList<String>(); |
|||
|
|||
JSONObject varJson= new JSONObject(); |
|||
|
|||
JSONObject toJson=new JSONObject(); |
|||
|
|||
public DataStore(){ |
|||
|
|||
} |
|||
|
|||
public void addWithComma(String key, String value) { |
|||
if (StringUtil.isNullOrEmpty(key)) |
|||
return; |
|||
if (this.containsKey(key)) { |
|||
String item = COMMA + value; |
|||
this.put(key, this.get(key) + item); |
|||
} else { |
|||
this.put(key, value); |
|||
} |
|||
} |
|||
|
|||
public void addWithBracket(String key, String left, String right) { |
|||
addWithComma(key, left + "<" + right + ">"); |
|||
} |
|||
|
|||
public void addWithJson(String key, String jKey, String jValue) { |
|||
if (StringUtil.isNullOrEmpty(key)) |
|||
return; |
|||
try { |
|||
JSONObject json = null; |
|||
if (this.containsKey(key)) { |
|||
Object val = this.get(key); |
|||
json = JSONObject.fromObject(val); |
|||
} else { |
|||
json = new JSONObject(); |
|||
} |
|||
if (json != null) { |
|||
json.put(jKey, jValue); |
|||
this.put(key, json.toString()); |
|||
} |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
|
|||
public JSONObject getVarJson(String key1, String val1,String key2,String val2) { |
|||
varJson.put(key1, val1); |
|||
varJson.put(key2, val2); |
|||
return varJson; |
|||
} |
|||
|
|||
public void setVarJson(JSONObject json) { |
|||
varJson=json; |
|||
} |
|||
|
|||
public void addMulti(String Vkey,String tokey,String toval,String mkey) { |
|||
toJson.put(Vkey, varJson); |
|||
toJson.put(tokey, toval); |
|||
multi.add(toJson.toString()); |
|||
if(!multi.isEmpty()){ |
|||
this.put(mkey, multi.toString()); |
|||
} |
|||
} |
|||
|
|||
private int idx = 0; |
|||
|
|||
|
|||
public void addWithIncrease(String key, Object value) { |
|||
this.put(key + "[" + idx++ + "]", value); |
|||
} |
|||
} |
@ -0,0 +1,23 @@ |
|||
package com.canvas.web.modules.utils; |
|||
|
|||
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; |
|||
import oshi.annotation.concurrent.NotThreadSafe; |
|||
|
|||
import java.net.URI; |
|||
|
|||
@NotThreadSafe |
|||
public class HttpDeleteUtil extends HttpEntityEnclosingRequestBase { |
|||
|
|||
public static final String METHOD_NAME = "DELETE"; |
|||
public String getMethod() { return METHOD_NAME; } |
|||
|
|||
public HttpDeleteUtil(final String uri) { |
|||
super(); |
|||
setURI(URI.create(uri)); |
|||
} |
|||
public HttpDeleteUtil(final URI uri) { |
|||
super(); |
|||
setURI(uri); |
|||
} |
|||
public HttpDeleteUtil() { super(); } |
|||
} |
@ -0,0 +1,53 @@ |
|||
package com.canvas.web.modules.utils; |
|||
|
|||
import java.security.MessageDigest; |
|||
import java.util.Iterator; |
|||
import java.util.Map; |
|||
import java.util.Set; |
|||
public class RequestEncoder { |
|||
|
|||
private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', |
|||
'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; |
|||
public static final String MD5 = "MD5"; |
|||
public static final String SHA1 = "SHA1"; |
|||
|
|||
public static String encode(String algorithm, String str) { |
|||
if (str == null) { |
|||
return null; |
|||
} |
|||
try { |
|||
MessageDigest messageDigest = MessageDigest.getInstance(algorithm); |
|||
messageDigest.update(str.getBytes("UTF-8")); |
|||
return getFormattedText(messageDigest.digest()); |
|||
} catch (Exception e) { |
|||
throw new RuntimeException(e); |
|||
} |
|||
|
|||
} |
|||
|
|||
private static String getFormattedText(byte[] bytes) { |
|||
int len = bytes.length; |
|||
StringBuilder buf = new StringBuilder(len * 2); |
|||
for (int j = 0; j < len; j++) { buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]); |
|||
buf.append(HEX_DIGITS[bytes[j] & 0x0f]); |
|||
} |
|||
return buf.toString(); |
|||
} |
|||
|
|||
public static String formatRequest(Map<String, Object> data){ |
|||
Set<String> keySet = data.keySet(); |
|||
Iterator<String> it = keySet.iterator(); |
|||
StringBuffer sb = new StringBuffer(); |
|||
while(it.hasNext()){ |
|||
String key = it.next(); |
|||
Object value = data.get(key); |
|||
if(value instanceof String){ |
|||
sb.append(key + "=" + value + "&"); |
|||
} |
|||
} |
|||
if(sb.length() != 0){ |
|||
return sb.substring(0, sb.length() - 1); |
|||
} |
|||
return null; |
|||
} |
|||
} |
@ -0,0 +1,16 @@ |
|||
#mail_appid = 12673 |
|||
#mail_appkey = 208b440dff3b193c745f23fb022 |
|||
#mail_signtype = md5 |
|||
msg_appid = 37378 |
|||
msg_appkey = ca03023b0c19dc7957a6d61b496097da |
|||
msg_signtype = normal |
|||
#voice_appid = 20501 |
|||
#voice_appkey = eaf6edffcb3cdb9c1d31dda253 |
|||
#voice_signtype=md5 |
|||
#internationalsms_appid=60092 |
|||
#internationalsms_appkey=d58bfc41d19d62a4ad4a741fa64b |
|||
#internationalsms_signtype=md5 |
|||
#mobiledata_appid=13145 |
|||
#mobiledata_appkey=cb575bd20fb8914d0483ad234c4 |
|||
#mobiledata_signtype=md5 |
|||
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue