字符串加密,校验工具类 org.springframework.security.crypto.password.PasswordEncoder

本文介绍了一个用于字符串加密和验证的工具类,包括加密方法和比较方法,使用了Spring Security中的StandardPasswordEncoder。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.crypto.password.StandardPasswordEncoder;

/**
 * 字符串加密,校验工具类
 */
public class EncryptUtil implements PasswordEncoder {

 private static final String SITE_WIDE_SECRET = "XXXXXXXXXXXX";
 private static final PasswordEncoder encoder = new StandardPasswordEncoder(SITE_WIDE_SECRET);
 
 /**
  * 加密方法
  * @param rawPassword 被加密的字符串
  * @return
  */
 @Override
 public String encode(CharSequence rawPassword) {
  return encoder.encode(rawPassword);
 }

 /**
  * 比较是否相等
  * @param rawPassword 加密后的字符串
  * @param password 源字符串
  * @return
  */
 @Override
 public boolean matches(CharSequence password, String rawPassword) {
  boolean isSuccess = false;
  try {
   isSuccess = encoder.matches(password, rawPassword);
  } catch(Exception e){
   e.printStackTrace();
  }
  return isSuccess;
 }
 
 public static void main(String[] args) {
  String source = "1";
  String target = "6900da5dfcf294bf5a6a48ad9579b007ab87e0af91f3982b5862e3def63ef8c1eff60e70243e4cbd";
  EncryptUtil util = new EncryptUtil();
  System.out.println(util.matches(source, target));
 }

}

package com.lh.config; import com.lh.Service.CustomUserDetailsService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.web.SecurityFilterChain; @Configuration @EnableWebSecurity public class SecurityConfig { @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http .authorizeHttpRequests(auth -> auth .requestMatchers("/login").permitAll() // 允许所有用户访问登录页面 .requestMatchers("/admin/**").hasRole("admin") // 管理员页面,只有ADMIN角色可以访问 .requestMatchers("/user/**").hasRole("user") // 用户页面,只有USER角色可以访问 .anyRequest().authenticated() // 其他请求都需要认证 ) .formLogin(form -> form .loginPage("/login") // 指定自定义登录页面 .loginProcessingUrl("/login") // 指定登录表单提交处理URL .defaultSuccessUrl("/index",true) // 登录成功后跳转的URL .failureUrl("/login?error") // 登录失败后跳转的URL .usernameParameter("username") // 表单中用户名的参数名 .passwordParameter("password") // 表单中密码的参数名 ) .logout(logout -> logout .logoutUrl("/logout") // 注销请求的URL .logoutSuccessUrl("/login") // 注销成功后跳转的URL ) .csrf(csrf -> csrf.disable()); // 禁用CSRF保护 return http.build(); } @Bean public UserDetailsService userDetailsService() { return new CustomUserDetailsService(); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } }这是配置文件,在loadUserByUsername方法内添加日志输出,请求没有触发
05-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值