项目实践――MD5加密

  在项目中,需要用MD5进行加密,这里分享1个MD5加密类。

MD5加密类:



public class Md5 { private static String DEFAULT_JCE = com.sun.crypto.provider.SunJCE; private static String IBM_JCE = com.ibm.crypto.provider.IBMJCE; protected static final Log log = LogFactory.getLog(Md5.class); /** * 初始化系统加密算法提供者 */ static { try { Security.addProvider((Provider)Class.forName(DEFAULT_JCE).newInstance()); } catch (Exception e) { log.info(e); try { Security.addProvider((Provider)Class.forName(IBM_JCE).newInstance()); } catch (Exception ex) { log.info(ex); } } } /** * get hex string * * @param x * @return */ private static String hexDigit(byte x) { StringBuffer sb = new StringBuffer(); char c; // First nibble c = (char) ((x >> 4) & 0xf); if (c > 9) { c = (char) ((c - 10) + 'a'); } else { c = (char) (c + '0'); } sb.append(c); // Second nibble c = (char) (x & 0xf); if (c > 9) { c = (char) ((c - 10) + 'a'); } else { c = (char) (c + '0'); } sb.append(c); return sb.toString(); } /** * 加密 * * @param content * 加密内容 * @return 加密串 */ public static String encrypt(String content) { try { MessageDigest algorithm = null; algorithm = MessageDigest.getInstance(MD5); algorithm.reset(); if (content != null) { algorithm.reset(); algorithm.update(content.getBytes()); byte digest[] = algorithm.digest(); StringBuffer hexString = new StringBuffer(); int digestLength = digest.length; for (int i = 0; i < digestLength; i++) { hexString.append(hexDigit(digest[i])); } return hexString.toString(); } else { return ; } } catch (NoSuchAlgorithmException ex) { //加密进程中出现异常,采取原始的的内容串 return content; } } }



运行测试:



@Test public void testMd5(){ System.err.println(this.encrypt(123456)); }


结果:

e10adc3949ba59abbe56e057f20f883e



用户登录


@RequestMapping(/login.do) @ResponseBody @Override public Object login(HttpServletRequest request,HttpServletResponse response) { Logger log = Logger.getLogger(getClass()); String biskeep = ; Md5 md5=new Md5(); try { String loginName = request.getParameter(loginName); String loginPassword = md5.encrypt(request.getParameter(loginPassword)); HttpSession session = request.getSession(); if (loginName != null && !loginName.trim().equals() && loginPassword != null && !loginPassword.trim().equals()) { SysUser user = userService.queryUser(loginName,loginPassword); biskeep = user.getBiskeep(); // 查询用户的部门信息 String deptIdStr = user.getDepartmentid(); SysDept sysDept=deptService.queryEntityById(SysDept.class,deptIdStr); // SysDept sysDept=null; // 查询用户的角色信息,应当是1个list集合 String roleIdStr = roleService.getRoleIdStr(user.getId()); session.setAttribute(ConstValues.LOGIN_DEPT_ID,deptIdStr); session.setAttribute(ConstValues.LOGIN_ROLE_ID,roleIdStr); session.setAttribute(ConstValues.LOGIN_DEPT_TYPE,sysDept.getCdeptno()); session.setAttribute(depId,deptIdStr); // 将用户信息放入到session中去 session.setAttribute(ConstValues.LOGIN_USER_NAME,user.getCloginname()); session.setAttribute(ConstValues.LOGIN_USER_ID,user.getId()); session.setAttribute(ConstValues.LOGIN_FirsTNAME,user.getFirstname()); session.setAttribute(ConstValues.LOGIN_LASTNAME,user.getLastname()); session.setAttribute(ConstValues.LOGIN_USER_PASSWORD,user.getCpassword()); String ip = request.getHeader(x-forwarded-for); if (ip == null || ip.length() == 0 || unkNown.equalsIgnoreCase(ip)) { ip = request.getHeader(Proxy-Client-IP); } if (ip == null || ip.length() == 0 || unkNown.equalsIgnoreCase(ip)) { ip = request.getHeader(WL-Proxy-Client-IP); } if (ip == null || ip.length() == 0 || unkNown.equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } log.info(本机IP: + ip); session.setAttribute(ConstValues.LOGIN_IP,ip); Map<String,String> param = new HashMap<String,String>(); param.put(ip,ip); } JSONObject obj = createSuccessMessage(null); obj.put(biskeep,biskeep); String depId = (String) session.getAttribute(ConstValues.LOGIN_DEPT_ID); String ss = (String) session.getAttribute(ConstValues.LOGIN_USER_ID); return obj.toString(); } catch (Exception e) { e.printstacktrace(); return createErrorMessage(e.getMessage()).toString(); } }



  思路很简单,数据库存的密码是经过MD5加密过的,将用户登录的密码亦经过MD5加密,匹配成功便可登录





相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...