Spring Security PasswordEncoder 的使用

Spring Security PasswordEncoder 的使用

Spring 中,提供了对于密码加密加盐的方法,我们可以通过引入依赖来方便的使用。它相对于MD5方法的优势在于:他的提供了加盐的选择,相反MD5的安全性在降低,可以通过彩虹表等方法来破解MD5的一部分密码。下面是具体的使用方法:

1.依赖

    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-core</artifactId>
      <version>5.1.7.RELEASE</version>
    </dependency>

2.注入bean

@Configuration
public class PasswordEncoderConfig {
  
  private static final Logger logger = LoggerFactory.getLogger(PasswordEncoderConfig.class);
  
  @Bean
  public PasswordEncoder passwordEncoder() {
    
    PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();

    if (passwordEncoder instanceof DelegatingPasswordEncoder) {
      ((DelegatingPasswordEncoder)passwordEncoder).setDefaultPasswordEncoderForMatches(NoOpPasswordEncoder.getInstance());
    }

    logger.debug("PasswordEncoderConfig passwordEncoder is {}", JSON.toJSONString(passwordEncoder));
    return passwordEncoder;
  }

}

3.具体的引用

    @Autowired
    private PasswordEncoder passwordEncoder;

4.方法

	/**
	 * Encode the raw password. Generally, a good encoding algorithm applies a SHA-1 or
	 * greater hash combined with an 8-byte or greater randomly generated salt.
	 */
	String encode(CharSequence rawPassword);

	/**
	 * Verify the encoded password obtained from storage matches the submitted raw
	 * password after it too is encoded. Returns true if the passwords match, false if
	 * they do not. The stored password itself is never decoded.
	 *
	 * @param rawPassword the raw password to encode and match
	 * @param encodedPassword the encoded password from storage to compare with
	 * @return true if the raw password, after encoding, matches the encoded password from
	 * storage
	 */
	boolean matches(CharSequence rawPassword, String encodedPassword);
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值