0

我不明白爲什麼我無法在身份驗證提供程序(文件:userDetailsS​​ervice)中使用服務層(文件:用戶服務)。結果我有下面的錯誤,但是當我使用存儲庫層(文件:庫)一切都很好。在自定義身份驗證提供程序(Spring Security 3.2)中使用服務層

我想封裝存儲庫層(Spring數據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)} 

的UserDetailsS​​ervice

@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; 
    } 

} 

的security.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> 

User_service

@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> 

回答

2

的問題是,春天沒有User_service豆的知識。

User_service bean未在您的上下文配置中聲明既不啓用組件註釋類的類路徑掃描。 (給你貼的配置)

嘗試用下面的XML聲明打開組件掃描(可能定義型屬性:

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

或上下文配置聲明中bean

希望這有助於!

+0

在webContent(aded web config)中已經打開了組件掃描,但是我不能理解上面的錯誤原因 – Mikhail

+0

@Mikhail不要在servlet上下文中聲明你的服務bean Servlet上下文可以訪問appl但不能反之亦然,所以bean不能從安全配置中訪問。而應在應用程序上下文中聲明它。 – gipinani

相關問題