在用户重点中可选的导致身份验证机制破裂

发布于 2025-01-22 23:35:44 字数 2212 浏览 0 评论 0原文

我正在使用Spring Boot以及Spring Security,在我的usersrepository中,我有以下代码:

public interface UsersRepository<T extends Users> extends CoreRepository<Users> {

    Optional<Users> findByEmail(String email);

}

并且,在我的websecurityconfig中/code>在配置方法中,我有以下操作:

@Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        
         auth.userDetailsService(
                username -> {
                    return usersRepository.findByEmail(username)
                            .orElseThrow(
                                    () -> new UsernameNotFoundException("Account not found for " + username)
                            )
                }
        );
        
    }

这弹出此错误:

当我添加try/catch(实际上不需要?)时,我会收到以下错误:

我知道这是因为usersrepository中的方法被声明为可选&lt;用户&gt; - 您可以使用.get()在检索对象后 - 但这不是我们在这里所做的。

我需要将userDetails的实现传递给auth.userdetailsservice()呼叫。

我需要做什么来满足这一点?

更新

这是编译器返回的错误:

[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /D:/Projects/sitebuild/src/main/java/com/zev/installations/config/WebSecurityConfig.java:[62,33] incompatible types: cannot infer type-variable(s) T
    (argument mismatch; bad return type in lambda expression
      java.lang.Object cannot be converted to org.springframework.security.core.userdetails.UserDetails)
[INFO] 1 error
[INFO] -------------------------------------------------------------

I am using spring boot along with spring security and in my UsersRepository, I have the following code:

public interface UsersRepository<T extends Users> extends CoreRepository<Users> {

    Optional<Users> findByEmail(String email);

}

And, in my WebSecurityConfig in the configure method, I have the following:

@Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        
         auth.userDetailsService(
                username -> {
                    return usersRepository.findByEmail(username)
                            .orElseThrow(
                                    () -> new UsernameNotFoundException("Account not found for " + username)
                            )
                }
        );
        
    }

This pops up with this error:
Try/Catch usage error

When I add a try/catch (which really shouldn't be necessary?), I get the following error:
bad type error

I know that this is because the method in the UsersRepository is declared as Optional<Users> - and you can retrieve the object by using .get() after the object is retrieved - but that is not what we are doing here.

I need to pass an implementation of UserDetails to the auth.userDetailsService() call.

What do I need to do to satisfy this?

Update

Here are the errors returned from the compiler:

[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /D:/Projects/sitebuild/src/main/java/com/zev/installations/config/WebSecurityConfig.java:[62,33] incompatible types: cannot infer type-variable(s) T
    (argument mismatch; bad return type in lambda expression
      java.lang.Object cannot be converted to org.springframework.security.core.userdetails.UserDetails)
[INFO] 1 error
[INFO] -------------------------------------------------------------

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小苏打饼 2025-01-29 23:35:44

满足UserDetails合同的最简单方法:

static class Users extends User {
    // ... Implement constructors 
}

使用此小更改,代码编译确定。

另一个是使用Spring Security提供的用户而不是用户。

The simplest way to satisfy UserDetails contract:

static class Users extends User {
    // ... Implement constructors 
}

With this small change the code compiles OK.

Another, is to use Spring Security provided User instead of Users.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文