我正在尝试使用spring安全性将密码加密到数据库中,但是我收到了错误。当我尝试编码密码时,我的spring安全类返回错误。我不知道我哪里出错了。
此图显示安全类抛出的错误:
SecurityConfig.java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserService userService;
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userService).passwordEncoder(passwordEncoder());
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
ApplicationAuthenticationSuccessHandler.java
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication
authentication) throws IOException, ServletException {
User user = (User) authentication.getPrincipal();
request.getSession().setAttribute("authenticated_user", user);
super.onAuthenticationSuccess(request, response, authentication);
}
UserService.java
@Service
@Transactional
public class UserService {
@Autowired
private UserRepository userRepository;
@Autowired
private PasswordEncoder passwordEncoder;
public Response createUser(User user) {
if( user == null || user.getId() == null){
throw new ResourceNotFoundException("Empty", "Missing Data Exception");
} else {
user.setPassword(passwordEncoder.encode(user.getPassword()));
userRepository.save(user);
return new Response(user.getId(), "Customer Created Successfully");
}
}
User.java
@Entity
public class User implements Serializable, UserDetails {
@NotBlank
private String surname;
@NotBlank
@Size(min = 5, max = 20, message = "Please enter between {min} and {max} characters.")
private String phoneNumber;
@NotBlank
@Size(min = 5, max = 100, message = "Please enter between {min} and {max} characters.")
private String password;
}
RegisterController.java
@RestController
public class RegisterController {
@Autowired
private UserService userService;
@RequestMapping(method = RequestMethod.POST, value = "/users")
public Response createUser(@Valid @RequestBody User user){
return userService.createUser(user);
}
}