ログインしているすべてのユーザーにコンテンツを表示し、ログインしていない場合は非表示にしたい。jspとSpringSecurityを使用している。
明らかに、自家製のソリューションは簡単に実行できます。しかし、これを達成するための最もクリーンな標準的な方法は何ですか?
Springセキュリティタグには、将来的に新しい役割を追加できるような優れた方法がないようです。
ログインしているすべてのユーザーにコンテンツを表示し、ログインしていない場合は非表示にしたい。jspとSpringSecurityを使用している。
明らかに、自家製のソリューションは簡単に実行できます。しかし、これを達成するための最もクリーンな標準的な方法は何ですか?
Springセキュリティタグには、将来的に新しい役割を追加できるような優れた方法がないようです。
私は次のことで成功しました:
<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>
現在のバージョン(おそらくそれ以前の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>
<sec:authorize />
次のように、タグでSpringELを使用できます。
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<sec:authorize access="isAuthenticated()">
YES, you are logged in!
</sec:authorize>
どのように'これについて?-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>
どうですか:
<%@ 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>
私がこれをコーディングするのに使用した最も単純なもの...
<%
if (request.getRemoteUser()== null) {%>
<!-- put public-only information-->
<%}%>
これが私がこれをしている方法です:
<%@ page import="org.springframework.security.context.SecurityContextHolder" %>
<c:if test="<%=SecurityContextHolder.getContext().getAuthentication() != null %>">
<!-- your secure content here -->
</c:if>
これがあなたにも役立つかどうか教えてください...
-aj
これはjspスプリングセキュリティタグ内で使用できます
request.getUserPrincipal().getName()