@Autowired AuthenticationManager为null(JavaFX + Spring Security)

时间:2014-12-10 11:25:08

标签: java spring spring-mvc spring-security javafx

我有一个JavaFX桌面应用程序,需要使用Spring Security对数据库进行登录验证。 AuthenticationManager未正确自动装配并抛出NullPointerException。我在网络应用程序中重用了大部分代码,这些代码工作正常,但不会在这里。

LoginController.java

@Component
public class LoginController{

private boolean userAuthenticated = true;
@Autowired
@Qualifier("authenticationManager")
private AuthenticationManager authenticationManager;

@FXML
public void login() {
        Authentication authToken = new UsernamePasswordAuthenticationToken(
            emailField.getText(), passField.getText());
    try {
        System.out.println(authenticationManager);
        authToken = authenticationManager.authenticate(authToken);  // throws NullPointer
        SecurityContextHolder.getContext().setAuthentication(authToken);
    } catch (Exception e) {
        e.printStackTrace();
    }

的applicationContext.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-3.0.xsd
       http://www.springframework.org/schema/security
       http://www.springframework.org/schema/security/spring-security-3.2.xsd">
<context:component-scan>
    <bean id="LoginController" class="controller.LoginController">
        <property name="authenticationManager" ref="authenticationManager" />
    </bean>
    <authentication-manager alias="authenticationManager">
        <authentication-provider user-service-ref="userService">
            <password-encoder ref="bCryptPasswordEncoder" />
        </authentication-provider>
    </authentication-manager>
</beans:beans>

0 个答案:

没有答案