스프링 시큐리티의 표현식

✅공동 표현식

✅ 표현식 사용

→ home.jsp를 수정한다.

→ 표현식을 이용한 내용을 추가합니다.

로그인 하지 않은 경우와 한 경우 실습(notice, board)

board/register.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="<http://www.springframework.org/security/tags>" prefix="sec" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Board Register</title>
</head>
<body>
	<h2>BOARD REGISTER : access to member</h2>
	
	<p>principal : <sec:authentication property="principal"/></p>
	<p>member : <sec:authentication property="principal.member"/></p>
	<p>사용자 이름 : <sec:authentication property="principal.member.userName"/></p>
	<p>사용자 아이디 : <sec:authentication property="principal.member.userId"/></p>
	<p>사용자 비밀번호 : <sec:authentication property="principal.member.userPw"/></p>
	<p>사용자 권한 리스트 : <sec:authentication property="principal.member.authList"/></p>
	<hr>
	<sec:authorize access="hasRole('ROLE_MEMBER')">
		역할명 : 일반회원 권한입니다.
	</sec:authorize>
	<sec:authorize access="hasRole('ROLE_ADMIN')">
		역할명 : 관리자 권한입니다.
	</sec:authorize>
</body>
</html>

notice/list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Notice List</title>
</head>
<body>
	<h2>NOTICE LIST : access to all</h2>
	<a href="/notice/register">등록</a>
</body>
</html>

notice/register.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	    pageEncoding="UTF-8"%>
	<%@ taglib uri="<http://www.springframework.org/security/tags>" prefix="sec" %>
	<!DOCTYPE html>
	<html>
	<head>
	<meta charset="UTF-8">
	<title>Notice Register</title>
	</head>
	<body>
		<h2>NOTICE REGISTER : access to admin</h2>
		
		<p>principal : <sec:authentication property="principal"/></p>
		<p>member : <sec:authentication property="principal.member"/></p>
		<p>사용자 이름 : <sec:authentication property="principal.member.userName"/></p>
		<p>사용자 아이디 : <sec:authentication property="principal.member.userId"/></p>
		<p>사용자 비밀번호 : <sec:authentication property="principal.member.userPw"/></p>
		<p>사용자 권한 리스트 : <sec:authentication property="principal.member.authList"/></p>
		<hr>
		<sec:authorize access="hasRole('ROLE_MEMBER')">
			역할명 : 일반회원 권한입니다.
		</sec:authorize>
		<sec:authorize access="hasRole('ROLE_ADMIN')">
			역할명 : 관리자 권한입니다.
		</sec:authorize>
	</body>
	</html>

아직 로그인을 하지 않은 home 화면이다. (board클릭)

Untitled

등록 하러 가기 누르고

Untitled

MEMBER 권한을 갖고 있는 아이디로 로그인하면

Untitled

로그인이 잘 된것을 확인할 수 있다.

Untitled

다시 home화면을 가면 이미 로그인(권한등록)을 하였기 때문에 로그아웃버튼으로 바뀌어 있고, notice를 다시 들어가자

Untitled

등록 버튼을 눌러보면 이전에 MEMBER권한으로 로그인을 하여서 해당 ADMIN관한을 가져야만 들어갈 수 있는 notice/register.jsp 페이지로 가는걸 거부당한 것을 확인할 수 있다.

Untitled

Untitled