spring 安全 StackOverflow 错误

spring security StackOverflow Error

我正在 spring 引导应用程序中实施安全性并收到 Whosebug 异常。 我有一个用户 pojo 和存储库来将用户保存在工作正常的数据库中。

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{

    @Autowired
    private UserDetailsService userDetailsService;
    ....
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
        .userDetailsService(userDetailsService); }

public class User {

    private String password;

    public User(User user) {
        this.password = user.password;
        ...
    }

    public String getPassword() {
        return password;
    }

期待用户的 getPassword() 在下面被调用但没有发生 -

public class MyUserDetails extends User implements UserDetails{
    ........
    @Override
    public String getPassword() {
        return getPassword();
}

UserDetailsS​​ervice 的实施 -

@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
    User user = userRepository.findByEmail(email);
    if (user == null)
         throw new UsernameNotFoundException("no " + email);
    return new MyUserDetails(user);
}

我通过调用 userRepository.save(user) 在数据库中插入了一个 user/password。 当我尝试传递无效的 user/password 时,它会正确返回无效的用户消息。 但是当我尝试将 user/password 存储在数据库中时,我得到以下异常 -

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Filter execution threw an exception] with root cause

java.lang.WhosebugError: null
    at com.xyz.MyUserDetails.getPassword(MyUserDetails.java:27) ~[classes/:na]

看一下MyUserDetails.getPassword()方法,你可以看到它会调用自己...无限...

public class MyUserDetails extends User implements UserDetails{
    ........
    @Override
    public String getPassword() {
        return getPassword(); // <= infinite recursion here
}

是否有您想要 return 具有相似名称的变量,或者可能调用不同的方法(来自超级 class.. 或不是..)?