说到春天,我是相当绿色的。
尝试自动将以下连接在一起时收到此消息,但我还没有解决它。
org.springframework.beans.factory.BeanCreationException: Error creating bean wit h name 'authenticator': Injection of autowired dependencies failed; nested excep tion is org.springframework.beans.factory.BeanCreationException: Could not autow ire method: public void nz.co.myproject.auth.Authenticator.setAuthenticationManag er(org.springframework.security.authentication.AuthenticationManager); nested ex ception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No m atching bean of type [org.springframework.security.authentication.Authentication Manager] found for dependency: expected at least 1 bean which qualifies as autow ire candidate for this dependency. Dependency annotations: {}
弹簧配置
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="myUserDetailsService">
<password-encoder ref="encoder"/>
</authentication-provider>
</authentication-manager>
<beans:bean id="myUserDetailsService" class="nz.co.myproject.auth.MyUserDetailsService" />
<beans:bean id="authenticator" class="nz.co.myproject.auth.Authenticator"/>
<beans:bean id="encoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>
我的课
@Component
public class Authenticator {
/* Authentication manager does not inject successfully, instead gives error message */
@Autowired
@Qualifier("authenticationManager")
protected AuthenticationManager authenticationManager;
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
public void setAuthenticationManager(AuthenticationManager authenticationManager) {
this.authenticationManager = authenticationManager;
}
}
有什么作用
有趣的是,我只有在尝试将身份验证管理器注入 Authenticator 时才得到这个。当我尝试将身份验证管理器注入 HomeController 时,依赖项注入成功。
例如,以下工作:
弹簧配置
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="myUserDetailsService">
<password-encoder ref="encoder"/>
</authentication-provider>
</authentication-manager>
<beans:bean id="myUserDetailsService" class="nz.co.myproject.auth.MyUserDetailsService" />
<beans:bean id="encoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>
我的课
@Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/* authentication manager injects successfully */
@Autowired
@Qualifier("authenticationManager")
protected AuthenticationManager authenticationManager;
......
}
谁能看到我错过了什么?
完整的上下文配置
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx">
<!-- DispatcherServlet Context: defines this servlet's request-processing
infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<resources mapping="/resources/**" location="/resources/" />
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<!-- Scans within the base package of the application for @Components to configure as beans -->
<context:component-scan base-package="nz.co.myproject.controller" />
<context:component-scan base-package="nz.co.myproject.auth" />
<tx:annotation-driven />
<!-- We want to use spring's multipart resolver -->
<beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 10MB hard limit. Lower values can be handled by the servlets, but over 10MB is suspicious.-->
<beans:property name="maxUploadSize" value="10485760"/>
</beans:bean>
<beans:bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
<!-- Internationalisation -->
<mvc:interceptors>
<beans:bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<beans:property name="paramName" value="lang" />
</beans:bean>
</mvc:interceptors>
<beans:bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<beans:property name="basename" value="classpath:messages" />
<beans:property name="defaultEncoding" value="UTF-8" />
</beans:bean>
<beans:bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<beans:property name="paramName" value="lang" />
</beans:bean>
<beans:bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<beans:property name="defaultLocale" value="en" />
</beans:bean>
<beans:bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<beans:property name="interceptors">
<beans:ref bean="localeChangeInterceptor" />
</beans:property>
</beans:bean>
<!-- Email Messaging -->
<beans:bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<beans:property name="host" value="smtp.gmail.com" />
<beans:property name="username" value="*******"/>
<beans:property name="password" value="*******"/>
<beans:property name="port" value="587"/>
<beans:property name="javaMailProperties">
<beans:props>
<beans:prop key="mail.smtp.auth">true</beans:prop>
<beans:prop key="mail.smtp.starttls.enable">true</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<!-- this is a template message that we can pre-load with default state -->
<beans:bean id="templateMessage" class="org.springframework.mail.SimpleMailMessage">
<beans:property name="from" value="noreply@myproject.co.nz" />
<beans:property name="subject" value="Your order" />
</beans:bean>
<beans:bean id="smtpMailer" class="nz.co.myproject.messaging.smtp.SMTPMailer">
<beans:property name="mailSender" ref="mailSender" />
<beans:property name="templateMessage" ref="templateMessage" />
</beans:bean>
</beans:beans>
完整的安全配置
<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<debug />
<http auto-config="false">
<intercept-url pattern="/favicon.ico" access="ROLE_ANONYMOUS" />
<intercept-url pattern="/login" access="ROLE_ANONYMOUS" />
<intercept-url pattern="/myproject" access="ROLE_USER" />
<form-login login-page="/login" default-target-url="/"
authentication-failure-url="http://www.youtube.com/watch?v=BcL---4xQYA" />
<logout logout-success-url="/" />
</http>
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="myUserDetailsService">
<password-encoder ref="encoder"/>
</authentication-provider>
</authentication-manager>
<beans:bean id="myUserDetailsService" class="nz.co.myproject.auth.myUserDetailsService" />
<beans:bean id="encoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>
</beans:beans>