gpt4 book ai didi

java - Spring 3项目JSP中登录用户的识别

转载 作者:行者123 更新时间:2023-12-02 07:10:02 24 4
gpt4 key购买 nike

我有一个通过 Spring security 保护的项目。我需要确定刚刚访问 JSP 的用户是否已经登录。我已经阅读了一些文章、帖子和文档,并尝试使用以下代码实现它:

<sec:authorize ifAnyGranted="ROLE_ANONYMOUS">
USER NOT LOGGED IN
<td><a href="<c:url value="/login"/>">Login</a></td>
</sec:authorize>
<sec:authorize ifNotGranted="ROLE_ANONYMOUS">
USER LOGGED IN
<td><a href="<c:url value="/j_spring_security_logout"/>">Logout</a></td>
</sec:authorize>

尽管如此,我总是收到“用户已登录”,但我不明白为什么......这是我的安全上下文

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

如果有任何帮助,我将不胜感激:)

最佳答案

如果某个页面可供任何人访问(如您的安全上下文中所设置),您可以有条件地显示内容,如下所示:

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

然后使用相当标准的 jSTL 你就可以:

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

一点谷歌搜索告诉我 ifnotgranted is deprecated

关于java - Spring 3项目JSP中登录用户的识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15637729/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com