Spring UserDetails服务引发UsernameNotFoundException
我们使用Spring引导和InMemoryAuthentication来存储用户。 在某些时候,我们需要检查特定用户的授权。 我们尝试通过获取UserDetailsService bean(自动连接)并调用loadUserByUsername(用户名)来实现这一点。但是,在所有尝试中都会抛出UsernameNotFoundException 要调试原因,请尝试在添加用户后立即在我们的WebSecurityConfigureAdapter的重写configureGlobal()中减少此操作:Spring UserDetails服务引发UsernameNotFoundException,spring,spring-boot,spring-security,Spring,Spring Boot,Spring Security,我们使用Spring引导和InMemoryAuthentication来存储用户。 在某些时候,我们需要检查特定用户的授权。 我们尝试通过获取UserDetailsService bean(自动连接)并调用loadUserByUsername(用户名)来实现这一点。但是,在所有尝试中都会抛出UsernameNotFoundException 要调试原因,请尝试在添加用户后立即在我们的WebSecurityConfigureAdapter的重写configureGlobal()中减少此操作: @A
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("user1").password("1234").roles("AGENT");
UserDetails ud = auth.inMemoryAuthentication().getUserDetailsService().loadUserByUsername("user1");
UsernameNotFoundException即使在内存中的用户被设置之后也会在那里抛出。
如前所述,代码中使用自动连线userDetailsServiceBean的其他地方也会发生同样的情况
如何让上述代码片段正常工作,有什么建议吗?或者一般来说,如何获取内存中用户的凭据
谢谢
注意:我们没有任何自定义UserDetailsService
更新
该类用@EnableWebSecurity注释:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
参考建议的链接,还尝试了其他样式:
@Autowired
public void whatever(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("user1").password("1234").roles("AGENT");
UserDetails ud = auth.inMemoryAuthentication().getUserDetailsService().loadUserByUsername("user1");
LOGGER.info(ud);
而且:
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("user1").password("1234").roles("AGENT");
UserDetails ud = auth.inMemoryAuthentication().getUserDetailsService().loadUserByUsername("user1");
LOGGER.info(ud);
但是,在以下情况中始终会引发异常:
Caused by: org.springframework.security.core.userdetails.UsernameNotFoundException: user1
at org.springframework.security.provisioning.InMemoryUserDetailsManager.loadUserByUsername(InMemoryUserDetailsManager.java:122) ~[spring-security-core-4.0.3.RELEASE.jar:4.0.3.RELEASE]
请共享您的spring配置类更新了带有类注释的问题您无法从
configureGlobal(AuthenticationManagerBuilder auth)
方法加载用户;UserDetailsService尚未初始化。您尚未提供引发的异常的完整堆栈跟踪。其中的信息将提供根本原因。是否在重写的“configure”方法中引发异常?