1 PasswordEncoder
2 BCryptPasswordEncoder(基于BCrypt的强散列哈希加密)
基于BCrypt的强散列哈希加密实现,并可以由客户端指定加密的强度strength
,强度越高安全性自然就越高,默认为10.
Spring Security设置加密方式。
/** * 身份认证接口 * * @param auth * @throws Exception */ @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth // 从数据库读取的用户进行身份认证 .userDetailsService(userDetailsService) //加密方法(强散列哈希加密) .passwordEncoder(new BCryptPasswordEncoder()); }
3 自定义加密
自定义加密方式,也可以不加密。
package com.entity; import cn.hutool.core.util.StrUtil; import org.springframework.security.crypto.password.PasswordEncoder; public class CustomPasswordEncoder implements PasswordEncoder { @Override public String encode(CharSequence charSequence) { return charSequence.toString(); } @Override public boolean matches(CharSequence charSequence, String s) { return StrUtil.equals(charSequence, s); } }
Spring Security设置加密方式。
/** * 身份认证接口 * * @param auth * @throws Exception */ @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth // 从数据库读取的用户进行身份认证 .userDetailsService(userDetailsService) //加密方法(不加密) .passwordEncoder(new CustomPasswordEncoder()); }
文章知识点与官方知识档案匹配,可进一步学习相关知识