用户登录后弹簧安全导航到其他页面

时间:2013-11-10 13:15:42

标签: java spring spring-mvc spring-security

首先,如果这是重复,请有人指出。但目前,我找不到有人问过这个问题。我有这个春天的安全配置:

<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-3.0.xsd
                    http://www.springframework.org/schema/security 
                    http://www.springframework.org/schema/security/spring-security-3.1.xsd">


    <http auto-config="true">
        <intercept-url pattern="/admin/**" access="ROLE_ADMIN" />
        <form-login login-page="/login.htm" default-target-url="/admin/adminDashboard.htm"
            authentication-failure-url="/loginfailed" />
        <logout logout-success-url="/logout.htm" />
    </http>

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

</beans:beans>

即这个控制器:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class AdministrationController {
    @RequestMapping(value = "/login.htm", method = RequestMethod.GET)
    public String login() {
        return "login";

    }

    @RequestMapping(value = "/admin/banAppealsList.htm", method = RequestMethod.GET)
    public String banAppealsList() {
        return "banAppealsList";

    }

    @RequestMapping(value = "/admin/adminDashboard.htm", method = RequestMethod.GET)
    public String adminDashboard() {
        return "adminDashboard";

    }
}

如您所见,它拦截了/admin/**网址。记录后,它会将我重定向到adminDashboard.jsp页面。但我面临的问题是:如何在不被要求提供凭据的情况下从我的adminDashboard.jsp页面重定向到其他安全页面?基本上,如果我想继续banAppealsList.htm,它会再次要求我提供凭据。

修改

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

    <!-- The definition of the Root Spring Container shared by all Servlets 
        and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
        /WEB-INF/spring/root-context.xml
        /WEB-INF/security-context.xml
        </param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <!-- Define a filter to enable Spring Security, be sure to use the suggested 
        name 'springSecurityFilterChain' -->
    <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>
    </filter-mapping>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>banAppeal.htm</welcome-file>
    </welcome-file-list>

    <session-config>
        <session-timeout>60</session-timeout>
            <!-- Obviously, problem was here -->
        <cookie-config>
            <secure>true</secure>
        </cookie-config>
    </session-config>

</web-app>

2 个答案:

答案 0 :(得分:1)

您登录的状态应存储在会话中。由于这不会发生,我怀疑你没有正确配置SpringSecurityFilterChain,因为那是将它存储在会话中的组件。

你应该在你的web.xml

中有这个
<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>
</filter-mapping>

编辑:事后看来,问题实际上并不是与Spring Security有关,而是因为您已将会话cookie设置为安全,这意味着它们无法使用非安全网址 - &gt;每次使用http网址时都会有新会话。但很高兴答案让你在从web.xml检查问题时朝着正确的方向前进。

答案 1 :(得分:0)

我认为,当你从同一个会话请求页面时,你应该使用某种会话,存储你的凭据和服务器检查它们。我还没有使用过这个框架,但我已经使用了游戏框架,因为它没有会话,但它可以处理cookie,可以用来创建这些会话。