【发布时间】:2015-07-17 21:59:13
【问题描述】:
我是 spring-security 的新手。我对 @Autowired 注释有疑问。
下面是我的 spring-security.xml 文件:
<http pattern="/login" security="none" />
<http pattern="/signup" security="none" />
<http pattern="/views/**" security="none" />
<http pattern="/createUser" security="none" />
<http pattern="/practice" security="none" />
<http use-expressions="true" authentication-manager-ref="authenticationManager">
<intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
<form-login login-page="/login" default-target-url="/home"
authentication-failure-url="/login" username-parameter="username"
password-parameter="password" login-processing-url="/j_spring_security_check" />
<logout logout-success-url="/login" invalidate-session="true"
logout-url="/j_spring_security_logout" delete-cookies="JSESSIONID" />
<session-management invalid-session-url="/login">
<concurrency-control max-sessions="1"
expired-url="/login" />
</session-management>
</http>
<authentication-manager id="authenticationManager">
<authentication-provider user-service-ref="userDetailsService" />
</authentication-manager>
<beans:bean id="userDetailsService" class="com.practice.service.UserDetailsServiceImpl" />
<beans:bean name="
passwordEncoder "
class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" />
这是我的 spring-servlet.xml 配置:
<context:annotation-config/>
<context:component-scan base-package="com.practice" />
<mvc:resources mapping="/views/**" location="/views/" />
<mvc:annotation-driven />
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations"
value="classpath:com/practice/resources/database.properties"></property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.username}" />
<property name="password" value="${database.password}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:com/practice/resources/hibernate.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
我会请求注册页面,并在填写必要的详细信息后,它会点击我的控制器,我将用户保存在数据库中,然后我在针对我存储在数据库中的请求中对用户进行身份验证。我按照这个stackoverflow post 寻求指导。
奇怪的是,在我实现 UserDetailsService 接口的类中没有发生自动装配:
@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {
@Autowired
PasswordEncoder passwordEncoder;
@Autowired
UserService userService;
@Override
public UserDetails loadUserByUsername(String arg0)
throws UsernameNotFoundException {
System.out.println("ddfddd");
return null;
}}
而自动装配发生在其他服务类中,我自动装配了与上述相同的字段!
当我登录时也会发生同样的事情,当我调试时,字段为空!以及在我编写 CustomAuthenticationProvider 的课程中!
有什么建议吗??如果我的配置有任何错误,请纠正我。
更新
我收到以下错误,
原因:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖找到[com.practice.service.UserService]类型的合格bean:预计至少有1个bean可以作为此依赖的自动装配候选者。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
当我删除 @Service 注释并添加 context:annotation-config
【问题讨论】:
-
从
UserDetailsServiceImpl中删除@Service并将<context:annotation-config />添加到spring-security.xml文件中。 -
@Deinum ,我收到以下错误,原因是:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖找到[com.practice.service.UserService]类型的合格bean:预期至少 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
-
请不要将错误、代码等添加为cmets,更新您的问题。
-
好吧,你还没有定义任何实现接口
UserService的bean,所以添加一个bean就可以了。 -
如果你定义了一个带有
@Component注释的UserServicebean,它被spring-servlet.xml 中的组件扫描拾取,那么问题可能是那个spring-servlet。 xml 是来自 spring-security.xml 的子应用程序上下文。在这种情况下,安全上下文中的 bean 无法访问子上下文中的任何 bean,反之亦然。
标签: java spring spring-mvc spring-security autowired