【发布时间】:2020-04-19 08:12:12
【问题描述】:
我正在尝试从 Android 应用执行身份验证。我基本上将用户名和密码(未编码)发送到我的 rest api 端点,即:/api/management/login我正在使用 Retrofit)。之后,我检查用户是否存在,如果存在则返回对象,如果不存在则返回null。我注意到编码的密码与存储在我的数据库中的密码不同,即使初始密码字符串是相同的。我读到 PasswordEncoder 接口正在生成一个随机盐以对密码进行编码。有没有办法让盐独一无二?
这是我的 Spring 安全配置文件:
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
private UserPrincipalDetailsService userPrincipalDetailsService;
public SecurityConfiguration(UserPrincipalDetailsService userPrincipalDetailsService) {
this.userPrincipalDetailsService = userPrincipalDetailsService;
}
@Override
protected void configure(AuthenticationManagerBuilder auth) {
auth.authenticationProvider(authenticationProvider());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf()
.disable()
.exceptionHandling()
.authenticationEntryPoint(new Http403ForbiddenEntryPoint() {}).and()
.authenticationProvider(authenticationProvider())
.authorizeRequests()
.antMatchers("/management/*").hasRole("ADMIN")
.antMatchers("/*").hasRole("ADMIN")
.antMatchers("/management/professor*").hasRole("ADMIN")
.antMatchers("/management/absence*").hasRole("ADMIN")
.antMatchers("/management/room*").hasRole("ADMIN")
.anyRequest().permitAll()
.and()
.formLogin()
.loginProcessingUrl("/signin").permitAll()
.loginPage("/login").permitAll()
.successHandler(mySimpleUrlAuthenticationHandler())
.failureUrl("/login?error=true")
.usernameParameter("username")
.passwordParameter("password")
.and()
.logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/login")
.and()
.rememberMe().userDetailsService(userPrincipalDetailsService).rememberMeParameter("checkRememberMe");
}
@Bean
DaoAuthenticationProvider authenticationProvider(){
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
daoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
daoAuthenticationProvider.setUserDetailsService(this.userPrincipalDetailsService);
return daoAuthenticationProvider;
}
@Bean
PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
MySimpleUrlAuthenticationHandler mySimpleUrlAuthenticationHandler() {
return new MySimpleUrlAuthenticationHandler();
}
}
有什么建议吗?
【问题讨论】:
标签: android spring-security retrofit retrofit2 bcrypt