Java 如何从数据输入字段获取密码?
网站安全配置类Java 如何从数据输入字段获取密码?,java,postgresql,Java,Postgresql,网站安全配置类 public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserService userSevice; @Autowired private PasswordEncoder passwordEncoder; @Bean public PasswordEn
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserService userSevice;
@Autowired
private PasswordEncoder passwordEncoder;
@Bean
public PasswordEncoder getPasswordEncoder() {
return new BCryptPasswordEncoder(8);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/registration", "/static/**", "/about").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userSevice)
.passwordEncoder(passwordEncoder);
}
}
public class UserService implements UserDetailsService {
@Autowired
private UserRepo userRepo;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userRepo.findByUsername(username);
System.out.println(username);
if (user == null) {
throw new UsernameNotFoundException("User not found");
}
return user;
}
}
用户服务类
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserService userSevice;
@Autowired
private PasswordEncoder passwordEncoder;
@Bean
public PasswordEncoder getPasswordEncoder() {
return new BCryptPasswordEncoder(8);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/registration", "/static/**", "/about").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userSevice)
.passwordEncoder(passwordEncoder);
}
}
public class UserService implements UserDetailsService {
@Autowired
private UserRepo userRepo;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userRepo.findByUsername(username);
System.out.println(username);
if (user == null) {
throw new UsernameNotFoundException("User not found");
}
return user;
}
}
UserDetailsService
只显示用户名,但我希望它能让我看到输入的密码,我输入了system.out.printl(username)
进行验证,它会显示出来,我不知道如何输出密码。它们来自数据库。提前谢谢。对不起,我的英语很差,所以,本质上,您需要的是通过客户端从AuthenticationManagerBuilder的auth对象发送的密码。如果是,这将不可能,因为密码存储在不可逆散列中如果要从DB获取密码,可以调用
user.getPassword()