0

我试图实现基于弹簧安全的自定义授权。这是config.xml:

<global-method-security pre-post-annotations="enabled" />
    <http use-expressions="true">
        <form-login login-page="/wellcome/" login-processing-url="/login" default-target-url="/" 
            username-parameter="email" password-parameter="password" />
        <remember-me key="skyhandling" token-validity-seconds="-1" />
        <logout invalidate-session="true" logout-success-url="/" logout-url="/logout"/>
        <intercept-url pattern="/administration/**" access="authenticated"/>
        <intercept-url pattern="/wellcome/" access="permitAll"/>
        <intercept-url pattern="/login" access="permitAll"/>
        <intercept-url pattern="/css/**" access="permitAll"/>
        <intercept-url pattern="/images/**" access="permitAll"/>
        <intercept-url pattern="/javascript/**" access="permitAll"/>
    </http>
    <beans:bean id="authenticationProvider"
        class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
        <beans:property name="userDetailsService" ref="userDetailsService"/>
    </beans:bean>
    <beans:bean id="authenticationManager"
        class="org.springframework.security.authentication.ProviderManager">
        <beans:property name="providers">
            <beans:list>
                <beans:ref local="authenticationProvider" />
            </beans:list>
        </beans:property>
    </beans:bean>
    <beans:bean class="com.test.service.UserDetailsExtendedService" id="userDetailsService" />
    <beans:bean class="org.springframework.security.authentication.encoding.ShaPasswordEncoder" 
        id="passwordEncoder" />
    <authentication-manager>
        <authentication-provider user-service-ref="userDetailsService">
            <password-encoder ref="passwordEncoder" />
        </authentication-provider>
    </authentication-manager>

用户详情服务:

@Service("userDetailsService")
@SuppressWarnings("deprecation")
public class UserDetailsExtendedService implements UserDetailsService {
    @Autowired
    private UsersDAO dao;

    /**
     * 
     * @param user
     * @return
     */
    private User prepare(com.test.User user) {
        boolean enabled = user.getState().equals(UserState.Active);

        Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
        for (UserRole r: user.getRoles()) {
          authorities.add(new GrantedAuthorityImpl(r.getName()));
        }

        return new UserDetailsExtended(user.getEmail(), user.getPassword(), user.getNickname(), enabled,
            enabled, enabled, enabled, authorities);
    }

    /**
     * 
     * @param email
     * @return
     */
    @Transactional(readOnly = true)
    public User loadUserByUsername(final String email) 
            throws UsernameNotFoundException, DataAccessException {
        com.test.User user = dao.getByEmail(email);
        if (user == null)
            throw new UsernameNotFoundException(email);

        return prepare(user);
    }
}

一切正常。但是当我添加

@Autowired 
private UserDetailsExtendedService useDetailsService;

进入控制器类,应用程序开始失败:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.test.service.UserDetailsExtendedService] 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)}

我错过了什么?谢谢

更新#1:

 <context:annotation-config />
    <context:component-scan base-package="com.test.service" />
    <bean id="usersDao" class="com.test.dao.UsersDAO" />
    <bean id="eventsLogDao" class="com.test.dao.EventsLogDAO" />
    <bean id="employeesDao" class="com.test.dao.EmployeesDAO" />
    <bean id="dictionariesDao" class="com.test.dao.DictionariesDAO" />
4

2 回答 2

0

在您的配置中,您有一个com.ejl.skyhandling.service.UserDetailsExtendedService但在您的 bean 中您试图访问com.test.service.UserDetailsExtendedService这显然是另一种类型。

请检查您的进口。

于 2012-08-07T11:20:45.137 回答
0

由于您使用值“userDetailsS​​ervice”来限定@Service,请确保@Autowired 是

@Autowired
private UserDetailsExtendedService userDetailsService;

您目前在变量名中缺少 r -> useDetailsS​​ervice

于 2012-08-07T19:35:11.480 回答