java中使用MD5对密码进行加密

import org.springframework.security.authentication.encoding.MessageDigestPasswordEncoder;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import com.ec.gen.base.model.ManagementUser;

/**
 * 当前登录用户信息
 *
 * @author liuyanmin
 * @date 2014-4-3 上午9:40:51
 */
public class UserContextUtils {
    
    private static MessageDigestPasswordEncoder passEncoder = new MessageDigestPasswordEncoder("MD5");
   
    
    /**
     * 取得当前的登录用户(管理平台)
     *
     * @return
     * @author liuyanmin
     * @date 2015-1-17 上午9:41:20
     */
    public static ManagementUser getManagementLoginUser(){
        ManagementUser user = null ; 
        try{
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            Object principal = authentication.getPrincipal();
            if(principal instanceof ManagementUser){
                user = (ManagementUser)principal;
            }
        }catch(Exception e){
            user = null ; 
        }
        if(user == null ){
           user = new ManagementUser();
           user.setId("AUTO") ; 
            
        }
       return user ;
    }

    /**
     * 返回加密的密码 
     *
     * @param password  需要加密的密码
     * @param salt      salt,--使用用户名
     * @return
     * @author liuyanmin
     * @date 2015-1-17 上午9:45:20
     */
    public static String encodePassword(String password,Object salt){
        return passEncoder.encodePassword(password, salt);
    }
    
    
    
    public static void main(String[] args) {
        System.out.println(encodePassword("admin","admin"));
        System.out.println(encodePassword("liuyanmin", "123456"));
    }
    
}

原文地址:https://www.cnblogs.com/liuyanmin/p/5146552.html