2013-04-08 64 views
4

我是新來的春季安全,所以我試圖用Spring MVC中來處理無效的登錄,但結束了找不到網頁404春季安全無效的登錄處理

在我的安全-context.xml中,我有這AuthenticationProvider處理所有的身份驗證登錄,所以只是基本上檢查用戶的帳戶和密碼,但出於某種原因,它一直說,authentication-failure-url沒有找到404,每當有一個無效的登錄嘗試。

<security:authentication-manager alias="authenticationManager"> 
    <security:authentication-provider ref="AuthenticationProvider"/> 
    </security:authentication-manager> 

    <bean id="preAuthenticationFilter" 
     class="authentication.PreAuthenticationFilter" 
     p:authenticationManager-ref="authenticationManager" /> 

    <security:http auto-config="true"> 
    <security:intercept-url pattern="/member/**" access="MEMBER" requires-channel="https"/> 
    <security:form-login login-page="/login" 
         username-parameter="email" 
         password-parameter="password" 
         default-target-url="/member/" 
         authentication-failure-url="/loginfailed" /> 
    <security:custom-filter position="PRE_AUTH_FILTER" ref="preAuthenticationFilter" /> 
    </security:http> 

但我確實有一個相應的控制器偵聽該URL模式處理無效登錄。

@Controller 
public class LoginController 
{ 
    @RequestMapping(value = "/loginfailed", method = RequestMethod.GET) 
    public String loginError(ModelMap model) 
    {  
     model.addAttribute("error", "true"); 
     return "login"; 
    } 
} 

** * *UPDATE* ** * *

在某些點上我AuthenticationProvider驗證用戶,並拋出一個異常(我不不知道是否有問題)每當用戶有錯誤的憑證

@Component 
public class AuthenticationProvider{ 
    private User validateUser(String userName, String password) 
    { 
     try{ 
      //authenticate user's info 
      ....... 
     } 
     catch (UnauthorizedAccessException e) 
     { 
      throw new BadCredentialsException(e); 
     } 
    } 
} 
+0

不'方法= RequestMethod.GET'工作? – Xaerxess 2013-04-08 15:57:03

+0

不......我嘗試了GET和POST – peter 2013-04-08 15:58:03

+4

在我看來,這不是Spring安全問題,而是MVC配置 - 對'/ loginfailed'返回200狀態執行簡單的POST/GET請求嗎? – Xaerxess 2013-04-08 16:09:48

回答

4

(作爲後續評論。)從j_spring_security_check 302 statu和BadCredentialsException都是正確的。它看起來像你的控制器根本沒有註冊。你有沒有想到使用@Controller註釋bean是不足以使其工作的? Quoting documentation

您可以明確定義註釋控制器豆,使用調度的情況下標準 的Spring bean定義。然而, @Controller原型還允許自動檢測,並與 彈簧一般支持檢測 類路徑中的組件類並自動註冊它們的bean定義。

要啓用此類註釋控制器的自動檢測,請將​​ 組件掃描添加到您的配置中。如示於以下XML片段使用彈簧上下文 架構:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context.xsd"> 

    <context:component-scan base-package="com.example"/> 

    <!-- ... --> 

</beans> 

(假設LoginController是com。示例包)。

簡單的bean聲明可以使用的,而不是組件掃描:

<bean class="com.example.LoginController" />