자바(Java)/자바+스프링 프레임워크+Mybatis
스프링 principal 사용방법 security 로그인
xemaker
2022. 3. 8. 15:35
security 태그에서도 사용하고 컨트롤러에서도 자동으로 전달해주는 Principal 객체이다.
Request에 대한 로직을 처리하는 과정에서 인증된 사용자에 대한 정보는 계속 필요하다.
로그인 정보를 쓸일이 많다. 그럴때 간편하게 사용할 수 있다.
jsp에서
<%@ taglib uri="http://www.springframework.org/security/tags" prefix="sec" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<sec:authentication property="principal" var="principal" />
html 에서 사용할때
<c:if test="${principal.USER_ID eq 'AA'}">
</c:if>
script 태그 내에서 사용할때
if("AA" == "${principal.USER_ID}"){
$$("BB").show();
}
컨트롤러에서 사용할때
LoginUser loginUser=(LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();