【问题标题】:spring secutiry custom xhtml. authenticationManager always is null弹簧安全自定义 xhtml。 authenticationManager 始终为空
【发布时间】:2014-12-31 02:08:42
【问题描述】:

我需要创建自定义 xhtml 登录表单。配置spring没有xml only注解,但是authenticationManager总是为null

登录表单

<!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>

配置

@Configuration
@EnableWebSecurity
@EnableGlobalAuthentication
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable().authorizeRequests()
            .antMatchers("/home", "/css/**", "/**/*.css*", "/").permitAll()
            .anyRequest().authenticated().and().formLogin()
            .loginPage("/login").permitAll().and().logout()
            .logoutUrl("/logout").invalidateHttpSession(true)
            .logoutSuccessUrl("/");
}
@Override
protected void configure(AuthenticationManagerBuilder auth)
        throws Exception {
    auth.inMemoryAuthentication().withUser("user").password("password")
            .roles("USER");
}

@Bean
public AuthenticationManager authenticationManager() throws Exception {
    return super.authenticationManagerBean();

}
}

在 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总是为null。如何注入 authenticationManager ?

我尝试的那些 @ovverride authenticationManagerBean 没有 我找到了 spring security giude 但我不想使用百里香

感谢您的帮助

【问题讨论】:

  • 您将 CDI 和 Spring 混合使用,这是行不通的。您最终会得到由不同容器管理的不同实例。要使用 @ManagedProperty,您必须使用 JSF 托管 bean 而不是 @Named 使用 @ManagedBean
  • 是的,当我使用@ManagedProperty 时,我使用@ManagedBean@RequestScoped
  • 确保您已正确设置 Spring JSF 集成,否则它将无法工作。此外,如果 bean 是 null,我希望在 @ManagedBean 的情况下会出现异常,类似于找不到 bean。
  • 认证成功后如何将用户重定向到想要的页面?

标签: spring spring-security


【解决方案1】:

您的 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>

【讨论】:

  • 这个答案没有意义,尤其是在基于 JSF 的应用程序中。你只需要ContextLoaderListener
  • 我正在尝试添加没有 applicationContext 的 ContextLoaderListener 但我总是得到 Cannot initialize context because there is already a root application context present new question
猜你喜欢
  • 2013-10-15
  • 2011-06-30
  • 2015-11-06
  • 2013-08-08
  • 1970-01-01
  • 2014-05-30
  • 2023-01-03
  • 2019-02-12
  • 2018-09-08
相关资源
最近更新 更多