SpringBoot

[springBoot] Security Tag library

유쾌한고등어 2023. 1. 5. 20:44

Security Tag library

 

 

1. UserController에서 model로 데이터 받아 사용

// userController.java
@GetMapping("/user/{id}/update")
    public String update(@PathVariable int id, 
    @AuthenticationPrincipal PrincipalDetails principalDetails,
    Model model){

        model.addAttribute("principal",principalDetails.getUser());

        return "user/update";
    }
<!--// user/update.jsp-->
<h2>${principal.username}</h2>

2. Security Tag library

 

- Tag Libray

<!-- 시큐리티 태그 라이브러리 -->
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-taglibs</artifactId>
</dependency>

 

 

- header.jsp 에 authentication <sec:authorize> spring security tag 넣기

<!-- header.jsp -->

<sec:authorize access="isAuthenticated()">
	<sec:authentication property="principal" var="principal"/>
</sec:authorize>

 

- principaldetails 세션정보접근

<!--// user/update.jsp-->
<h2>${principal.user.username}</h2>

 

- principaldetails 세션정보접급할때 Security tag lib을 사용함으로써, model을 사용하지않아도 된다.

// userController.java
@GetMapping("/user/{id}/update")
    public String update(@PathVariable int id, 
    @AuthenticationPrincipal PrincipalDetails principalDetails)
    {
        return "user/update";
    }