WebSecurityConfig 类
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);
}
}
UserService 类
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)
进行验证,并且显示了该用户名,但我不知道如何输出密码。它们取自数据库。预先感谢。对不起,我的英语
答案 0 :(得分:0)
因此,实质上,您想要的是通过客户端从AuthenticationManagerBuilder
的auth对象发送的密码。如果是,则将不可能,因为密码存储在不可逆的哈希中。
如果要从数据库获取密码,可以致电user.getPassword()
。
答案 1 :(得分:0)
HttpServletRequest request =((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
String passwordFromForm = request.getParameter("password");