spring secutiry自定义xhtml。authenticationManager始终为空

spring secutiry自定义xhtml。authenticationManager始终为空,spring,spring-security,Spring,Spring Security,我需要创建自定义的xhtml登录表单。配置spring时不带仅xml注释,但authenticationManager始终为空 登录表单 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:p="http://primefaces.org/ui" xmlns:h="http://java.sun.com/jsf/html" xml

我需要创建自定义的xhtml登录表单。配置spring时不带仅xml注释,但authenticationManager始终为空

登录表单

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org" xmlns:p="http://primefaces.org/ui"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Spring Security Example</title>
</head>
<body>

<h:form prependId="false"   >
    <h:outputLabel value="User Id: " for="j_username" />
    <h:inputText id="j_username" label="User Id" required="true"
        value="#{loginBean.userName}" />
    <h:outputLabel value="Password: " for="j_password" />
    <h:inputSecret id="j_password" value="#{loginBean.password}" />
    <h:commandButton value="Submit" action="#{loginBean.login}" />
    </h:form>
    </body>
    </html>
想要在LoginBean类中使用
@Autowired

 import java.io.IOException;
 import java.io.Serializable;

 import javax.faces.bean.ManagedProperty;
 import javax.inject.Named;
 import javax.servlet.ServletException;

 import org.springframework.context.annotation.Scope;
 import org.springframework.security.authentication.AuthenticationManager;
 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
 import org.springframework.security.core.Authentication;
 import org.springframework.security.core.AuthenticationException;
 import org.springframework.security.core.context.SecurityContextHolder;
 import org.springframework.stereotype.Component;
@Scope("request")
@Component
@Named(value="loginBean")
public class LoginBean implements Serializable {


@Autowired
private AuthenticationManager authenticationManager;

private static final long serialVersionUID = 1L;
private String userName;
private String password;


public String login() {
    try {
        Authentication request = new UsernamePasswordAuthenticationToken(
                this.getUserName(), this.getPassword());
        Authentication result = authenticationManager.authenticate(request);
        SecurityContextHolder.getContext().setAuthentication(result);
    } catch (AuthenticationException e) {
        e.printStackTrace();
    }
    return "secured";
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public AuthenticationManager getAuthenticationManager() {
    return authenticationManager;
}

public void setAuthenticationManager(
        AuthenticationManager authenticationManager) {
    this.authenticationManager = authenticationManager;
}
}

我正在尝试注入authenticationManager

 @ManagedProperty(value = "#{authenticationManager}")
private AuthenticationManager authenticationManager;
或者创建构造函数

    @Autowired
 public LoginBean(AuthenticationManager authenticationManager) {
this.authenticationManager = authenticationManager;
}
或者在setter中添加
@Autowired
,authenticationManager始终为空。如何注入authenticationManager

我试过的那些@ovverride什么都没有 我找到了spring security,但我不想使用thymeleaf


感谢您的帮助

您的web.xml中是否有以下条目?如果您使用@Configuration,那么它是必需的

<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
    </init-param>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
           com.yourClass
        </param-value>
    </init-param>
</servlet>

appServlet
org.springframework.web.servlet.DispatcherServlet
上下文类
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
上下文配置位置
com.yourClass

您将CDI和Spring混用,这是行不通的。最终会得到由不同容器管理的不同实例。要使用
@ManagedProperty
您必须使用JSF托管bean而不是
@Named
使用
@ManagedBean
。是的,当我使用
@ManagedProperty
时,我使用
@ManagedBean
@RequestScoped
确保您已正确设置Spring JSF集成,否则它将无法工作。另外,如果bean为
null
,我希望在
@ManagedBean
的情况下会出现异常,即找不到任何bean。在成功验证后,如何将用户重定向到所需页面?这个答案没有意义,尤其是在基于JSF的应用程序中。您只需要
ContextLoaderListener
。我正在尝试在没有
applicationContext
的情况下添加ContextLoaderListener,但我始终获得
无法初始化上下文,因为已经存在根应用程序上下文
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
    </init-param>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
           com.yourClass
        </param-value>
    </init-param>
</servlet>