当前位置: 首页 > 知识库问答 >
问题:

将JSF登录页面与Spring Security集成

仰城
2023-03-14

我正在尝试将JSF Web应用程序与Spring Security集成。

目前我正在通过一种方法登录:在此方法中进行身份验证并根据用户重定向到目标页面。

登录页面(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>

loginManagedBean。autenticar()(身份验证和重定向的方法):

如何替换此页面和方法以使用SpringSecurity?

SpringSecurityConfig:

@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");
}

共有2个答案

呼延曜灿
2023-03-14

登录按钮操作应该首先像这样执行重定向,并将流传递给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();
阎昌勋
2023-03-14

我不希望使用JSF进行身份验证,而是使用普通HTML表单(之前配置了身份验证入口点):

<form action="#{request.contextPath}/j_spring_security_check"
    method="post">
    <h:panelGrid styleClass="centered tight-grid" columns="2">
        <p:outputLabel>Usuario</p:outputLabel>
        <input type="text" id="username" name="username"
            required="required" />
        <p:outputLabel>Contraseña</p:outputLabel>
        <input type="password" id="password" name="password"
            required="required" />
    </h:panelGrid>
    <h:panelGrid>
        <button type="submit">
            <span class="ui-button-text">Login</span>
        </button>
    </h:panelGrid>
</form>

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

 类似资料:
  • 我将OpenId4Java与JSF应用程序一起使用,我在web.xml中添加了安全约束,如下所示。 现在,当应用程序启动并且用户希望访问 http://www.somehost:8080/myapp/core/abc.xhtml 在web.xml的帮助下,我可以向用户显示一个登录页面,现在这里我用openid登录,就像Google、Yahoo现在我的问题是,我如何告诉openid我的返回url是

  • 问题内容: 我有一个主要使用Richfaces的JSF应用程序。我想介绍一个功能更丰富的网格控件,例如基于jQuery的jqgrid。是否可以从JSF支持bean获得JSON数据?如果没有,是否有替代方法? 问题答案: 这里可能有几个选择。 Bean方法可以返回一个字符串,它只是一个JSON字符串 您可以使用JsonLib将Java对象转换为Json。 使用其他Servlet服务网格请求 我将推荐

  • 我试图从jsf启动激活引擎 引擎总是以null返回,我读到activiti.cfg.xml文件应该在类路径中,我已经把文件放在web-inf/class下,但是引擎仍然返回null,这意味着激活不能初始化引擎,有什么想法吗?

  • 我试图实现OIDC与Spring Boot-OKTA…我能够运行应用程序,一切都很好…我的问题在这里,是否有任何方法来覆盖默认的Spring Boot登录页面,在那里它将提供OKTA/任何OIDC提供商的链接,它将带到OKTA/相应的登录页面…我需要帮助覆盖/绕过默认的Spring Boot登录页面,当击中任何应用uri时,它应该直接带到OKTA/相应的OIDC Proverer登录页面… 请让我

  • 本文向大家介绍SpringSecurity 默认表单登录页展示流程源码,包括了SpringSecurity 默认表单登录页展示流程源码的使用技巧和注意事项,需要的朋友参考一下 SpringSecurity 默认表单登录页展示流程源码 本篇主要讲解 SpringSecurity提供的默认表单登录页 它是如何展示的的流程, 涉及 1.FilterSecurityInterceptor, 2.Excep

  • 每当发出GET请求时,keycloak调试将显示以下日志: O.K.Adapters.oAuthRequestAuthenticator:发送重定向到登录页面:http://localhost:9080/auth/realms/myrealm/protocol/openid-connect/auth?response_type=code&client_id=myclient&redirect_ur