Spring security 将JSF登录页面与Spring安全性集成

Spring security 将JSF登录页面与Spring安全性集成,spring-security,jsf-2.2,Spring Security,Jsf 2.2,我正在尝试将JSFweb应用程序与Spring集成 安全性 目前,我通过一种方法登录:在这个 方法,并根据用户重定向到目标页面 登录页面(Login.xhtml): <h:form id="login"> <h:outputLabel for="email" value="E-mail: "/> <p:inputText id="email" value="#{loginManagedBean.usuario.email}" required="tr

我正在尝试将JSFweb应用程序与Spring集成 安全性

目前,我通过一种方法登录:在这个 方法,并根据用户重定向到目标页面

登录页面(Login.xhtml):

 <h:form id="login">
   <h:outputLabel for="email" value="E-mail: "/> 
   <p:inputText id="email" value="#{loginManagedBean.usuario.email}" required="true"/>
   <p:message for="email"/>

   <h:outputLabel for="pass" value="Contraseña: "/>                
   <p:password id="pass" value="#{loginManagedBean.usuario.password}" required="true"/>
   <p:message for="pass"/>

   <!-- <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/> -->

   <p:commandButton value="Login" update="@form" action="#{loginManagedBean.autenticar()}"/>
 </h:form>
@Override
protected void configure(HttpSecurity http) throws Exception {

    //.csrf() is optional, enabled by default, if using WebSecurityConfigurerAdapter constructor
    // Have to disable it for POST methods:
    // http://stackoverflow.com/a/20608149/1199132
    http.csrf().disable();

    // Logout and redirection:
    // http://stackoverflow.com/a/24987207/1199132
    http
            .logout()
            .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
            .deleteCookies("JSESSIONID")
            .invalidateHttpSession(true)
            .logoutSuccessUrl("/login.xhtml");

    http
            .authorizeRequests()
            //Permit access for all to error and denied views
            .antMatchers("/WEB-INF/errorpages/general.xhtml", "/WEB-INF/errorpages/accessDenied.xhtml", "/WEB-INF/errorpages/expired.html", "/login.xhtml")
            .permitAll()
            // Only access with admin role
            .antMatchers("/admin/**")
            .hasRole("ADMIN")
            //Permit access only for some roles
            .antMatchers("/alumno/**")
            .hasRole("ALUMNO")
            //Permit access only for some roles
            .antMatchers("/profesor/**")
            .hasRole("PROFESOR")
            //If user doesn't have permission, forward him to login page
            .and()
            .formLogin()
            .loginPage("/login.xhtml")
            .usernameParameter("login:email")
            .passwordParameter("login:pass")
            .loginProcessingUrl("/login") //
            .defaultSuccessUrl("/admin/homeAdmin.xhtml")
            .and()
            .exceptionHandling()
            .accessDeniedPage("/WEB-INF/errorpages/accessDenied.xhtml");
}

我不希望使用JSF进行身份验证,而是使用一个简单的HTML表单(以前是一个身份验证入口点):


乌萨里奥
康瑟斯尼娜
登录

这将执行到Spring安全认证入口点的POST。用户登录后,您可以使用或默认目标url重定向到JSF应用程序。

登录按钮操作应首先执行重定向,如下所示,并将流传递给spring security

ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
RequestDispatcher dispatcher = ((ServletRequest) context.getRequest()).getRequestDispatcher("/login");
dispatcher.forward((ServletRequest) context.getRequest(), (ServletResponse) context.getResponse());
FacesContext.getCurrentInstance().responseComplete();

请不要把你所有的代码都放在这里。只需输入导致问题的代码或您不理解的代码部分。否则,它就太无聊了,无法回答。Spring安全性可以通过URL进行配置。除此之外,它还提供了一个身份验证端点,因此您可以执行到该身份验证端点的POST(使用普通表单而不是JSF表单),登录后,导航到您的应用程序urlsolution@Kukeltje,仍然可以为样式类提供输入。纯JSFish解决方案将涉及为登录过程实现JSFbean,并将信息重新发送到Spring端点。即使这两个看起来像一个黑客,我认为这更简单。是的,但是你已经回答了所有在2017;市场营销部门不应该被忽视(prime ui可以在这里提供帮助…)对我来说,我使用这个版本时,还需要为发布的表单变量取消表单前缀。对于这一点,我有一个问题:登录后,我如何使用类“Users”中的参数,因为它不使用托管bean?在“欢迎”页面上,欢迎消息是“欢迎#{users.username}”(与打印的内容完全相同)。我使用类“Users.java”,它包含注释“ManagedBean”和“SessionScoped”。
ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
RequestDispatcher dispatcher = ((ServletRequest) context.getRequest()).getRequestDispatcher("/login");
dispatcher.forward((ServletRequest) context.getRequest(), (ServletResponse) context.getResponse());
FacesContext.getCurrentInstance().responseComplete();