我在Spring Boot中使用Spring Security。我希望用户密码应该加密并通过控制器插入数据库。但是我遇到了这个错误
第1行的“ password”列的数据太长
这是WebSecurityConfig类
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource)
.usersByUsernameQuery("select username, password, enabled from users where username=?")
.authoritiesByUsernameQuery("select username, authority from authorities where username=?")
.passwordEncoder(passwordEncoder());
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
这是控制器类
....
@Autowired
private PasswordEncoder passwordEncoder;
...
@RequestMapping(value="/register", method=RequestMethod.POST)
public String submitMemberForm(@ModelAttribute("user") @Valid User user, BindingResult result, Model model) {
if (result.hasErrors()) {
viewPage = "membership";
return viewPage;
}
String userPassword = user.getPassword();
String encodedPassword = passwordEncoder.encode(userPassword);
Authority authority = new Authority(user.getUsername(),"ROLE_USER");
user.setPassword(encodedPassword);
user.setAuthority(authority);
userDaoServiceImpl.createUser(user);
viewPage = "membership";
return viewPage;
}
答案 0 :(得分:0)
Spring security使用$ 2a $ BCrypt生成一个密码字符串,该字符串的长度为60个字符。示例:$2a$10$IvpDzjQ3RhFvIECF7r2muOlLsDFywkyGJNEg1q6OudgK9.2vkubfW
因此请确保您的密码列至少为char(60)
或varchar(60)