스프링 기초_Security_7

bitna's study note·2022년 7월 25일

스프링

목록 보기
31/54

7월 25일

1.스프링 시큐리티를 jsp에서 사용하기
번거롭게 CustomUserDetailsService를 쓰는 가장 큰 이유는 jsp 등에서 단순히 사용자의 아이디(스프링 시큐리티에서의 username)정도가 아닌 사용자의 이름이나 이메일과 같은 추가적인 정보를 이용하기 위함임.

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://www.springframework.org/security/tags" prefix="sec" %>    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>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>

<a href="/customLogout">로그아웃</a>

</body>
</html>

<sec:authentication property="principal"/> 의 의미는 UserDetailsService에서 반환된 객체임. 즉 CustomUserDetailsService를 이용 했다면 그안에 있는 유일한 메서드인 loadUserByUsername()에서 반환된 CustomUser객체가됨.
즉, principal 이 CustomUser를 의미함.
principal.member는 CustomUser 객체의 getMember()를 호출함.

2.표현식을 이용하는 동적 화면 구성
로그인한 사용자에게 특정한 내용을 보여줄때, 스프링 시큐리티의 표현식을 사용한다.

hasRole([role])인증할때 true
hasAuthority([authority])권한확인할때 true
hasAnyRole([role,role2])인증할때 여러개중 하나가 해당되면 true
hasAnyAuthority([authority])권한확인할때 여러개중 하나가 해당되면 true
principal 현재사용자 정보를 의미
permiAll 모든사용자에게 허용
denyAll 모든사용자에게 거부
isAnonymous()익명의 사용자의 경우(로그인을 하지 않은 경우도 해당)
isAuthenticated() 인증된 사용자면 true
isFullyAuthenticated() Remember-me로 인증된 것이 아닌 인증된 사용자의 경우true

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://www.springframework.org/security/tags" prefix="sec" %>  
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<h1>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>
profile
좋은개발자가 되기위한 삽질기록 노트

0개의 댓글