2014-10-28 32 views
0

我在兩頁基本網站上有以下安全配置。Spring Security過濾掉每一個案例

<http use-expressions="true"> 
    <intercept-url pattern="/login*" access="permitAll" /> 
    <intercept-url pattern="/resources/**" access="permitAll" /> 
    <intercept-url pattern="/**" access="hasRole('ROLE_ADMIN')" /> 
    <form-login login-page='/login.jsp' 
     default-target-url="/index.jsp"   
     username-parameter="name" 
     password-parameter="password"/> 
</http> 

<authentication-manager> 
    <authentication-provider> 
     <user-service> 
      <user name="admin" password="admin" authorities="ROLE_USER, ROLE_ADMIN" /> 
      <user name="user" password="user" authorities="ROLE_USER" /> 
     </user-service> 
    </authentication-provider> 
</authentication-manager> 

我沒有使用自定義控制器,只使用默認的彈簧視圖分辨率。

<mvc:annotation-driven /> 
<mvc:resources location="/resources/" mapping="/resources/**"/> 

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/view/"/> 
     <property name="suffix" value=".jsp"/> 
</bean> 

當我嘗試訪問默認頁面index.jsp它重定向我到login.jsp頁如我所料,但是當我把表單的數據不會送我去index.jsp頁面重定向再次我到login.jsp。表單字段用name定義,如安全配置中的showin,表格actionindex.jsp

login.jsp形式:

<form method="post" action="<c:url value = '/index.jsp' />"> 
    <label for="inputEmail3">User</label> 
    <input name="name" type="text" id="inputEmail3" placeholder="User name" required> 

    <label for="inputPassword3">Password</label> 
    <input name="password" type="password" id="inputPassword3" placeholder="Password" required> 
    <button type="submit" class="btn btn-success btn-sm">Sign in</button> 
    <button type="reset" class="btn btn-default btn-sm">Reset</button> 
</form> 

我缺少什麼?

+0

你能顯示'login.jsp'嗎? (我假設有一個,因爲你使用自定義參數名稱) – 2014-10-28 14:10:48

+0

我現在在login.jsp中顯示錶單。 – 2014-10-28 14:30:56

+0

它爲什麼要做任何事情。爲什麼表單提交給'index.jsp'?它應該是'j_spring_security_check'。 – 2014-10-28 14:33:59

回答

1

Spring安全使用包括表單登錄的過濾器。默認情況下,當需要對用戶進行身份驗證時,DefaultLoginPageGeneratingFilter會生成包含HTML表單的頁面,但您可以爲其提供。

然後UsernamePasswordAuthenticationFilter截取表單的提交,默認情況下URL爲j_spring_security_check,並且表單應該用POST提交或者spring安全會(默認情況下)拋出一個異常。

您應該爲登錄表單提交使用專門的URL。如果您不喜歡j_spring_security_check,您可以使用login-processing-url表單登錄屬性更改它,但它應該而不是與成功URL相同。