Study/Spring
스프링 시큐리티(spring security) JSP 사용(1) 표현식
오늘만 사는 여자
2022. 7. 4. 11:12
728x90
반응형
앞선 시큐리티 설정 때 추가해놓은 security-tablib 라이브러리를 활용해 봅시다.
JSP상단에 <sec:authentication> 태그와 principal이라는 이름의 속성을 사용합니다.
admin.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/security/tags" prefix="sec" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http//
www.w3.org/TR/html4/loose.dtd>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>/sample/admin page</h1>
<p>principal : <sec:authentication property="principal" /></p>
<p>MemberVO : <sec:authentication property="principal.member"/></p>
<p>사용자 이름 : <sec:authentication property="principal.member.userName"/></p>
<p>사용자 아이디 : <sec:authentication property="principal.username"/></p>
<p>사용자 권한 리스트 : <sec:authentication property="principal.member.authList"/></p>
<form action="/customLogout" method="post">
<input type="submit" value="Logout">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
</form>
</body>
</html>
위 와 같이 한후 로그인을 해보면 다음과 같은 정보들이 출력됩니다.
![](https://blog.kakaocdn.net/dn/bb8kst/btqwl6h9a3P/MIZ3xmv2OtvqMUqPwiKfSk/img.png)
표현식을 이용하는 동적 화면 구성
all.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/security/tags" prefix="sec" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http//
www.w3.org/TR/html4/loose.dtd>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>/sample/all page</h1>
<sec:authorize access="isAnonymous()">
<a href="/customLogin">로그인</a>
</sec:authorize>
<sec:authorize access="isAuthenticated()">
<a href="/customLogout">로그아웃</a>
</sec:authorize>
</body>
</html>
![](https://blog.kakaocdn.net/dn/wzqag/btqwnQZJnru/U711yiCm3KEDTlnId8JfN1/img.png)
![](https://blog.kakaocdn.net/dn/bMJ9EB/btqwkVOYbSz/onP2uQUayQqn8qsBGQeQL1/img.png)
이처럼 표현식에 따라 로그인한 경우와 그렇지 않은 경우에 따라서 다른 결과를 나타낸다.
출처: https://taetae0079.tistory.com/6 [TH블로그:티스토리]
728x90
반응형