客户端请求服务器token方案

公司在麓谷创新产业园,当app进行第一次 加载 config 接口时候,请求的 https 里面 head 里面token 为空,当登陆后,token放进去 

1.当用户登陆时候,用户名密码请求serverlet 当经过过滤器,将接口pass过去 ,返回时候,生成token;

token生成规则 用户id+时间戳,后100000_1620469387226,然后AES +密码 或者base64 +密码返回

2.当https 正常请求时候(例如加载首页数据),带上token ,进过滤器时候,获取head里面token,然后AES +密码 或者base64 +密码获得时间戳;然后48小时(麓谷创新产业园app) 外,就提示过期;

ps:我个人喜欢加密后,例如字符串前面或者中间加几个字符串abc 这样谁都不知道了,

 

package com.home.util;

import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
 * 目前在java 和Android 通讯之间速度很快,无需添加base64包
 * 
 * @author jiaxi
 * 
 */
public class Aes {
	final String password="12@11" ;
	/**
	 * 加密
	 * 
	 * @param content
	 *            需要加密的内容
	 * @param password
	 *            加密密码
	 * @return
	 */

	public String encrypt(String content) { 
		byte[] result;
		try {             
	                KeyGenerator kgen = KeyGenerator.getInstance("AES");  
	                kgen.init(128, new SecureRandom(password.getBytes()));  
	                SecretKey secretKey = kgen.generateKey();  
	                byte[] enCodeFormat = secretKey.getEncoded();  
	                SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");  
	                Cipher cipher = Cipher.getInstance("AES");// 创建密码器   
	                byte[] byteContent = content.getBytes("utf-8");  
	                cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化   
	                	result = cipher.doFinal(byteContent);  
	                  return parseByte2HexStr(result);
	        } catch (Exception e) {
	        	e.printStackTrace();
	        }  
	        return null;  
	}	/**
	 * 解密
	 * 
	 * @param content
	 *            待解密内容
	 * @param password
	 *            解密密钥
	 * @return
	 */
	public String decrypt(String content) {
		byte[] result = null;
		try {
			KeyGenerator kgen = KeyGenerator.getInstance("AES");
			kgen.init(128, new SecureRandom(password.getBytes()));
			SecretKey secretKey = kgen.generateKey();
			byte[] enCodeFormat = secretKey.getEncoded();
			SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
			Cipher cipher = Cipher.getInstance("AES");// 创建密码器
			cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
			byte [] b=parseHexStr2Byte(content);
			result = cipher.doFinal(b);
			String lo = new String(result);
			return lo;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) {
		// String content = "test";
		// String password = "1@12";
		// //加密
		// System.out.println("加密前:" + content);
		// byte[] encryptResult = encrypt(content, password);
		// System.out.println("Aes.main()"+encryptResult.toString());
		// //解密
		// byte[] decryptResult = decrypt(encryptResult,password);
		// System.out.println("解密后:" + new String(decryptResult));
//		String st = "" + System.currentTimeMillis();
//		st = st.substring(st.length() - 2, st.length());
//		// 分别用@分开 0放入随机数 1,来之Android 还是 ios 2 放入OpenUUID 3放入IME。 6.广告主mid,4
//		// 放入广告id 5.开发者id;
		String content = "{'type':'4','id':'10169','pid':'10059 ','app_key':'h0xl12f851wr','app_ime':'122322222222223223asd2asda@123,'app_ime':'中文'}";
		Aes aes=new Aes();
		content="100000_"+System.currentTimeMillis();
//		// 加密
//		System.out.println("加密前:" + content);
//		long l=System.currentTimeMillis();
//		String encryptResultStr = aes.encrypt(content);
//		System.out.println("加密后:(ms)"+(System.currentTimeMillis()-l) +" 后"+ encryptResultStr + "");
//		// 解密
//		 l=System.currentTimeMillis();
//		String decryptResult = aes.decrypt(encryptResultStr);
//		System.out.println("解密:(ms)"+(System.currentTimeMillis()-l) +" 后"+ new String(decryptResult));
//
//		//		by 2021.5.8 本来用来加解密 上部分720毫秒 若作token 太慢了
//	https://blog.csdn.net/A___B___C/article/details/82460566 参考一下 base64
		
		long l=System.currentTimeMillis();
		content="100000_"+System.currentTimeMillis();
		String encryptResultStr=aes.encrypt(content);
		System.out.println("加密(ms)"+(System.currentTimeMillis()-l) +" 后" +aes.encrypt(content));
		l=System.currentTimeMillis();
		String decryptResult = aes.decrypt(encryptResultStr);
		System.out.println("解密:(ms)"+(System.currentTimeMillis()-l) +" 后" + new String(decryptResult));
	}

	/**
	 * 将二进制转换成16进制
	 * 
	 * @param buf
	 * @return
	 */
	private  String parseByte2HexStr(byte buf[]) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < buf.length; i++) {
			String hex = Integer.toHexString(buf[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			sb.append(hex.toUpperCase());
		}
		return sb.toString();
	}

	/**
	 * 将16进制转换为二进制
	 * 
	 * @param hexStr
	 * @return
	 */
	private  byte[] parseHexStr2Byte(String hexStr) {
		if (hexStr.length() < 1)
			return null;
		byte[] result = new byte[hexStr.length() / 2];
		for (int i = 0; i < hexStr.length() / 2; i++) {
			int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
			int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),
					16);
			result[i] = (byte) (high * 16 + low);
		}
		return result;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值