关于SpringSecurity配置文件中自定义userDetailsService报错

报错:

Field userDetailsService in com.zhao.security.config.WebSecurityConfig required a bean of type 'org.springframework.security.core.userdetails.UserDetailsService' that could not be found.
The injection point has the following annotations:
	- @org.springframework.beans.factory.annotation.Autowired(required=true)

The following candidates were found but could not be injected:
	- Bean method 'inMemoryUserDetailsManager' in 'UserDetailsServiceAutoConfiguration' not loaded because @ConditionalOnMissingBean (types: org.springframework.security.authentication.AuthenticationManager,org.springframework.security.authentication.AuthenticationProvider,org.springframework.security.core.userdetails.UserDetailsService,org.springframework.security.oauth2.jwt.JwtDecoder,org.springframework.security.oauth2.server.resource.introspection.OpaqueTokenIntrospector; SearchStrategy: all) found beans of type 'org.springframework.security.authentication.AuthenticationManager' authenticationManager


Action:

Consider revisiting the entries above or defining a bean of type 'org.springframework.security.core.userdetails.UserDetailsService' in your configuration.

原因:

自定义业务对象,但是引入的是org.springframework.security.core.userdetails.中的userDetailsService。导致找不到userDetailsService。

办法1:(不保留自定义)取消自定义对象类,在自定义对象类实现方法中直接导入官方的对象类

 

办法2:(保留自定义)在自己写的自定义对象类中继承官方的对象类

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
根据提供的引用内容,Spring Security提供了UserDetails的实现类User和UserDetailService的作用。在自定义UserDetailsService时,如果无法进入自定义UserDetailsService,可能是由于以下原因: 1. 配置错误:请确保在Spring Security配置文件正确配置了自定义UserDetailsService。可以通过以下方式进行配置: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsService userDetailsService; @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService); } // 其他配置... } ``` 2. 自定义UserDetailsService实现类错误:请确保自定义UserDetailsService实现类正确实现了loadUserByUsername方法,并且能够根据用户名从数据库获取用户信息并封装为UserDetails的实现类返回。例如: ```java @Service public class CustomUserDetailsService implements UserDetailsService { @Autowired private UserRepository userRepository; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { User user = userRepository.findByUsername(username); if (user == null) { throw new UsernameNotFoundException("User not found"); } return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), user.getAuthorities()); } } ``` 3. 数据库查询错误:请确保数据库存在与用户名匹配的用户信息,并且能够正确查询到用户信息。 如果以上步骤都正确配置和实现,但仍然无法进入自定义UserDetailsService,可能需要进一步检查日志或调试代码以确定具体原因。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

子书棋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值