2009-10-28 13 views
42

ログインしているすべてのユーザーにコンテンツを表示し、ログインしていない場合は非表示にします。Springセキュリティを使用してログインしたユーザーに条件付きでjspコンテンツを表示する方法

明らかに自家製の解決法が行われます。しかし、これを達成する最もクリーンな標準的な方法は何ですか?

春のセキュリティタグは、今後新しいロールの追加を可能にする良い方法はないようです。ここで

答えて

78

春2.5に準拠し;-):

<sec:authorize ifAnyGranted="ROLE_ANONYMOUS"> 
     <td><a href="<c:url value="/login.htm"/>">Login</a></td> 
    </sec:authorize> 
    <sec:authorize ifNotGranted="ROLE_ANONYMOUS"> 
     <td><a href="<c:url value="/j_spring_security_logout"/>">Logout</a></td> 
    </sec:authorize> 

新しい役割がここにロジックに影響を与えずに追加することができます。


isAnonymous()isAuthenticated()式は同じことを達成するために、これまでの組み合わせでうまく働いている使用して、春のセキュリティ3での日付にこの回答を表示します。ここでは例です:

<sec:authorize access="isAnonymous()"> 
    <form method="POST" action="<c:url value='j_spring_security_check'/>"> 
     Username: <input name="j_username" type="text" value="${SPRING_SECURITY_LAST_USERNAME}" /> 
     Password: <input name="j_password" type="password" /> 
     <input type="submit" value="Sign in" /> 
    </form> 
</sec:authorize> 
<sec:authorize access="isAuthenticated()"> 
    <a href="<c:url value="/j_spring_security_logout" />">Logout</a> 
</sec:authorize> 
+16

'ifAnyGranted'属性はSpringセキュリティー3.0では' access'属性のために推奨されなくなりました。たとえば ' Josh

+3

答えをありがとう、それは私を助けた。 また、タグライブラリをjspファイルに追加する必要があります。 '<%@ taglib prefix =" sec "uri =" http://www.springframework.org/security/tags "%>' maven groupId:org.springframework.security、artifactId:spring-security-taglibs –

+0

ブラウザで見苦しい '>'印字を避けるために、一重引用符を使用することを意図していました。次の依存関係をプロジェクトに追加する必要があります。一重引用符の正しい構文は次のとおりです: 'Click here to Logout'。そうでなければ、+1。 – CodeMed

1

は、私がこれをやっている方法は次のとおりです。

-aj

3

...

<%@ page import="org.springframework.security.context.SecurityContextHolder" %> 

<c:if test="<%=SecurityContextHolder.getContext().getAuthentication() != null %>"> 
    <!-- your secure content here --> 
</c:if> 

が、これはあまりにもあなたのために働くなら、私に教えてくださいどのようにについて:

<%@ taglib uri="http://acegisecurity.org/authz" prefix="authz" %> 

<c:set var="authenticated" value="${false}"/> 
<authz:authorize ifAllGranted="ROLE_USER"> 
    <c:set var="authenticated" value="${true}"/> 
</authz:authorize> 

<c:if test="${authenticated}"> 
<!-- your secure content here --> 
</c:if> 
+1

春のセキュリティ2.0:そのことによって、あなたは以下のコードすることができます別のtaglib uriを使用します。 –

7

どのようにこれを試合? - 私は次のように成功を収めてきた

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> 
<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %> 

<security:authorize ifAllGranted="ROLE_USER"> 
    Welcome <%= request.getUserPrincipal().getName() %> 
    <a href="<c:url value="/j_spring_security_logout"/>">Logout</a><br/> 
</security:authorize> 
12

あなたはこのように、タグ<sec:authorize />で春ELを使用することができます。

<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %> 

<sec:authorize access="isAuthenticated()"> 
    YES, you are logged in! 
</sec:authorize> 
27

現在のバージョン(3.1は、おそらく以前は)属性に結果を保存するためのvarパラメータをサポートしています。

<sec:authorize var="loggedIn" access="isAuthenticated()" /> 
<c:choose> 
    <c:when test="${loggedIn}"> 
     You are loged in 
    </c:when> 
    <c:otherwise> 
     You are logged out 
    </c:otherwise> 
</c:choose> 
2

私はこれを符号化するために使用される最も簡単な...

<% 
if (request.getRemoteUser()== null) {%> 
    <!-- put public-only information--> 
<%}%> 
0

あなたが使用することができ、この内部JSPスプリングセキュリティタグ

request.getUserPrincipal().getName() 
関連する問題