用户勾选自动登录,登录成功后,存入cookie
if(StringUtils.isNotBlank(freeLogin)&&"1".equals(freeLogin)){//判断用户是否选中免登录 String info=getEncryptionInfo(user.getShouji(), MD5Util.string2MD5(user.getPassword())); CookieUtils.addCookie(getResponse(), LOGIN_COOKIE_NAME, info, "/", 60*60*24*7); }操作用户信息加密的方法
/** * 用户信息加密 * @param userName * @param UserPassword * @return 加密后的信息 */ public static final String getEncryptionInfo(String userName,String UserPassword){ String info=userName+UserPassword+FREE_LOGIN_TAG; String md5Str=MD5Util.string2MD5(info); String dBase64=getBase64(md5Str)+","+userName; return getBase64(dBase64); } /** * 从base64中解密获取用户名 * @return */ public static final String getBase64UserName(String encryptionInfo){ //解密身份信息; String userName=null; String str= getStrFromBase64(encryptionInfo); if(str==null||"".equals(str.trim())){ return null; } if(str.indexOf(",")>0){ String []arr=str.split(","); userName=arr[1]; } return userName; } // base64加密 public static final String getBase64(String str) { if(StringUtils.isBlank(str)){ return null; } byte[] b = null; try { b = str.getBytes("utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return new BASE64Encoder().encode(b); } // base64解密 public static final String getStrFromBase64(String str) { byte[] b = null; String result = null; if (str != null&&!"".equals(str.trim())) { BASE64Decoder decoder = new BASE64Decoder(); try { b = decoder.decodeBuffer(str); result = new String(b, "utf-8"); } catch (Exception e) { e.printStackTrace(); } } return result; }