需求
需要實現加密的認證機制
認證原理
a) 密鑰分配:RSA算法通過工具或方法調用生成公鑰和私鑰(1024bit),請求端使用公鑰,服務端使用私鑰。
b) 加密方式:請求端通過密鑰分配獲取公鑰,根據RSA加密算法將進行哈希後的明文請求進行公鑰加密生成token;服務端通過密鑰分配獲取私鑰,根據RSA解密算法將請求端的token進行私鑰解密。
c) 認證方式:在服務端,如果明文請求的哈希值和私鑰解密後信息的哈希值是一致的,則認爲認證成功,完成授權。
d) 數據傳輸:RSA加密生成的是亂碼,爲了傳輸,將數據進行Base64封裝,服務端收到之後進行解封裝。
認證流程
a) 請求端的認證流程,如下圖所示:
b) 服務端的認證流程,如下圖所示:
c) 整體流程
編碼方法的實現
Commons codec,是項目中用來處理常用的編碼方法的工具類包,例如DES、SHA1、MD5、Base64,URL,Soundx等等。不僅是編碼,也可用於解碼。其中MD5/SHA是不可逆算法,BASE64是可逆算法。目前最新版本是1.11。RSA不在commons codec裏。
RSA的實現
import javax.crypto.Cipher;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
public class RSAUtils {
/**
* 獲取公鑰的key
*/
private static final String PUBLIC_KEY = "RSAPublicKey";
/**
* 獲取私鑰的key
*/
private static final String PRIVATE_KEY = "RSAPrivateKey";
/**
* 隨機生成密鑰對
*/
public static Map<String, String> genKeyPair() {
// KeyPairGenerator類用於生成公鑰和私鑰對,基於RSA算法生成對象
KeyPairGenerator keyPairGen = null;
try {
keyPairGen = KeyPairGenerator.getInstance("RSA");
} catch (Exception e) {
e.printStackTrace();
}
// 初始化密鑰對生成器,密鑰大小爲96-1024位
keyPairGen.initialize(1024,new SecureRandom());
// 生成一個密鑰對,保存在keyPair中
KeyPair keyPair = keyPairGen.generateKeyPair();
// 得到私鑰
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
// 得到公鑰
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
try {
// 使用Base64對公鑰加密得到字符串
String publicKeyString = Base64Utils.encode(publicKey.getEncoded());
// 使用Base64對私鑰加密得到字符串
String privateKeyString = Base64Utils.encode(privateKey.getEncoded());
Map<String, String> keyMap = new HashMap<String, String>(2);
keyMap.put(PUBLIC_KEY, publicKeyString);
keyMap.put(PRIVATE_KEY, privateKeyString);
return keyMap;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 從字符串中加載公鑰
* @param publicKeyStr 公鑰數據字符串
* @return RSAPublicKey 加載出來的公鑰
* @exception Exception 加載公鑰時產生的異常
*/
public static RSAPublicKey loadPublicKeyByStr(String publicKeyStr)
throws Exception {
try {
byte[] buffer = Base64Utils.decode(publicKeyStr);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer);
return (RSAPublicKey) keyFactory.generatePublic(keySpec);
} catch (Exception e) {
throw new Exception(e);
}
}
/**
* 從字符串中加載私鑰
* @param privateKeyStr 私鑰數據字符串
* @return RSAPublicKey 加載出來的私鑰
* @exception Exception 加載私鑰時產生的異常
*/
public static RSAPrivateKey loadPrivateKeyByStr(String privateKeyStr)
throws Exception {
try {
byte[] buffer = Base64Utils.decode(privateKeyStr);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
} catch (Exception e) {
throw new Exception(e);
}
}
/**
* 公鑰加密過程
* @param publicKey 公鑰
* @param plainTextData 明文數據
* @return byte[] 加密結果
* @throws Exception 加密過程中的異常信息
*/
public static byte[] encrypt(RSAPublicKey publicKey, byte[] plainTextData)
throws Exception {
if (publicKey == null) {
throw new Exception("加密公鑰爲空, 請設置");
}
Cipher cipher = null;
try {
// 使用默認RSA
cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] output = cipher.doFinal(plainTextData);
return output;
} catch (Exception e) {
throw new Exception(e);
}
}
/**
* 私鑰加密過程
* @param privateKey 私鑰
* @param plainTextData 明文數據
* @return byte[] 加密結果
* @throws Exception 加密過程中的異常信息
*/
public static byte[] encrypt(RSAPrivateKey privateKey, byte[] plainTextData)
throws Exception {
if (privateKey == null) {
throw new Exception("加密私鑰爲空, 請設置");
}
Cipher cipher = null;
try {
// 使用默認RSA
cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] output = cipher.doFinal(plainTextData);
return output;
} catch (Exception e) {
throw new Exception(e);
}
}
/**
* 私鑰解密過程
* @param privateKey 私鑰
* @param cipherData 密文數據
* @return 明文
* @throws Exception 解密過程中的異常信息
*/
public static byte[] decrypt(RSAPrivateKey privateKey, byte[] cipherData)
throws Exception {
if (privateKey == null) {
throw new Exception("解密私鑰爲空, 請設置");
}
Cipher cipher = null;
try {
// 使用默認RSA
cipher = Cipher.getInstance("RSA");
// cipher= Cipher.getInstance("RSA", new BouncyCastleProvider());
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] output = cipher.doFinal(cipherData);
return output;
} catch (Exception e) {
throw new Exception(e);
}
}
/**
* 公鑰解密過程
* @param publicKey 公鑰
* @param cipherData 密文數據
* @return 明文
* @throws Exception 解密過程中的異常信息
*/
public static byte[] decrypt(RSAPublicKey publicKey, byte[] cipherData)
throws Exception {
if (publicKey == null) {
throw new Exception("解密公鑰爲空, 請設置");
}
Cipher cipher = null;
try {
// 使用默認RSA
cipher = Cipher.getInstance("RSA");
// cipher= Cipher.getInstance("RSA", new BouncyCastleProvider());
cipher.init(Cipher.DECRYPT_MODE, publicKey);
byte[] output = cipher.doFinal(cipherData);
return output;
} catch (Exception e) {
throw new Exception(e);
}
}
/**
* 獲取私鑰
* @param keyMap 密鑰對
* @return
* @throws Exception
*/
public static String getPrivateKey(Map<String, String> keyMap)
throws Exception {
String privateKey = keyMap.get(PRIVATE_KEY);
return privateKey;
}
/**
* 獲取公鑰
* @param keyMap 密鑰對
* @return
* @throws Exception
*/
public static String getPublicKey(Map<String, String> keyMap)
throws Exception {
String publicKey = keyMap.get(PUBLIC_KEY);
return publicKey;
}
}
SHA256與Base64實現
commons.codec包有這兩個算法的實現,分別如下:
SHA256
import org.apache.commons.codec.digest.DigestUtils;
public class SHA256Utils {
/**
* sha256加密
* */
public static String sha256Hex(String data){
return DigestUtils.sha256Hex(data);
}
}
Base64
import org.apache.commons.codec.binary.Base64;
public class Base64Utils
{
/**
* 使用Base64加密字符串
* @return 加密之後的字符串
* @exception Exception
*/
public static String encode(byte[] data){
Base64 base64 = new Base64();
String encodedData = base64.encodeAsString(data);
return encodedData;
}
/**
* 使用Base64解密
* @return 解密之後的字符串
* @exception Exception
*/
public static byte[] decode(String data){
Base64 base64 = new Base64();
byte[] decodedData = base64.decodeBase64(data);
return decodedData;
}
}
Springmvc的實現
當前端對服務端進行調用時,需要在springmvc中編寫一個攔截器,實現一個class繼承HandlerInterceptorAdapter,並重寫preHandle函數,實現如下:
在dispatcher中添加攔截器:
<mvc:interceptors>
<bean class="com.zyuc.fw.web.util.tokenInterceptor"/>
</mvc:interceptors>
攔截器代碼實現
import com.commons.common.support.spring.security.SecurityInterceptor;
import com.commons.common.utils.PropUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.apache.commons.codec.binary.Base64;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.interfaces.RSAPrivateKey;
import java.util.*;
public class tokenInterceptor extends HandlerInterceptorAdapter {
private static final Logger logger = LoggerFactory.getLogger(SecurityInterceptor.class);
public tokenInterceptor() {
}
private String getPrivateValues(byte[] decodeByte) throws Exception{
String privateKeyString = PropUtil.get("RSA.PrivateKey");
RSAPrivateKey privateKey = RSAUtils.loadPrivateKeyByStr(privateKeyString);
logger.info("[tokenInterceptor getPrivateValues] : privateKey = %s.", privateKey);
//私鑰解密
byte[] decodedData = RSAUtils.decrypt(privateKey, decodeByte);
String token = new String(decodedData);
return token;
}
/*
* 使用攔截器在客戶端訪問之前對請求的數據進行校驗
*/
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String token = "";
String serviceToken = "";
String requestValues = "";
String requestUrl = request.getRequestURL().toString();//得到請求的URL地址
requestValues += requestUrl;
Map params = request.getParameterMap();//得到所有請求的參數
Iterator it = params.keySet().iterator();
/*獲取URL+body的字符串集合*/
while(it.hasNext()){
String paramName = (String) it.next();
String paramValue = request.getParameter(paramName);
requestValues += paramName;
requestValues += paramValue;
}
/*獲取token,並對token做base64解碼*/
Enumeration<String> reqHeadInfos = request.getHeaderNames();//獲取所有的請求頭
while (reqHeadInfos.hasMoreElements()) {
token = request.getHeader("Authorization");//根據請求頭的名字獲取token的值
break;
}
/*如果沒有添加token,默認不進行校驗*/
if (null == token) {
return super.preHandle(request, response, handler);
}
byte[] decodeByte = Base64.decodeBase64(token);
/*獲取私鑰解密之後的token值*/
token = getPrivateValues(decodeByte);
serviceToken = SHA256Utils.sha256Hex(requestValues);
if (!serviceToken.equals(token)) {//判斷兩次的token值是否相同
return false;
}
return super.preHandle(request, response, handler);
}
}