【问题标题】:Spring migration issue to version 4.x春季迁移问题到版本 4.x
【发布时间】:2015-07-20 00:52:36
【问题描述】:

我有这个调度器 servlet.xml

<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"/>

安全性.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 它显示spring默认登录表单而不是我自己的自定义登录表单

【问题讨论】:

标签: spring spring-mvc spring-security


【解决方案1】:

Spring Security 并不像您想象的那样工作!该框架基本上提供了一个称为 DelegatingFilterProxy 的过滤器链,并根据配置的(默认 100)过滤器顺序将其嵌入到 ApplicationFilterChain 中(一个非常基本的用例,否则它很大:))。

但它是非常可配置的! (感谢开发团队)

您的控制器方法没有被执行,因为过滤器链使用默认登录 URL 作为版本 4+ 中的 /login(在 3.x 中它曾经是别的东西,不再记得它了;))。每当您尝试点击 /login URL 时,它都会被过滤器链拦截,并且当您要求默认登录页面时,它会生成并提供给您(这是预期的行为)。由于这个原因,您的配置曾经与 3.x 一起使用(您试图访问一个只允许匿名访问的 URL)。换句话说,您曾经做过一些 Spring security 可以开箱即用的事情(在 3.x 中);自己。

如果您希望使用与默认 URL 不同的 URL,则需要在元素中显式设置它。例如,

   <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"/>

通过这种配置,您可以强制 Spring 安全性在调度程序上下文中查找登录 URL。您的配置 URL /login 可能仍然无法触发您的自定义登录操作。但是您可以使用 login.jsp(基于您的视图解析器和控制器模型视图,您的登录页面是 login.jsp)作为您的登录页面,如示例所示,其余部分由 Spring 安全性处理。

您可能需要参考此 migration doc from 3.x to 4.x (NS config) 以获得更多见解和帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-03
    • 2017-12-08
    • 2021-04-01
    相关资源
    最近更新 更多