【问题标题】:Error creating bean with name 'webSecurityConfig创建名为“webSecurityConfig”的 bean 时出错
【发布时间】:2021-09-21 17:42:03
【问题描述】:

当我运行我的 spring boot 应用程序时,我收到以下错误:

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“webSecurityConfig”的bean时出错:通过字段“userDetailsS​​ervice”表示的依赖关系不满足;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“org.springframework.security.core.userdetails.UserDetailsS​​ervice”类型的合格 bean 可用:预计至少有 1 个有资格作为自动装配候选者的 bean。依赖注解:{@org.springframework.beans.factory.annotation.Qualifier("userDetailsS​​erviceImpl"), @org.springframework.beans.factory.annotation.Autowired(required=true)}

请帮我解决这个错误...

【问题讨论】:

  • 我认为您应该根据需要声明一个返回 UserDetailsS​​ervice 的 bean 配置。

标签: spring spring-boot


【解决方案1】:

只需根据错误的需要实现一个 userService 即可。 您应该根据您的身份验证行为真正实施一个(例如在您的公司数据库中查找用户)。并将其添加到您的 Spring 上下文中(这对于您的 componentScan 中的 @Service 是自动的)。

package com.company.example; // TODO: change this according to your project

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tudu.domain.Role;
import tudu.domain.User;
import tudu.service.UserService;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Set;

/**
 * An implementation of Spring Security's UserDetailsService.
 * 
 * @author Julien Dubois
 */
@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {

    private final Log log = LogFactory.getLog(UserDetailsServiceImpl.class);

    @Autowired
    private UserService userService;

    /**
     * Load a user for Spring Security.
     */
    @Transactional
    public UserDetails loadUserByUsername(String login)
            throws UsernameNotFoundException, DataAccessException {

        login = login.toLowerCase();
        if (log.isDebugEnabled()) {
            log.debug("Security verification for user '" + login + "'");
        }
        User user;
        try {
            user = userService.findUser(login);
        } catch (ObjectRetrievalFailureException orfe) {
            throw new UsernameNotFoundException("User '" + login
                    + "' could not be found.");
        }
        user.setLastAccessDate(Calendar.getInstance().getTime());

        Set<Role> roles = user.getRoles();

        Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
        for (Role role : roles) {
            authorities.add(new GrantedAuthorityImpl(role.getRole()));
        }

        return new org.springframework.security.core.userdetails.User(login,
                user.getPassword(), user.isEnabled(), true, true, true,
                authorities);
    }
}

【讨论】:

    猜你喜欢
    • 2022-01-12
    • 1970-01-01
    • 2019-08-02
    • 2021-11-28
    • 2020-10-03
    • 2019-08-28
    • 2015-03-18
    • 2021-03-17
    • 2021-06-18
    相关资源
    最近更新 更多