gpt4 book ai didi

java - 身份验证失败 : password does not match stored value (see update 2)

转载 作者:行者123 更新时间:2023-11-30 09:04:55 25 4
gpt4 key购买 nike

在我当前的 spring 项目中,我有以下安全配置:

@Configuration
@ComponentScan(value="com.spring.loja")
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private UserDetailsService userDetailsService;

@Autowired
private SocialUserDetailsService socialUserDetailsService;

@Autowired
private PasswordEncoder passwordEncoder;

@Override
public void configure(WebSecurity web) throws Exception {
DefaultWebSecurityExpressionHandler handler = new DefaultWebSecurityExpressionHandler();
handler.setPermissionEvaluator(new CustomPermissionEvaluator());
web.expressionHandler(handler);
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf()
.disable()
.authorizeRequests()
.antMatchers("/resources/**", "/erro/**", "/categoria/**", "/produto/**", "/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/entrar").permitAll()
.loginProcessingUrl("/login").permitAll()
.usernameParameter("login")
.passwordParameter("senha")
.defaultSuccessUrl("/admin")
.failureUrl("/entrar?erro=login").permitAll()
.and()
.exceptionHandling()
.accessDeniedPage("/erro/403")
.and()
.logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/").permitAll()
.and()
.apply(new SpringSocialConfigurer());
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(userDetailsService)
.passwordEncoder(passwordEncoder);
}

@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}

}

当我运行应用程序并尝试登录时,系统返回到登录页面,即使登录凭据正确(我确信)。

谁能看出这里出了什么问题?

更新

我也试过,同样的问题。在我看来,这个配置无法到达我的 userDetailsS​​ervice 类,它应该通过此类中的 Autowiring 属性检索:

@Configuration
@ComponentScan(value="com.spring.loja")
@EnableGlobalMethodSecurity(prePostEnabled=true)
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private UserDetailsService userDetailsService;

@Autowired
private SocialUserDetailsService socialUserDetailsService;

@Autowired
private PasswordEncoder passwordEncoder;

@Autowired
private AuthenticationManagerBuilder auth;

@Override
public void configure(WebSecurity web) throws Exception {
DefaultWebSecurityExpressionHandler handler = new DefaultWebSecurityExpressionHandler();
handler.setPermissionEvaluator(new CustomPermissionEvaluator());
web.expressionHandler(handler);
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf()
.disable()
.authorizeRequests()
.antMatchers("/resources/**", "/erro/**", "/categoria/**", "/produto/**", "/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/entrar").permitAll()
.loginProcessingUrl("/login").permitAll()
.usernameParameter("login")
.passwordParameter("senha")
.defaultSuccessUrl("/admin")
.failureUrl("/entrar?erro=login").permitAll()
.and()
.exceptionHandling()
.accessDeniedPage("/erro/403")
.and()
.logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/").permitAll()
.and()
.apply(new SpringSocialConfigurer());
}

@Autowired
protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(userDetailsService)
.passwordEncoder(passwordEncoder);
}

@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return auth.getOrBuild();
}

}

更新 2

配置log4j后,发现错误是密码与存储值不匹配。问题是我确保存储在数据库中的密码被编码为 MD5,而我的 PasswordEncoder bean 是这样的:

@Component
public class BCryptPasswordEncoder implements PasswordEncoder {

@Override
public String encode(CharSequence arg0) {
try {
return getMD5Hex((String) arg0);
} catch (NoSuchAlgorithmException e) {
return "NoSuchAlgorithmException";
}
}

@Override
public boolean matches(CharSequence arg0, String arg1) {
return arg0.equals(encode(arg1));
}

public static String getMD5Hex(final String inputString) throws NoSuchAlgorithmException {

MessageDigest md = MessageDigest.getInstance("MD5");
md.update(inputString.getBytes());

byte[] digest = md.digest();

return convertByteToHex(digest);
}

private static String convertByteToHex(byte[] byteData) {

StringBuilder sb = new StringBuilder();
for (int i = 0; i < byteData.length; i++) {
sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
}

return sb.toString();
}

}

我明确告诉我要使用 MD5 的地方。这里有什么问题吗?

ps.: 我也注意到应用程序没有使用我的 authenticationManager bean,它在 SEcurityConfig 类中定义(问题中的第二个列表)

最佳答案

org.springframework.security.crypto.password.PasswordEncoder matches 方法有签名

boolean matches(CharSequence rawPassword, String encodedPassword);

所以这意味着 arg0 是用户输入的密码,arg1 是您保存在 DB 中的编码密码。所以实现应该是

 public boolean matches(CharSequence rawPassword, String encodedPassword) {
return encodedPassword.equals(encode(rawPassword));
}

您在 matches 方法中再次对编码后的密码进行编码,因为您使用的参数序列不正确。最好使用有意义的名称而不是 arg0、arg1,以避免混淆。

关于java - 身份验证失败 : password does not match stored value (see update 2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25035125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com