2015-07-19 92 views
-1

我有這個調度servlet.xml中春季遷徙問題4.x版本

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    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"> 

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

</beans> 

的applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task" 
    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 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc.xsd 
     http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd   
     http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> 

    <context:component-scan base-package="com.mkyong.*,com.mobapp.security.Login.handlers" /> 
    <mvc:annotation-driven /> 
    <mvc:resources mapping="/resources/**" location="/resources/" cache-period="31556926"/>  
    <import resource="ConfigFiles/Security.xml"/> 

的security.xml

<beans:beans xmlns="http://www.springframework.org/schema/security" 
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/security 
    http://www.springframework.org/schema/security/spring-security.xsd"> 

    <http> 
     <form-login 
      username-parameter="username" 
      password-parameter="password" />   
    </http> 

    <beans:bean id="pwdEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"> 
     <beans:constructor-arg name="strength" value="11" /> 
    </beans:bean> 
    <beans:bean id="appUserDetailService" class="com.mobapp.security.AppUserDetailService"></beans:bean> 

    <authentication-manager> 
     <authentication-provider user-service-ref="appUserDetailService"> 
      <password-encoder ref="pwdEncoder"/> 
     </authentication-provider> 
    </authentication-manager>   

</beans:beans> 

這裏是/login

@RequestMapping(value = "/login", method = RequestMethod.GET) 
public ModelAndView login(@RequestParam(value = "error", required = false) String error, 
     @RequestParam(value = "logout", required = false) String logout) { 

    ModelAndView model = new ModelAndView(); 
    if (error != null) { 
     model.addObject("error", "Invalid username and password!"); 
    } 

    if (logout != null) { 
     model.addObject("msg", "You've been logged out successfully."); 
    } 
    model.setViewName("login"); 

    return model; 

} 

如果我使用

<spring.version>3.2.8.RELEASE</spring.version> 
<spring.security.version>3.2.3.RELEASE</spring.security.version> 

一切正常

但我用

<spring.version>4.1.7.RELEASE</spring.version> 
<spring.security.version>4.0.1.RELEASE</spring.security.version> 

,並嘗試訪問/login它顯示了春天默認的登錄表單,而不是我自己的custome loginf形式

+0

[爲什麼我的自定義登錄頁面不顯示在Spring Security 4中?](http://stackoverflow.com/questions/29595098/why-doesnt-my-custom-login-page-show-with -spring安全-4) – manish

回答

2

Spring Security不能像你想象的那樣工作!該框架基本上提供了一個稱爲DelegatingFilterProxy的過濾器鏈,並根據配置的(默認爲100)過濾器順序將其嵌入ApplicationFilterChain(一個非常基本的用例,否則它是巨大的:))。

但它是非常多的可配置! (感謝開發團隊)

您的控制器方法沒有得到執行,因爲過濾器鏈在版本4+中使用默認登錄URL作爲/ login(在3.x中它曾經是別的東西,不記得它已經;))。每當您嘗試點擊/ login URL時,它都會被過濾器鏈截獲,並且在您要求使用默認登錄頁面時,它會生成並提供給您(這是預期的行爲)。因爲這個原因,你的配置曾經與3.x一起工作(你試圖訪問一個只允許匿名訪問的URL)。換句話說,你曾經做過Spring安全性可以做的一些事情(在3.x中);你自己。

如果您希望使用不同於默認網址的網址,那麼您需要在元素中明確設置它。例如,

<form-login login-page="/custom_login_url" 
     default-target-url="/" 
     login-processing-url="/auth" 
     authentication-failure-url="/custom_login_url?login=failed" 
     username-parameter="username_param" 
     password-parameter="password_param" 
     always-use-default-target="false"/> 

有了這樣的配置,你可以強制春季安全查找在調度上下文中的登錄URL。您的配置URL /登錄可能仍然不會打你自定義登錄操作。但是,您可以使用login.jsp(基於您的視圖解析器和控制器模型視圖,您的登錄頁面爲login.jsp)作爲您的登錄頁面,如示例中所示,Spring安全性將負責其餘部分。

您可能想要參考此 migration doc from 3.x to 4.x (NS config)以獲得更多見解和幫助。