どのように条件付きで示jspのコンテンツへのログインユーザー春の安全保障
-
08-07-2019 - |
質問
にしていきたいコンテンツをユーザーがログインし、隠れば、ログインしていない。私が使っているjspの温泉です。
明らかに家栽培の解決は簡単に行われます。しかし、最も標準的な方法の実現。
春ュタグをもっと見る素敵なる仕上がりのための新しい役割です。
解決
また、以下の:
<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>
新しい役割を追加できな影響を及ぼすロジックです。
この回答の最新春キ3を使用し、 isAnonymous()
や isAuthenticated()
表現においてどのように組み合わせこれまでの達成のためにおいしかったです。次に例を示します。
<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 loged in
</c:when>
<c:otherwise>
You are logged out
</c:otherwise>
</c:choose>
次のように、タグ<sec:authorize />
でSpring ELを使用できます。
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<sec:authorize access="isAuthenticated()">
YES, you are logged in!
</sec:authorize>
これはどう? -Spring 2.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 springセキュリティタグ内でこれを使用できます
request.getUserPrincipal().getName()
所属していません StackOverflow