45

ログインしているすべてのユーザーにコンテンツを表示し、ログインしていない場合は非表示にしたい。jspとSpringSecurityを使用している。

明らかに、自家製のソリューションは簡単に実行できます。しかし、これを達成するための最もクリーンな標準的な方法は何ですか?

Springセキュリティタグには、将来的に新しい役割を追加できるような優れた方法がないようです。

4

8 に答える 8

84

私は次のことで成功しました:

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

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


この答えをSpringSecurity3で最新のものにするために、isAnonymous()andisAuthenticated()式を組み合わせて使用​​することは、これまでのところ同じことを達成するためにうまく機能しています。次に例を示します。

<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>
于 2009-11-30T06:19:17.960 に答える
29

現在のバージョン(おそらくそれ以前の3.1)は、結果を属性に保存するためのvarパラメーターをサポートしています。これにより、次のようにコーディングできます。

<sec:authorize var="loggedIn" access="isAuthenticated()" />
<c:choose>
    <c:when test="${loggedIn}">
        You are logged in
    </c:when>
    <c:otherwise>
        You are logged out
    </c:otherwise>
</c:choose>
于 2012-08-16T12:39:38.937 に答える
12

<sec:authorize />次のように、タグでSpringELを使用できます。

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

<sec:authorize access="isAuthenticated()">
   YES, you are logged in!
</sec:authorize>
于 2010-11-11T10:11:23.530 に答える
7

どのように'これについて?-Spring2.5準拠;-)

<%@ 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>
于 2009-11-20T01:04:45.350 に答える
3

どうですか:

<%@ 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>
于 2009-11-09T22:44:34.050 に答える
2

私がこれをコーディングするのに使用した最も単純なもの...

<%
if (request.getRemoteUser()== null) {%>  
    <!-- put public-only information-->
<%}%>
于 2012-12-04T10:30:23.893 に答える
1

これが私がこれをしている方法です:

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

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

これがあなたにも役立つかどうか教えてください...

-aj

于 2009-10-30T03:35:50.310 に答える
0

これはjspスプリングセキュリティタグ内で使用できます

request.getUserPrincipal().getName()
于 2017-12-11T18:11:45.893 に答える