3

我正在使用构建一个 Web 应用程序Spring 3 security

登录页面myapp.com/login可以不受任何限制地访问。

当我在那里登录时,它允许我继续到另一个页面myapp.com/home

如果我再次加载myapp.com/login,它不知道用户在此会话期间已经登录,但是当我将 URL 更改为myapp.com/home它时,我可以以先前登录的用户身份访问它。

我尝试了不同的方法来确定用户是否已登录,但没有任何成功。

我使用的前端技术是JSP.

我试过这些:

    <sec:authorize ifAnyGranted="ROLE_ANONYMOUS">
        <td><a href="<c:url value="/login"/>">Login</a></td>
    </sec:authorize>
    <sec:authorize ifNotGranted="ROLE_ANONYMOUS">
        <!-- shall go to the homepage or better logout the user? -->
        <td><a href="<c:url value="/j_spring_security_logout"/>">Logout</a></td>
    </sec:authorize>

    <sec:authorize var="loggedIn" access="isAuthenticated()"/>

    <c:choose>
      <c:when test="${loggedIn}">
        <td><a href="<c:url value="/j_spring_security_logout"/>">Logout2</a></td>
      </c:when>
    </c:choose>

上面的代码似乎不起作用。为什么会这样?

applicationContext-security.xml

<beans xmlns:security="http://www.springframework.org/schema/security"
       xmlns="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">

    <security:http pattern="/resources/**" security="none"/>
    <security:http pattern="/login" security="none" auto-config="true"/>
    <security:http pattern="/denied" security="none"/>

    <security:http auto-config="true" access-denied-page="/denied" servlet-api-provision="false">
        <security:intercept-url pattern="/login**" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
        <security:intercept-url pattern="/edit/**" access="ROLE_EDIT"/>
        <security:intercept-url pattern="/admin/**" access="ROLE_ADMIN"/>
        <security:intercept-url pattern="/**" access="ROLE_USER"/>
        <security:form-login login-page="/login"  authentication-failure-url="/denied"
                             default-target-url="/"/>
        <security:logout  logout-success-url="/login" />
    </security:http>

    <security:authentication-manager>
        <security:authentication-provider>
            <security:user-service>
                <security:user name="adam" password="adampassword" authorities="ROLE_USER"/>
                <security:user name="jane" password="janepassword" authorities="ROLE_USER, ROLE_ADMIN"/>
                <security:user name="sue" password="suepassword" authorities="ROLE_USER, ROLE_EDIT"/>
            </security:user-service>
        </security:authentication-provider>
    </security:authentication-manager>

</beans>

您可以在此处检查行为:

http://147.32.204.138:5001/GENEPI/login

4

1 回答 1

0

我认为你的配置应该是这样的:

<http auto-config='true'>
    <intercept-url pattern="/home*" access="ROLE_USER,ROLE_ADMIN,ROLE_EDIT"/>
    <intercept-url pattern="/" access="ROLE_ANONYMOUS" />
    <form-login login-page='/login.jsp'/>
  </http>

如果 /home 目录发生匿名登录尝试,您将被重定向到 login.jsp。在 home/ 中,您可能有一个注销链接,其 href 是 j_spring_security_logout 此外,您可以为不同的角色添加新的拦截模式,例如

<intercept-url pattern="/edit*" access="ROLE_ADMIN,ROLE_EDIT"/>

您可以在代码的任何位置检查您的登录用户,例如:

SecurityContextHolder.getContext().getAuthentication().getPrincipal()
于 2013-05-05T03:02:41.807 回答