拦截器
/**
* 处理ssodan单点登录拦截
*/
String sessionId = "";
//获取cookie
Cookie[] cookie = request.getCookies();
//如果cookie为空则拦截不通过
if (cookie == null) {
return false;
}
//获取cookie里面的sessionId的值
for (int i = 0; i < cookie.length; i++) {
Cookie cook = cookie[i];
if (cook.getName().equals("CORE_SESSION")) {
sessionId = cook.getValue().toString();
}
}
//sessionId如果为空则拦截不通过
if ("".equals(sessionId)) {
return false;
}
//获取Jedis对象
Jedis jedis = RedisUtil.getJedis();
//获取redis里面的sessionId
String UserLoginSessionStr = "";
try {
UserLoginSessionStr = (jedis.get(sessionId));
}catch (Exception e){
e.printStackTrace();
}
//如果redis里面的sessionId为空则拦截不通过
if (null == UserLoginSessionStr || "".equals(UserLoginSessionStr)) {
return false;
}
return true;
登录入口
@ResponseBody @RequestMapping(value = "/login", produces = "application/json; charset=utf-8", method = { RequestMethod.POST }) public String login(String userName, String pas, HttpServletResponse response){ WebResultMsg resutlMsg = new WebResultMsg(); resutlMsg.setResultType(false); resutlMsg.setMsg("登录失败"); if (userName == null || userName == "") { resutlMsg.setMsg("请输入用户名"); return JSON.toJSONString(resutlMsg); } if (pas == null || pas == "") { resutlMsg.setMsg("请输入登录密码"); return JSON.toJSONString(resutlMsg); } // 用户信息 UserInfo model = iUserInfoService.getUserByUserName(userName); if (model == null) { resutlMsg.setMsg("用户不存在或密码不正确"); } else { if(model.getPas().equals(pas)){ //获取sessionId String sessionId = RedisUtil.storeSessionInRedis(model); //设置cookie Cookie cookie = new Cookie("CORE_SESSION", sessionId); cookie.setPath("/"); response.addCookie(cookie); resutlMsg.setUid(model.getId()+""); resutlMsg.setSign(sessionId); resutlMsg.setResultType(true); resutlMsg.setMsg("登录成功"); }else{ resutlMsg.setMsg("用户不存在或密码不正确"); } } return JSON.toJSONString(resutlMsg); }
Redis工具类(存取数据)
public class RedisUtil { //连接Jedis获取Jedis对象 public static Jedis getJedis(){ //连接本地的 Redis 服务 Jedis jedis = new Jedis("localhost"); System.out.println("连接成功"); return jedis; } /** * 存储用户登录session信息到redis中 * @param userInfo 用户信息 * @return */ public static String storeSessionInRedis(UserInfo userInfo) { Jedis jedis = getJedis(); // 存储用户ticket信息 // 使用加密登录用户ID和秘钥生成SessionID String sessionId = PaymentUtil.encrypt(String.valueOf(userInfo.getId()), StaticData.key); //声明redis的值 String unique_ticket = userInfo.getId()+sessionId+"_USER_LOGIN"; //声明redis的键 String ticket = userInfo.getId()+sessionId+System.currentTimeMillis()+"_USER_LOGIN"; //限制唯一登录,判断是否已存在,删除上一个用户信息 if (jedis.exists(unique_ticket)){ //删除redis上一个用户信息 jedis.del((jedis.get(unique_ticket))); } //将用户id存到redis里面 jedis.set(unique_ticket, String.valueOf(userInfo.getId())); //设置过期时间 jedis.expire(unique_ticket, 300); //设置redis的键和值 jedis.set(ticket , unique_ticket); //设置过期时间 jedis.expire(ticket, 300); return ticket; } }
签名工具类(把数据加密后生成签名)
public class PaymentUtil { /** * 生成签名 * @param key 业务类型 * @param uid 商户秘钥 * @return */ public static String encrypt(String key,String uid){ //返回的签名 String sNewString = ""; StringBuffer sValue = new StringBuffer(); sValue.append(key); sValue.append(uid); try { byte[] md5Bytes = HmacMD5Util.getHmacMd5Bytes(key.getBytes(),sValue.toString().getBytes()); //字节转字符 sNewString = HexUtil.encode(md5Bytes); }catch (Exception e){ e.getLocalizedMessage(); } return sNewString; } }
Md5加密工具类(加密数据)
/** * 加密 */ public class HmacMD5Util { /** * 计算参数的md5信息 * @param str 待处理的字节数组 * @return md5摘要信息 * @throws NoSuchAlgorithmException */ private static byte[] md5(byte[] str) throws NoSuchAlgorithmException { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(str); return md.digest(); } /** * 将待加密数据data,通过密钥key,使用hmac-md5算法进行加密,然后返回加密结果。 * 参照rfc2104 HMAC算法介绍实现。 * @author 尹星 * @param key 密钥 * @param data 待加密数据 * @return 加密结果 * @throws NoSuchAlgorithmException */ public static byte[] getHmacMd5Bytes(byte[] key,byte[] data) throws NoSuchAlgorithmException { /* HmacMd5 calculation formula: H(K XOR opad, H(K XOR ipad, text)) * HmacMd5 计算公式:H(K XOR opad, H(K XOR ipad, text)) * H代表hash算法,本类中使用MD5算法,K代表密钥,text代表要加密的数据 * ipad为0x36,opad为0x5C。 */ int length = 64; byte[] ipad = new byte[length]; byte[] opad = new byte[length]; for(int i = 0; i < 64; i++) { ipad[i] = 0x36; opad[i] = 0x5C; } byte[] actualKey = key; //Actual key. byte[] keyArr = new byte[length]; //Key bytes of 64 bytes length /*If key's length is longer than 64,then use hash to digest it and use the result as actual key. * 如果密钥长度,大于64字节,就使用哈希算法,计算其摘要,作为真正的密钥。 */ if(key.length>length) { actualKey = md5(key); } for(int i = 0; i < actualKey.length; i++) { keyArr[i] = actualKey[i]; } /*append zeros to K * 如果密钥长度不足64字节,就使用0x00补齐到64字节。 */ if(actualKey.length < length) { for(int i = actualKey.length; i < keyArr.length; i++) keyArr[i] = 0x00; } /*calc K XOR ipad * 使用密钥和ipad进行异或运算。 */ byte[] kIpadXorResult = new byte[length]; for(int i = 0; i < length; i++) { kIpadXorResult[i] = (byte) (keyArr[i] ^ ipad[i]); } /*append "text" to the end of "K XOR ipad" * 将待加密数据追加到K XOR ipad计算结果后面。 */ byte[] firstAppendResult = new byte[kIpadXorResult.length+data.length]; for(int i=0;i<kIpadXorResult.length;i++) { firstAppendResult[i] = kIpadXorResult[i]; } for(int i=0;i<data.length;i++) { firstAppendResult[i+keyArr.length] = data[i]; } /*calc H(K XOR ipad, text) * 使用哈希算法计算上面结果的摘要。 */ byte[] firstHashResult = md5(firstAppendResult); /*calc K XOR opad * 使用密钥和opad进行异或运算。 */ byte[] kOpadXorResult = new byte[length]; for(int i = 0; i < length; i++) { kOpadXorResult[i] = (byte) (keyArr[i] ^ opad[i]); } /*append "H(K XOR ipad, text)" to the end of "K XOR opad" * 将H(K XOR ipad, text)结果追加到K XOR opad结果后面 */ byte[] secondAppendResult = new byte[kOpadXorResult.length+firstHashResult.length]; for(int i=0;i<kOpadXorResult.length;i++) { secondAppendResult[i] = kOpadXorResult[i]; } for(int i=0;i<firstHashResult.length;i++) { secondAppendResult[i+keyArr.length] = firstHashResult[i]; } /*H(K XOR opad, H(K XOR ipad, text)) * 对上面的数据进行哈希运算。 */ byte[] hmacMd5Bytes = md5(secondAppendResult); return hmacMd5Bytes; } public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException { //加密生成签名 byte[] macmd5 = HmacMD5Util.getHmacMd5Bytes("7e62ff29f6548e2e7cf5307315fb2b1".getBytes(), "".getBytes()); //把字节转字符串 System.out.println(HexUtil.encode(macmd5)); }
进制转换工具类(把加密后的数据转换成字符串)
/** * 进制转换 */ public class HexUtil { /** * 字节流转成十六进制字符串表示 */ public static String encode(byte[] src) { String strHex = ""; StringBuilder sb = new StringBuilder(""); for (int n = 0; n < src.length; n++) { strHex = Integer.toHexString(src[n] & 0xFF); sb.append((strHex.length() == 1) ? "0" + strHex : strHex); // 每个字节由两个字符表示,位数不够,高位补0 } return sb.toString().trim(); } /** * 字符串转成字节流 */ public static byte[] decode(String src) { int m = 0, n = 0; int byteLen = src.length() / 2; // 每两个字符描述一个字节 byte[] ret = new byte[byteLen]; for (int i = 0; i < byteLen; i++) { m = i * 2 + 1; n = m + 1; int intVal = Integer.decode("0x" + src.substring(i * 2, m) + src.substring(m, n)); ret[i] = Byte.valueOf((byte)intVal); } return ret; } }