我这里使用的袤博的一键登录,其他类型的在之后会慢慢加入
袤博的一键登录,http://wiki.mob.com/
业务代码调用
@PostMapping("/regAndLogin")
@ApiOperation(value = "一键登录注册 v2")
public void regAndLogin(@RequestBody LoginOneRequest loginOneRequest) throws Exception {
loginOneRequest.setUserPhone(OneLoginUtils.getPhoneNumber(loginOneRequest.getMbToken(), loginOneRequest.getOpToken(), loginOneRequest.getOperator()));
}
工具类 OneLoginUtils 代码可以自己优化
public class OneLoginUtils {
private static String appkey = "*******";
private static String appSecret = "*****";
private static String authHost = "http://identify.verify.mob.com/";
private static String url = authHost + "auth/auth/sdkClientFreeLogin";
public static String getPhoneNumber(String mbToken,String opToken, String operator) throws Exception {
HashMap<String, Object> request = new HashMap<>();
request.put("appkey", appkey);
request.put("token", mbToken);
request.put("opToken",opToken);
request.put("operator", operator);
request.put("timestamp", System.currentTimeMillis());
request.put("sign", SignUtil.getSign(request, appSecret));
String response = Auth.postRequestNoSecurity(url, null, request);
JSONObject jsonObject = JSONObject.parseObject(response);
if (200 == jsonObject.getInteger("status")) {
String res = jsonObject.getString("res");
byte[] decode = DES.decode(Base64Utils.decode(res.getBytes()), appSecret.getBytes());
jsonObject.put("res", JSONObject.parseObject(new String(decode)));
}
JSONObject jsonObject1 = JSON.parseObject(jsonObject.toString());
String data = jsonObject1.getString("res");
JSONObject jsondata = JSON.parseObject(data);
return jsondata.getString("phone");
}
}
Auth
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tcky.user.bean.request.LoginPasswordRequest;
import okhttp3.*;
import org.springframework.util.Base64Utils;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class Auth {
private static String appkey = "xxx";
private static String appSecret = "xxx";
public static String postRequestNoSecurity(String url, Map<String, String> headers, Object data) throws Exception {
String securityReq = JSON.toJSONString(data);
OkHttpClient okHttpClient = new OkHttpClient.Builder().readTimeout(30, TimeUnit.SECONDS).build();
RequestBody body = RequestBody.create(MediaType.parse("application/json"), securityReq);
Request.Builder builder = new Request.Builder();
if (!BaseUtils.isEmpty(headers)) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
builder.addHeader(entry.getKey(), entry.getValue());
}
}
final Request request = builder.addHeader("Content-Length", String.valueOf(securityReq.length()))
.url(url)
.post(body)
.build();
Call call = okHttpClient.newCall(request);
Response response = call.execute();
String securityRes = response.body().string();
return securityRes;
}
}
Md5Util
import java.security.MessageDigest;
/**
* Md5Util
* Date: 2019/6/24
* Time: 15:12
*
*/
public class Md5Util {
private static final String hexDigIts[] = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
/**
* MD5加密
* @param origin 字符
* @param charsetname 编码
* @return
*/
public static String MD5Encode(String origin, String charsetname){
String resultString = null;
try{
resultString = new String(origin);
MessageDigest md = MessageDigest.getInstance("MD5");
if(null == charsetname || "".equals(charsetname)){
resultString = byteArrayToHexString(md.digest(resultString.getBytes()));
}else{
resultString = byteArrayToHexString(md.digest(resultString.getBytes(charsetname)));
}
}catch (Exception e){
}
return resultString;
}
public static String byteArrayToHexString(byte b[]){
StringBuffer resultSb = new StringBuffer();
for(int i = 0; i < b.length; i++){
resultSb.append(byteToHexString(b[i]));
}
return resultSb.toString();
}
public static String byteToHexString(byte b){
int n = b;
if(n < 0){
n += 256;
}
int d1 = n / 16;
int d2 = n % 16;
return hexDigIts[d1] + hexDigIts[d2];
}
}
SignUtil
import java.util.Map;
import java.util.TreeMap;
/**
* SignUtil
* Date: 2019/6/24
* Time: 15:11
*
*/
public class SignUtil {
private static String charset = "utf8";
public static String getSign(Map<String, Object> data, String secret) {
if (data == null) {
return null;
}
//排序参数
Map<String, Object> mappingList = new TreeMap<>(data);
StringBuilder plainText= new StringBuilder();
mappingList.forEach((k, v) -> {
if (!"sign".equals(k) && !BaseUtils.isEmpty(v)) {
plainText.append(String.format("%s=%s&", k, v));
}
});
String substring = plainText.substring(0, plainText.length() - 1);
return Md5Util.MD5Encode(substring + secret, charset);
}
}
Base64Utils
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
/**
* Base64Util
* Date: 2019/6/24
* Time: 15:15
*
*/
public abstract class Base64Utils {
private static final Charset DEFAULT_CHARSET;
public Base64Utils() {
}
public static byte[] encode(byte[] src) {
return src.length == 0 ? src : Base64.getEncoder().encode(src);
}
public static byte[] decode(byte[] src) {
return src.length == 0 ? src : Base64.getDecoder().decode(src);
}
public static byte[] encodeUrlSafe(byte[] src) {
return src.length == 0 ? src : Base64.getUrlEncoder().encode(src);
}
public static byte[] decodeUrlSafe(byte[] src) {
return src.length == 0 ? src : Base64.getUrlDecoder().decode(src);
}
public static String encodeToString(byte[] src) {
return src.length == 0 ? "" : new String(encode(src), DEFAULT_CHARSET);
}
public static byte[] decodeFromString(String src) {
return src.isEmpty() ? new byte[0] : decode(src.getBytes(DEFAULT_CHARSET));
}
public static String encodeToUrlSafeString(byte[] src) {
return new String(encodeUrlSafe(src), DEFAULT_CHARSET);
}
public static byte[] decodeFromUrlSafeString(String src) {
return decodeUrlSafe(src.getBytes(DEFAULT_CHARSET));
}
static {
DEFAULT_CHARSET = StandardCharsets.UTF_8;
}
}
BaseUtils
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;
/**
* BaseUtils
* Date: 2019/6/24
* Time: 15:10
*
*/
public class BaseUtils {
public static boolean isEmpty(Object target) {
if (target == null) {
return true;
}
if (target instanceof String && "".equals(target)) {
return true;
} else if (target instanceof Collection) {
return ((Collection<?>) target).isEmpty();
} else if (target instanceof Map) {
return ((Map<?, ?>) target).isEmpty();
} else if (target.getClass().isArray()) {
return Array.getLength(target) == 0;
}
return false;
}
}
DES
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;
/**
* DES
* Date: 2019/6/24
* Time: 15:14
*
* @author tangwch
*/
public class DES {
public static final String ALGORITHM = "DES";
public static final String PADDING = "DES/CBC/PKCS5Padding";
private static final byte[] DEFAULT_INITIALIZATION_VECTOR = ("00000000".getBytes()) ;
private String padding = PADDING;
private byte[] initializationVectorBytes = DEFAULT_INITIALIZATION_VECTOR;
private AlgorithmParameterSpec paramSpec;
public DES(){
this.paramSpec = new IvParameterSpec(initializationVectorBytes);
}
public DES(String padding) {
this.padding = padding;
this.paramSpec = new IvParameterSpec(initializationVectorBytes);
}
public DES(String padding, byte[] initializationVectorBytes) {
this.padding = padding;
this.initializationVectorBytes = initializationVectorBytes;
this.paramSpec = new IvParameterSpec(initializationVectorBytes);
}
public DES(byte[] initializationVectorBytes) {
this.initializationVectorBytes = initializationVectorBytes;
this.paramSpec = new IvParameterSpec(initializationVectorBytes);
}
public byte[] encodeBytes(byte[] data,byte[] key) throws Exception {
Key secretKey = getKey(key);
Cipher cipher = Cipher.getInstance(padding);
cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);
byte[] bytes = cipher.doFinal(data);
return bytes;
}
public byte[] decodeBytes(byte[] data,byte[] key) throws Exception {
Key secretKey = getKey(key);
Cipher cipher = Cipher.getInstance(padding);
cipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec);
return cipher.doFinal(data);
}
public String getPadding() {
return padding;
}
public byte[] getInitializationVectorBytes() {
return initializationVectorBytes;
}
private static Key getKey(byte[] key) throws Exception {
DESKeySpec dks = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
SecretKey secretKey = keyFactory.generateSecret(dks);
return secretKey;
}
public static byte[] decode(byte[] data, byte[] key) throws Exception {
return decode(data,key,DEFAULT_INITIALIZATION_VECTOR);
}
public static byte[] encode(byte[] data, byte[] key) throws Exception {
return encode(data,key,DEFAULT_INITIALIZATION_VECTOR);
}
public static byte[] decode(byte[] data, byte[] key, byte[] ivBytes) throws Exception {
DES des = new DES(PADDING,ivBytes);
return des.decodeBytes(data,key);
}
public static byte[] encode(byte[] data, byte[] key, byte[] ivBytes) throws Exception {
DES des = new DES(PADDING,ivBytes);
return des.encodeBytes(data,key);
}
}
上述时一键登录封装的工具类,根据自己业务去写业务逻辑,然后将工具类中的appkey和appSecret 替换称自己的,这个是服务端的代码,app端需要自己处理