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);
Spring Security自定义PasswordEncoder可以通过实现PasswordEncoder接口来实现。可以参考引用和引用中的代码示例。 在自定义的PasswordEncoder中,需要实现encode方法和matches方法。encode方法用于对密码进行编码,matches方法用于比较输入的密码和数据库中保存的密码是否匹配。 示例代码如下: ```java @Configuration public class CustomPasswordEncoder implements PasswordEncoder { @Override public String encode(CharSequence charSequence) { // 进行编码逻辑,例如使用BCryptPasswordEncoder进行编码 // return new BCryptPasswordEncoder().encode(charSequence); // 或者使用自定义的编码方法 return AppMD5Util.getMD5((String) charSequence); } @Override public boolean matches(CharSequence charSequence, String s) { // 进行密码匹配逻辑,例如比较输入的密码和数据库保存的密码是否相等 // return new BCryptPasswordEncoder().matches(charSequence, s); // 或者使用自定义的匹配方法 String encodedPassword = this.encode(charSequence); return encodedPassword.toLowerCase().equals(s.toLowerCase()); } } ``` 然后,在配置类中将自定义的PasswordEncoder注入到Spring Security中: ```java @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private CustomPasswordEncoder customPasswordEncoder; @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService).passwordEncoder(customPasswordEncoder); } } ``` 通过以上步骤,可以实现Spring Security的自定义PasswordEncoder。注意,自定义的PasswordEncoder需要根据具体的加密需求进行编写。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [spring security 原理自定义用户验证逻辑passwordEncoder验证](https://blog.csdn.net/weixin_42546729/article/details/93064247)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [spring security 一些自定义操作](https://blog.csdn.net/qq_27886773/article/details/106626636)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Spring Security自定义身份验证](https://blog.csdn.net/m0_60467232/article/details/130279177)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值