springsecurity 权限管理实例菜单管理之自定义加密
自定义加密是实现PasswordEncoder接口并重写其两个方法
public interface PasswordEncoder { String encode(CharSequence var1); //加密 boolean matches(CharSequence var1, String var2);//比较 //对密码进行再次加密 default boolean upgradeEncoding(String encodedPassword) { return false; } }
自定义具体实现
MD5工具代码实现
public class MD5Util { protected MD5Util(){ } private static final String ALGORITH_NAME = "md5"; private static final int HASH_ITERATIONS = 2; public static String encrypt_md5(String password) { byte[] secretBytes = null; try { secretBytes = MessageDigest.getInstance("md5").digest( password.getBytes()); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("找不到md5算法"); } String md5code = new BigInteger(1, secretBytes).toString(16); for (int i = 0; i < 32 - md5code.length(); i++) { md5code = "0" + md5code; } return md5code; } }
实现自定义MD5加密
public class MD5PasswordEncoder implements PasswordEncoder { /** * 加密方法 * @param charSequence * @return */ @Override public String encode(CharSequence charSequence) { return MD5Util.encrypt_md5(charSequence.toString()); } /** * 密码比较 * @param charSequence * @param encodedPassword * @return */ @Override public boolean matches(CharSequence charSequence, String encodedPassword) { return encodedPassword.equals(MD5Util.encrypt_md5(charSequence.toString())); } }