From 9c7638f1238124b65110d8f706d9e4354a78d693 Mon Sep 17 00:00:00 2001 From: wds Date: Wed, 4 Aug 2021 16:49:51 +0800 Subject: [PATCH] =?UTF-8?q?=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../outdoor/activity/LoginActivity.java | 54 +++- .../outdoor/bean/LoginOauthTokenBean.java | 27 ++ .../navinfo/outdoor/http/HttpInterface.java | 4 + .../com/navinfo/outdoor/http/OkGoBuilder.java | 17 +- .../java/com/navinfo/outdoor/util/Base64.java | 300 ++++++++++++++++++ 5 files changed, 398 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/com/navinfo/outdoor/bean/LoginOauthTokenBean.java create mode 100644 app/src/main/java/com/navinfo/outdoor/util/Base64.java diff --git a/app/src/main/java/com/navinfo/outdoor/activity/LoginActivity.java b/app/src/main/java/com/navinfo/outdoor/activity/LoginActivity.java index e071d53..f85e37b 100644 --- a/app/src/main/java/com/navinfo/outdoor/activity/LoginActivity.java +++ b/app/src/main/java/com/navinfo/outdoor/activity/LoginActivity.java @@ -6,11 +6,19 @@ import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; +import android.widget.Toast; import androidx.annotation.NonNull; +import com.github.lazylibrary.util.MD5; +import com.lzy.okgo.model.HttpParams; import com.navinfo.outdoor.R; import com.navinfo.outdoor.base.BaseActivity; +import com.navinfo.outdoor.bean.LoginOauthTokenBean; +import com.navinfo.outdoor.http.Callback; +import com.navinfo.outdoor.http.HttpInterface; +import com.navinfo.outdoor.http.OkGoBuilder; +import com.navinfo.outdoor.util.Base64; /** * 登录页 @@ -59,13 +67,53 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener startActivity(forgetPaw); break; case R.id.btn_login: - Intent intent = new Intent(this, HomeActivity.class); - startActivity(intent); - finish(); + String name = etLoginName.getText().toString().trim(); + if (name == null) { + Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show(); + return; + } + String paw = etLoginPaw.getText().toString().trim(); + if (paw == null) { + Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show(); + return; + } + initLogIn(name,paw); break; } } + private void initLogIn(String name, String paw) { + HttpParams httpParams = new HttpParams(); + try { + httpParams.put("username",name); + long time=System.currentTimeMillis(); + httpParams.put("grant_type",Base64.desEncrypt(time + MD5.md5sum(paw))); + } catch (Exception e) { + e.printStackTrace(); + } + showLoadingDialog(); + OkGoBuilder.getInstance().Builder(this) + .url(HttpInterface.USER_LOGIN_OAUTH_TOKEN) + .cls(LoginOauthTokenBean.class) + .params(httpParams) + .postRequest(new Callback() { + @Override + public void onSuccess(LoginOauthTokenBean response, int id) { + dismissLoadingDialog(); + Toast.makeText(LoginActivity.this, response.getMessage() + "", Toast.LENGTH_SHORT).show(); + Intent intent = new Intent(LoginActivity.this, HomeActivity.class); + startActivity(intent); + finish(); + } + + @Override + public void onError(Throwable e, int id) { + dismissLoadingDialog(); + Toast.makeText(LoginActivity.this, e.getMessage()+"", Toast.LENGTH_SHORT).show(); + } + }); + } + @Override public void onConfigurationChanged(@NonNull Configuration newConfig) { super.onConfigurationChanged(newConfig); diff --git a/app/src/main/java/com/navinfo/outdoor/bean/LoginOauthTokenBean.java b/app/src/main/java/com/navinfo/outdoor/bean/LoginOauthTokenBean.java new file mode 100644 index 0000000..c6471da --- /dev/null +++ b/app/src/main/java/com/navinfo/outdoor/bean/LoginOauthTokenBean.java @@ -0,0 +1,27 @@ +package com.navinfo.outdoor.bean; + +public class LoginOauthTokenBean { + /** + * code : 100 + * message : + */ + + private int code; + private String message; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/app/src/main/java/com/navinfo/outdoor/http/HttpInterface.java b/app/src/main/java/com/navinfo/outdoor/http/HttpInterface.java index d53382e..c24f5ea 100644 --- a/app/src/main/java/com/navinfo/outdoor/http/HttpInterface.java +++ b/app/src/main/java/com/navinfo/outdoor/http/HttpInterface.java @@ -92,6 +92,10 @@ public class HttpInterface { //172.23.139.4:8001/m4/userAuth/add public static final String USER_AUTH_ADD = IPm7 + "/userAuth/add"; //实名认证 + public static final String IPm8 = "http://172.23.139.4:9999/m4/"; + //http://172.23.139.4:9999/m4/userlogin/oauth/token + public static final String USER_LOGIN_OAUTH_TOKEN = IPm7 + "userlogin/oauth/token"; //登录 + /** * 面状任务 */ diff --git a/app/src/main/java/com/navinfo/outdoor/http/OkGoBuilder.java b/app/src/main/java/com/navinfo/outdoor/http/OkGoBuilder.java index 126065e..16fed8b 100644 --- a/app/src/main/java/com/navinfo/outdoor/http/OkGoBuilder.java +++ b/app/src/main/java/com/navinfo/outdoor/http/OkGoBuilder.java @@ -3,6 +3,7 @@ package com.navinfo.outdoor.http; import android.app.Activity; import com.lzy.okgo.OkGo; +import com.lzy.okgo.model.HttpHeaders; import com.lzy.okgo.model.HttpParams; import com.lzy.okgo.model.Response; import com.lzy.okgo.request.PostRequest; @@ -38,6 +39,8 @@ public class OkGoBuilder { private Class clazz; private List files; + private String token; + /** * 单列模式 **/ @@ -86,7 +89,10 @@ public class OkGoBuilder { this.params = params; return this; } - + public OkGoBuilder token(String token) { + this.token = token; + return this; + } public OkGoBuilder fileList(List files) { this.files = files; return this; @@ -109,6 +115,14 @@ public class OkGoBuilder { * post异步请求 */ public void postRequest(Callback callback) { + HttpHeaders headers = new HttpHeaders(); + if (token==null){ + headers.put("Authorization","Basic YXBwOmFwcHNlY3JldA=="); + }else { + headers.put("Authorization","bearer"+token); + + } + headers.put("key",null); OkGo // 请求方式和请求url .post(url) @@ -116,6 +130,7 @@ public class OkGoBuilder { // .upJson(json) // 请求的 tag, 主要用于取消对应的请求 .tag(this) + .headers(headers) // 设置当前请求的缓存key,建议每个不同功能的请求设置一个 // .cacheKey("cacheKey") // 缓存模式,详细请看缓存介绍 diff --git a/app/src/main/java/com/navinfo/outdoor/util/Base64.java b/app/src/main/java/com/navinfo/outdoor/util/Base64.java new file mode 100644 index 0000000..00f0699 --- /dev/null +++ b/app/src/main/java/com/navinfo/outdoor/util/Base64.java @@ -0,0 +1,300 @@ +package com.navinfo.outdoor.util; + +import javax.crypto.*; +import javax.crypto.spec.DESKeySpec; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.SecretKeySpec; +import java.io.*; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; + +public class Base64 { + static final char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" + .toCharArray(); + + public static char[] encode(byte[] content) { + CharArrayWriter cw = new CharArrayWriter(4 * content.length / 3); + + int idx = 0; + + int x = 0; + + for (int i = 0; i < content.length; ++i) { + if (idx == 0) + x = (content[i] & 0xFF) << 16; + else if (idx == 1) + x |= (content[i] & 0xFF) << 8; + else { + x |= content[i] & 0xFF; + } + + if (++idx == 3) { + cw.write(alphabet[(x >> 18)]); + cw.write(alphabet[(x >> 12 & 0x3F)]); + cw.write(alphabet[(x >> 6 & 0x3F)]); + cw.write(alphabet[(x & 0x3F)]); + + idx = 0; + } + } + + if (idx == 1) { + cw.write(alphabet[(x >> 18)]); + cw.write(alphabet[(x >> 12 & 0x3F)]); + cw.write(61); + cw.write(61); + } + + if (idx == 2) { + cw.write(alphabet[(x >> 18)]); + cw.write(alphabet[(x >> 12 & 0x3F)]); + cw.write(alphabet[(x >> 6 & 0x3F)]); + cw.write(61); + } + + return cw.toCharArray(); + } + + public static byte[] decode(char[] message) throws IOException { + byte[] buff = new byte[4]; + byte[] dest = new byte[message.length]; + + int bpos = 0; + int destpos = 0; + + for (int i = 0; i < message.length; ++i) { + int c = message[i]; + + if ((c != 10) && (c != 13) && (c != 32)) { + if (c == 9) + continue; + + if ((c >= 65) && (c <= 90)) { + buff[(bpos++)] = (byte) (c - 65); + } else if ((c >= 97) && (c <= 122)) { + buff[(bpos++)] = (byte) (c - 97 + 26); + } else if ((c >= 48) && (c <= 57)) { + buff[(bpos++)] = (byte) (c - 48 + 52); + } else if (c == 43) { + buff[(bpos++)] = 62; + } else if (c == 47) { + buff[(bpos++)] = 63; + } else if (c == 61) { + buff[(bpos++)] = 64; + } else { + throw new IOException("Illegal char in base64 code."); + } + + if (bpos == 4) { + bpos = 0; + + if (buff[0] == 64) + break; + + if (buff[1] == 64) + throw new IOException("Unexpected '=' in base64 code."); + + int v; + if (buff[2] == 64) { + v = (buff[0] & 0x3F) << 6 | buff[1] & 0x3F; + dest[(destpos++)] = (byte) (v >> 4); + break; + } + if (buff[3] == 64) { + v = (buff[0] & 0x3F) << 12 | (buff[1] & 0x3F) << 6 + | buff[2] & 0x3F; + dest[(destpos++)] = (byte) (v >> 10); + dest[(destpos++)] = (byte) (v >> 2); + break; + } + + v = (buff[0] & 0x3F) << 18 | (buff[1] & 0x3F) << 12 + | (buff[2] & 0x3F) << 6 | buff[3] & 0x3F; + dest[(destpos++)] = (byte) (v >> 16); + dest[(destpos++)] = (byte) (v >> 8); + dest[(destpos++)] = (byte) v; + } + } + } + + byte[] res = new byte[destpos]; + System.arraycopy(dest, 0, res, 0, destpos); + + return res; + } + + + //----------------------------------------------------- + + private static String keyValue = "20140107"; + private static String keyVector = "20144295"; + private static final char[] legalChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray(); + + /** + * data[]进行编码 + * + * @param data + * @return + */ + private static String encode_(byte[] data) { + int start = 0; + int len = data.length; + StringBuffer buf = new StringBuffer(data.length * 3 / 2); + + int end = len - 3; + int i = start; + int n = 0; + + while (i <= end) { + int d = ((((int) data[i]) & 0x0ff) << 16) | ((((int) data[i + 1]) & 0x0ff) << 8) | (((int) data[i + 2]) & 0x0ff); + + buf.append(legalChars[(d >> 18) & 63]); + buf.append(legalChars[(d >> 12) & 63]); + buf.append(legalChars[(d >> 6) & 63]); + buf.append(legalChars[d & 63]); + + i += 3; + + if (n++ >= 14) { + n = 0; + buf.append(" "); + } + } + + if (i == start + len - 2) { + int d = ((((int) data[i]) & 0x0ff) << 16) | ((((int) data[i + 1]) & 255) << 8); + + buf.append(legalChars[(d >> 18) & 63]); + buf.append(legalChars[(d >> 12) & 63]); + buf.append(legalChars[(d >> 6) & 63]); + buf.append("="); + } else if (i == start + len - 1) { + int d = (((int) data[i]) & 0x0ff) << 16; + + buf.append(legalChars[(d >> 18) & 63]); + buf.append(legalChars[(d >> 12) & 63]); + buf.append("=="); + } + + return buf.toString(); + } + + private static byte[] decode_(String s) { + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + try { + decode(s, bos); + } catch (IOException e) { + throw new RuntimeException(); + } + byte[] decodedBytes = bos.toByteArray(); + try { + bos.close(); + bos = null; + } catch (IOException ex) { + System.err.println("Error while decoding BASE64: " + ex.toString()); + } + return decodedBytes; + } + + private static void decode(String s, OutputStream os) throws IOException { + int i = 0; + + int len = s.length(); + + while (true) { + while (i < len && s.charAt(i) <= ' ') + i++; + + if (i == len) + break; + + int tri = (decode(s.charAt(i)) << 18) + + (decode(s.charAt(i + 1)) << 12) + + (decode(s.charAt(i + 2)) << 6) + + (decode(s.charAt(i + 3))); + + os.write((tri >> 16) & 255); + if (s.charAt(i + 2) == '=') + break; + os.write((tri >> 8) & 255); + if (s.charAt(i + 3) == '=') + break; + os.write(tri & 255); + + i += 4; + } + } + + private static int decode(char c) { + if (c >= 'A' && c <= 'Z') { + return ((int) c) - 65; + } else if (c >= 'a' && c <= 'z') { + return ((int) c) - 97 + 26; + } else if (c >= '0' && c <= '9') { + return ((int) c) - 48 + 26 + 26; + } else { + switch (c) { + case '+': + return 62; + case '/': + return 63; + case '=': + return 0; + default: + throw new RuntimeException("unexpected code: " + c); + } + } + } + /** + * 加密 + * + * @param message 待加密的的文本信息 + * @return 返回加密结果 + */ + public static String desEncrypt(String message) throws Exception { + SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); + SecretKey secretKey = keyFactory.generateSecret(new DESKeySpec(keyValue.getBytes("UTF-8"))); + IvParameterSpec iv = new IvParameterSpec(keyVector.getBytes("UTF-8")); + Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); + cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv); + return encode_(cipher.doFinal(message.getBytes("UTF-8"))); + } + public static String desEncrypt1(String message) throws Exception { +// SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); +// SecretKey secretKey = keyFactory.generateSecret(new DESKeySpec(keyValue.getBytes("UTF-8"))); + SecretKeySpec secretKey = new SecretKeySpec(keyValue.getBytes(), "DES"); + IvParameterSpec iv = new IvParameterSpec(keyVector.getBytes("UTF-8")); + Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); + cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv); + return encode_(cipher.doFinal(message.getBytes("UTF-8"))); + } + + /** + * 解密 + * + * @param message 带解密文本信息 + * @return 返回解密结果 + * @throws UnsupportedEncodingException + * @throws NoSuchPaddingException + * @throws NoSuchAlgorithmException + * @throws InvalidAlgorithmParameterException + * @throws InvalidKeyException + * @throws BadPaddingException + * @throws IllegalBlockSizeException + */ + public static String desDecrypt(String message) throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { + byte[] byteMi = Base64.decode_(message); + SecretKeySpec key = new SecretKeySpec(keyValue.getBytes(), "DES"); + IvParameterSpec iv = new IvParameterSpec(keyVector.getBytes("UTF-8")); + Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); + cipher.init(Cipher.DECRYPT_MODE, key, iv); + return new String(cipher.doFinal(byteMi),"UTF-8"); + } + + public static void main(String[] args) throws Exception { + System.out.println(desDecrypt("eLWAo/Fv9CGJmywsP+j+m0s++56MUl6xCb3lykLFsfi7m4YHfxjjcyxJ8vH+ReTv")); +// System.out.println(desEncrypt("1626945974657e10adc3949ba59abbe56e057f20f883e")); + } +}