2012-01-13 204 views
1

我正在嘗試實現自定義登錄頁面以在我的JSF 2.0應用程序中使用。我正在使用spring security 3.0.5,並且在用戶登錄後,頁面沒有正確重定向。而不是去請求的頁面(本地主機:8080/ERP的Web),它是將這樣的:無法從登錄頁面重定向

http://localhost:8080/erp-web/javax.faces.resource/forms/forms.js.xhtml?ln=primefaces&v=2.2.1

這是我的JSF頁面:

<h:form prependId="false"> 
    <h:panelGroup layout="block" class="hrgi-dialog-content hrgi-div-form clearfix"> 
     <p:focus/>    
     <h:outputLabel for="j_username"/> 
     <p:inputText id="j_username" value="#{loginBean.usuario}" required="true"/> 
     <h:outputLabel for="j_password"/> 
     <h:inputSecret id="j_password" value="#{loginBean.senha}" required="true"/> 
     <h:commandButton id="submit" type="submit" value="OK" action="#{loginBean.submit}"/> 
    </h:panelGroup> 
</h:form> 

這是用綠豆:

public class LoginBean { 

    private String usuario; 
    private String senha; 

    public String submit() throws IOException, ServletException { 
     ExternalContext context = FacesContext.getCurrentInstance().getExternalContext(); 
     RequestDispatcher dispatcher = ((ServletRequest) context.getRequest()).getRequestDispatcher("/j_spring_security_check"); 
     dispatcher.forward((ServletRequest) context.getRequest(), (ServletResponse) context.getResponse()); 
     FacesContext.getCurrentInstance().responseComplete(); 
     return null; 
    } 

/*getters and setters here*/ 

} 

最後,這是我的春天安全配置文件:

<http auto-config="true"> 
    <intercept-url pattern="/login.xhtml*" access="IS_AUTHENTICATED_ANONYMOUSLY"/> 
    <intercept-url pattern="/**" access="ROLE_CADASTRADOR,ROLE_ADMINISTRADOR,ROLE_VENDEDOR,ROLE_BANCO"/> 
    <form-login login-page="/login.xhtml"/> 
    <session-management> 
     <concurrency-control max-sessions="1" error-if-maximum-exceeded="true"/> 
    </session-management> 
</http> 

<authentication-manager alias="authenticationManager"> 
    <authentication-provider ref="daoAuthenticationProvider"/> 
</authentication-manager> 

<bean:bean id="daoAuthenticationProvider" 
      class="org.springframework.security.authentication.dao.DaoAuthenticationProvider" 
      scope="singleton"> 
    <bean:property name="userDetailsService" ref="detalhadorDeUsuarios"/> 
    <bean:property name="passwordEncoder" ref="passwordEncoder"/> 
</bean:bean> 

<bean:bean id="detalhadorDeUsuarios" class="com.hrgi.web.seguranca.DetalhadorDeUsuarios" 
      scope="singleton"> 
    <bean:property name="recuperador" ref="funcionarioDao"/> 
</bean:bean> 

<bean:bean id="passwordEncoder" class="org.springframework.security.authentication.encoding.ShaPasswordEncoder" 
      scope="singleton"> 
    <bean:constructor-arg name="strength" value="256"/> 
</bean:bean> 

<bean:bean id="loginBean" class="com.hrgi.web.seguranca.LoginBean" scope="request"/> 


<bean:bean id="loggerListener" 
     class="org.springframework.security.authentication.event.LoggerListener" /> 

這是我收到的迴應: after login app is redirecting to wrong place

+1

我不會做Spring,但我敢打賭它是Spring方面的一個bug。看起來好像沒有考慮到'/ javax.faces.resource'請求,同時檢查最後一個請求的URL,它與'FacesServlet'相匹配。 – BalusC 2012-01-13 04:50:54

回答

2

您應該添加JS/CSS資源不受限制,像

<intercept-url pattern="/**/*.css*" access="IS_AUTHENTICATED_ANONYMOUSLY"/> 
<intercept-url pattern="/**/*.js*" access="IS_AUTHENTICATED_ANONYMOUSLY"/> 

問題是春季安全截獲您的JS要求登錄頁面所需的文件並強制進行身份驗證。完成後,它會重定向到最近的受限制網址,JavaScript就是您的情況。