我正在使用构建一个 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