1

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

<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/**URL。登录后,它会将我重定向到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>
4

2 回答 2

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 设置为安全的事实有关,这意味着它们不适用于非安全 url-> 每次 http url 时的新会话是用过的。但很高兴答案让您在从 web.xml 检查问题时朝着正确的方向前进。

于 2013-11-10T15:28:45.607 回答
0

我认为,当从同一会话请求页面时,您应该使用某种会话,女巫存储您的凭据并服务器检查它们。我没用过这个框架但是我用过play框架,witch没有现成的session,但是它可以处理cookies,witch可以用来创建这些session。

于 2013-11-10T13:45:10.753 回答