는 방법을 조건에 따라 표시 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>
당신이 사용할 수 있습 봄 EL 태그 <sec:authorize />
, 은 다음과 같습니다.
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<sec:authorize access="isAuthenticated()">
YES, you are logged in!
</sec:authorize>
How'bout 이? 봄 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 봄 보안 태그
request.getUserPrincipal().getName()
제휴하지 않습니다 StackOverflow