Java 为什么我的Spring应用程序可以';我不知道,有人已经准备好登录了?

Java 为什么我的Spring应用程序可以';我不知道,有人已经准备好登录了?,java,spring,spring-security,Java,Spring,Spring Security,我正在使用spring3security 登录页面myapp.com/login无任何限制 当我登录到那里时,它允许我继续进入另一个页面myapp.com/home 如果我再次加载myapp.com/login,它不知道该用户在此会话期间已登录,但当我将URL更改为myapp.com/home时,它允许我作为以前登录的用户访问它 我尝试了不同的方法来确定用户是否登录,但没有成功 我使用的前端技术是JSP 我试过这些: <sec:authorize ifAnyGranted="ROL

我正在使用
spring3security

登录页面
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

我认为您的配置应该是:

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

Thx的答案,但它不能解决我的问题。。。我尝试了
SecurityContextHolder.getContext().getAuthentication().getPrincipal()
,但随后得到了“异常”[javax.servlet.ServletException:javax.servlet.jsp.jspeException:java.io.IOException:在应用程序上下文中找不到可见的WebSecurityExpressionHandler实例。必须至少有一个实例才能支持jsp“authorize”标记中的表达式。]with root cause java.io.IOException:在应用程序上下文中找不到可见的WebSecurityExpressionHandler实例。必须至少有一个实例才能支持JSP“authorize”标记中的表达式。`我已更新了我的post和安全cfg文件。'您可能会看到,是什么导致它……检查有关如何设置WebSecurityExpressionH的文档andler,在您的XML配置中将“使用表达式”属性设置为“true”。请参阅此处:。接受的答案和其他答案都可以解决您的问题。
<intercept-url pattern="/edit*" access="ROLE_ADMIN,ROLE_EDIT"/>
SecurityContextHolder.getContext().getAuthentication().getPrincipal()