2017-07-14 51 views
-1

我正嘗試一個JSF Web應用程序與春 安全整合。集成一個JSF的登錄頁面使用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"); 
} 
+1

請不要把你所有的代碼放在這裏。只需將導致出現問題的代碼或您不明白的部分代碼放入即可。否則,它太無聊了。 –

+0

Spring安全性可通過URL進行配置。除此之外,它還提供了一個身份驗證端點,因此您可以對該身份驗證端點執行POST(使用純文本格式而不是JSF格式),一旦登錄,導航至您的應用程序url。 –

回答

1

我寧可不使用JSF進行身份驗證和使用普通的HTML表單代替(前having configured身份驗證入口點):

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

這將執行POST到您的Spring Security身份驗證入口點。然後,一旦用戶登錄,您就可以使用AuthenticationSuccessHandler或默認目標URL重定向到您的JSF應用程序。

0

登錄按鈕操作應首先像這樣執行重定向並將流傳遞給spring安全性。

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