2013-03-26 80 views
0

我有項目通過Spring的安全保護。我需要確定用戶,誰剛剛訪問到JSP是否已經登錄我在這裏讀了一些文章,帖子和文檔,並試圖用這個代碼來實現它。的再認識在春季3項目登錄用戶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> 

我想感謝所有幫助:)

+0

如果您檢查ROLE_USER而不是ROLE_ANONYMOUS會發生什麼? – Luciano 2013-03-26 13:04:28

+0

仍然是相同的: - /我只得到登錄消息USER:-S這是相當混亂... – Dworza 2013-03-26 13:10:19

回答

2

如果頁面入店給任何人,在你的secruity設置上下文中,您可以有條件地顯示如下內容:

<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

+0

不過這是行不通的,以及: - /我已經嘗試過這一點,所以可能有問題其他地方 – Dworza 2013-03-26 15:50:18

+0

@Dworza你會得到什麼錯誤,日誌說什麼?你需要enbale安全標籤 – NimChimpsky 2013-03-26 16:22:42

+0

我已經啓用了它們......實際上它沒有錯誤......它似乎正在正常工作。儘管如此,似乎總是用戶登錄,即使他沒有登錄 – Dworza 2013-03-26 17:55:59