package com.ff.base.security.encode;

import com.ff.base.utils.StringUtils;
import com.ff.base.utils.sign.Md5Utils;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;


/**
 * md5密码加密方法
 *
 * @author liukang
 */
@Component
public class CustomMd5PasswordEncoder implements PasswordEncoder {


    /**
     * 重写security加密算法
     * @param rawPassword
     * @return
     */
    @Override
    public String encode(CharSequence rawPassword) {
       return Md5Utils.hash(rawPassword.toString());
    }

    /**
     * 重写security密码比对方法
     * @param rawPassword the raw password to encode and match
     * @param encodedPassword the encoded password from storage to compare with
     * @return
     */
    @Override
    public boolean matches(CharSequence rawPassword, String encodedPassword) {
        return StringUtils.equals(Md5Utils.hash(rawPassword.toString()), encodedPassword);
    }
}
