在用户重点中可选的导致身份验证机制破裂
我正在使用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)
)
}
);
}
When I add a try/catch (which really shouldn't be necessary?), I get the following 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
满足UserDetails合同的最简单方法:
使用此小更改,代码编译确定。
另一个是使用Spring Security提供的用户而不是用户。
The simplest way to satisfy UserDetails contract:
With this small change the code compiles OK.
Another, is to use Spring Security provided User instead of Users.