【问题标题】:Using the service layer in custom authentication-provider ( Spring security 3.2)在自定义身份验证提供程序中使用服务层(Spring security 3.2)
【发布时间】:2014-07-30 11:26:04
【问题描述】:

我不明白为什么我不能在身份验证提供者(文件:userDetailsS​​ervice)中使用服务层(文件:Userservice)。结果我有下面的错误,但是当我使用 Repository layer(file: Repository) 时一切都很好。

我想封装 Repository 层(Spring data Jpa)并仅使用 Service 层来满足所有需求。为什么在这种情况下不可能?

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.DefaultSecurityFilterChain#1': Cannot resolve reference to bean 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0' while setting constructor argument with key [3]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0': Cannot resolve reference to bean 'org.springframework.security.authentication.ProviderManager#0' while setting bean property 'authenticationManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authentication.ProviderManager#0': Cannot resolve reference to bean 'org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authenticationManager': Cannot resolve reference to bean 'daoAuthenticationProvider' while setting constructor argument with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'daoAuthenticationProvider' defined in class path resource [spring/applicationContext/applicationContext-security.xml]: Cannot resolve reference to bean 'authService' while setting bean property 'userDetailsService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'authService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.dubovskiyM.mvc.service.User_service com.dubovskiyM.mvc.security.AuthServiceImpl.user_service; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.dubovskiyM.mvc.service.User_service] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

用户详细信息服务

@Service
public class AuthServiceImpl implements UserDetailsService {

    private final Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    User_service user_service;

    @Transactional
    @Override
    public UserDetails loadUserByUsername(String username)
            throws UsernameNotFoundException {
        log.trace("START-------------------------------------");
        Signup details = user_service.findByLogin(username);
        log.trace(details.getLogin()+"---------------------"+details.getPassword());
        Collection<SimpleGrantedAuthority> authorities = new ArrayList<SimpleGrantedAuthority>();
        SimpleGrantedAuthority userAuthority = new SimpleGrantedAuthority(
                "ROLE_USER");
        SimpleGrantedAuthority adminAuthority = new SimpleGrantedAuthority(
                "ROLE_ADMIN");
        if (details.getRole() == 1)
            authorities.add(userAuthority);
        else if (details.getRole() == 2) {
            authorities.add(userAuthority);
            authorities.add(adminAuthority);
        }
        UserDetails user = new User(details.getLogin(),
                details.getPassword(), true, true, true, true, authorities);
        return user;
    }

}

安全性.xml

<beans:beans xmlns="http://www.springframework.org/schema/security"
             xmlns:beans="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security.xsd">



    <http pattern="/resources/**" security="none" />
    <http auto-config="true" use-expressions="true">
        <intercept-url pattern="/"
                       access="permitAll"/>
        <intercept-url pattern="/login"
                       access="permitAll"/>
        <intercept-url pattern="/logout"
                       access="permitAll"/>
        <intercept-url pattern="/errors/**"
                       access="permitAll"/>
        <intercept-url pattern="/com/dubovskiyM/mvc"
                       access="hasRole('ROLE_USER')"/>
        <access-denied-handler error-page="/errors/403"/>
        <form-login login-page="/login"
                    login-processing-url="/j_spring_security_check"
                    username-parameter="j_username"
                    password-parameter="j_password"
                    authentication-failure-url="/login/form?error"
                    default-target-url="/default"/>
    </http>

    <authentication-manager>
        <authentication-provider ref="daoAuthenticationProvider">
        </authentication-provider>
    </authentication-manager>

    <beans:bean id="daoAuthenticationProvider"
                class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
        <beans:property name="userDetailsService" ref="authService" />
        <!--<beans:property name="passwordEncoder" ref="passwordEncoder" />-->
    </beans:bean>

    <beans:bean id="authService"
                class="com.dubovskiyM.mvc.security.AuthServiceImpl" />

    <!--<beans:bean id="passwordEncoder"
                class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" />-->


</beans:beans>

用户服务

@Service
@Transactional
public class User_service   {

@Autowired
private User_repository user_repository;


@Transactional(readOnly = false)
public void save(Signup signup){
    user_repository.save(signup);
}


@Transactional(readOnly = true)
public Signup findByLogin(String login){
Signup signup = user_repository.findByLogin(login);
return signup;
}

@Transactional(readOnly = true)
public Signup findByLoginAndPassword(String login,String password){
    Signup signup = user_repository.findByLoginAndPassword(login, password);
    return signup;
}

}

存储库

@Repository
public interface User_repository extends JpaRepository<Signup,Long>  {

    Signup findByLogin(String login);
    Signup findByLoginAndPassword(String login,String password);

}

servet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
    <!-- Scan for components under this package -->
    <!-- Scan for components under this package expept Repository -->
    <context:component-scan base-package="com.dubovskiyM.mvc">
        <context:exclude-filter expression="org.springframework.stereotype.Repository" type="annotation"/>
    </context:component-scan>
<!--    &lt;!&ndash; Ensures that any resource requests not handled by Spring MVC mappings will be delegated back to the Servlet container &ndash;&gt;
    <mvc:default-servlet-handler />-->
    <!--Enable jpa for repository access-->
</beans>

【问题讨论】:

    标签: spring spring-mvc spring-security spring-data service-layer


    【解决方案1】:

    问题是spring不知道User_service bean。

    User_service bean 未在您的上下文配置中声明,您也未启用组件注释类的类路径扫描。 (鉴于您发布的配置)

    尝试使用以下 xml 声明打开组件扫描(可能定义一个构造型属性:

    <context:component-scan base-package="your.package" />
    

    或在上下文配置中声明您的 bean。

    希望这会有所帮助!

    【讨论】:

    • 在 webContent(添加的 web 配置)中打开了组件扫描。但我无法理解上述错误的原因。
    • @Mikhail 不要在 servlet 上下文中声明您的服务 bean。 Servlet 上下文可以访问应用程序上下文,但反之则不行,因此无法从安全配置中访问 bean。而是在您的应用程序上下文中声明它。
    猜你喜欢
    • 1970-01-01
    • 2011-02-09
    • 1970-01-01
    • 2011-06-21
    • 2011-01-03
    • 2016-07-19
    • 2015-08-16
    • 2023-03-05
    • 2016-09-24
    相关资源
    最近更新 更多