2014-04-14 40 views
1

我試圖在認證形式JSF Spring Security的整合,民營頁面現在需要一個認證,但是當我輸入用戶名和密碼,這個錯誤是diplayed錯誤404:/ j_spring_security_check不可

Error 404 : Project/j_spring_security_check is not available 

因此,我創建一個JSF形式:

<h:form id="loginForm" prependId="false" > 
    <label for="exampleInputEmail1">Email address</label> 
<h:inputText class="form-control" id="j_username" required="true"/> 
<label for="exampleInputPassword1">Password</label> 
<h:inputSecret class="form-control" id="j_password" required="true"/> 
    <h:commandButton type="submit" class="btn btn-primary btn-block" action="#{authentication.doLogin()}" value="Login" /> 
</h:form> 

函數doLogin()在認證豆被定義這樣的:

public String doLogin() throws ServletException, IOException 
    { 
     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; 
    } 

的彈簧security.xml文件:

<http auto-config="true"> 
<intercept-url pattern="/j_spring_security_check" access="IS_AUTHENTICATED_ANONYMOUSLY"/> 
<intercept-url pattern="/login.xhtml*" access="IS_AUTHENTICATED_ANONYMOUSLY" /> 
<intercept-url pattern="/Backoff/*" access="ROLE_USER"/> 
<form-login login-page="/login.xhtml" default-target-url="/Backoff/activities.xhtml" 
always-use-default-target="true" 
authentication-failure-url="/Front/error.xhtml" /> 
</http> 

我應該添加,使其作品?

回答

1

爲了解決這個問題,你可以按照這個下面的步驟:

1 - 更改你的Spring-security.xml文件是這樣的:

<http use-expressions="true"> 

<intercept-url pattern="/login.xhtml*" access="IS_AUTHENTICATED_ANONYMOUSLY" /> 
<intercept-url pattern="/Backoff/*" access="ROLE_USER"/> 
<form-login 
     login-page="/login.xhtml" 
     default-target-url="/Backoff/activities.xhtml" 
     always-use-default-target="true" 
     authentication-failure-url="/Front/error.xhtml" 
     <!-- 
     if you want to use primefaces components you can use this: 
     password-parameter="j_idt10" : Name of password input 
     username-parameter="j_idt8" : Name of username input 
     ==>To get the name of your inputs you can use "element inspection" offred by you browser (chrome,firefox) 
      --> 

    /> 
</http> 

2 - 更改您的JSF表單如

<h:form id="loginForm" prependId="false" onsubmit="document.getElementById('loginForm').action='j_spring_security_check';" > 
    <label for="exampleInputEmail1">Email address</label> 
<h:inputText id="exampleInputEmail1" class="form-control" name="j_username" required="true"/> 
<label for="exampleInputPassword1">Password</label> 
<h:inputSecret id="exampleInputPassword1" class="form-control" name="j_password" required="true"/> 
    <h:commandButton type="submit" class="btn btn-primary btn-block" value="Login" /> 
</h:form> 

您可以注意到我刪除了您的commandButton的動作,並且將您的輸入的ID更改爲名稱,我還在

希望這有助於你出去

0

嘗試添加<dispatcher>FORWARD</dispatcher>

<filter> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
</filter> 

<filter-mapping> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <url-pattern>/*</url-pattern> 
    <dispatcher>FORWARD</dispatcher> 
    <dispatcher>REQUEST</dispatcher> 
</filter-mapping> 

Custom login page with JSF and Spring-Security 3