[Spring] 시큐리티2 (표현식)

Whatever·2022년 2월 24일
0

Spring(스프링)

목록 보기
28/29

1. 스프링 시큐리티 표현식

  • 인증 및 권한 정보에 따라 화면을 동적으로 구성
  • 로그인한 사용자 정보 보여줄 수 있음

2. 표현식

  • hasRole("ROLE_MEMBER") : 해당 롤이 있는가?
  • hasAnyrole(["ROLE_MEMBER","ROLE_AUTH"]) : 여러 롤들 중 하나라도 해당하는가?
  • principal : 인증된 사용자의 사용자 정보(UserDetails 인터페이스)
  • permitAll : 모든 사용자에게 허용
  • denyAll : 모든 사용자에게 거부
  • isAnonymous() : 로그인 하지 않아도 해당됨
  • isAuthenticated() : 로그인 해야 해당됨
  • isFullyAuthenticated() : 일반적(remember-me) 방법을 인증된 사용자일 경우 true

JSP

JSP에 다음과 같이 사용

1. taglib 추가

<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>

2. 사용할 부분에 입력

로그인 안 한 사용자(isAnonymous())

      	<sec:authorize access="isAnonymous()"> 
        <a class="nav-link dropdown-toggle text-nowrap px-3" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">
          <img class="user-avatar rounded-circle mr-2" src="/resources/shards/images/avatars/0.jpg" alt="User Avatar">
          <span class="d-none d-md-inline-block">로그인해주세요</span>
        </a>
      	</sec:authorize>

로그인 한 사용자(isAuthenticated())

      <!-- 로그인 한 사용자의 경우 
      principal.user => CusVO(로그인 시 CustomerUser에서 세팅된 정보)
      -->
      <sec:authorize access="isAuthenticated()">
      	<sec:authentication property="principal.user.cusNm"/>
      	(<sec:authentication property="principal.user.username"/>)님&nbsp;
      	<form method="post" action="/logout">
      		<button type="submit" class="mb-2 btn btn-sm btn-outline-info mr-1">로그아웃</button>
      		<sec:csrfInput/>
      	</form>
      </sec:authorize>

출력화면 :

로그인 안 한 사용자

로그인 한 사용자

0개의 댓글