본문으로 바로가기
728x90
반응형
SMALL

POST 방식의 데이터 전송에 CSRF 토큰값을 같이 전송시켜 검증 받는 과정을 처리할 것이다.

get 방식에서는 사용되지 않는다

1.글 등록창

register.jsp

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

 <form role="form" action="/board/register" method="post">
        <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />

hidden 값을 이용해서 글 전송과 함께 토큰값을 함께 보내도록 한다.

 

2.글 등록 권한설정

 

현재 로그인 없이 글 등록을 할수 없게 적용을 시켜두었다.

list.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<div class="panel-heading">
                자유게시판
                <button id='regBtn' type="button" class="btn btn-xs pull-right">글 등록</button>
</div>
.
.
.
.
<script>
    $("#regBtn").on("click"function() {
                            
                            '<sec:authorize access="isAuthenticated()">'
                            self.location = "/board/register";
                            '</sec:authorize>'
                            
                            '<sec:authorize access="isAnonymous()">'
                            alert("로그인후 가능합니다.");
                            '</sec:authorize>'
                        });
</script>
 
cs

12행: 어떤권한이든 로그인이 되었다면 글등록창으로 이동하게된다.

16행: 익명의 사용자(비로그인)일때, 알람창만 띠운다.(=접근불가)

 

BoardController.java

1
2
3
4
5
@GetMapping("/register")
    @PreAuthorize("isAuthenticated()"// 어떤 사용자든 로그인이 성공한 사용자만이 해당 기능을 사용할 수 있도록 처리
    public void register() {
        
    }
cs

 

3.글 등록 컨트롤러 권한설정

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    @PostMapping("/register")
    @PreAuthorize("isAuthenticated()"// 어떤 사용자든 로그인이 성공한 사용자만이 해당 기능을 사용할 수 있도록 처리
    public String register(BoardVO board, RedirectAttributes rttr) {
 
        log.info("==========================");
 
        log.info("register: " + board);
 
        if (board.getAttachList() != null) {
 
            board.getAttachList().forEach(attach -> log.info(attach));
 
        }
 
        log.info("==========================");
 
        service.register(board);
 
        rttr.addFlashAttribute("result", board.getBno());
 
        return "redirect:/board/list";
    }
cs

2행: 글 등록의 조건은 로그인이 된 상태이여야 한다.

9행:pulpul8282.tistory.com/151?category=859948 에서 MemberVO 에서 회원의 권한을 로그로 확인한다.

17행: 글 등록

 

4.글 조회

get.jsp

1
2
3
4
5
6
7
8
9
10
11
   <sec:authentication property="principal" var="pinfo"/>
        <sec:authorize access="isAuthenticated()">
 
        <c:if test="${pinfo.username eq board.writer}">
        
        <button data-oper='modify' class="btn btn-default">수정</button>
        
        </c:if>
    </sec:authorize>
 
<button data-oper='list' class="btn btn-info">목록으로</button>
cs

조회 페이지에서 로그인 관련정보인 principal을 pinfo라는 변수로 사용하도록 설정하여

만약 조회페이지의 작성자가 들어온것이라면 수정이 가능하게 하고 아니라면 안보이게 설정을 해두었다.

 

5.글 수정

사용자가 url을 조작해서 접근할 수도 있기 때문에 이또한 POST방식으로 처리되는 부분이기에 CSRF 토큰을 적용한다.

수정과 삭제는 현재 로그인한 사용자와 게시물의 작성자가 동일한 경우에만 할 수 있게 적용시켰다.

modify.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<%@ taglib uri="http://www.springframework.org/security/tags" prefix="sec"%>   
<form role="form" action="/board/modify" method="post">
   <input type="hidden" name="${_csrf.parameterName }" value="${_csrf.token}" />
.
.
.
.
<sec:authentication property="principal" var="pinfo" />
  <sec:authorize access="isAuthenticated()">
  <c:if test="${pinfo.username eq board.writer}">    
  <button type="submit" data-oper='modify' class="btn btn-default">Modify</button>
  <button type="submit" data-oper='remove' class="btn btn-danger">Remove</button>
  </c:if>
  </sec:authorize>
  <button type="submit" data-oper='list' class="btn btn-info">List</button>
</form>
cs

boardController.java

1
2
3
4
5
6
7
8
9
10
    @PreAuthorize("principal.username == #board.writer")
    @PostMapping("/modify")
    public String modify(BoardVO board, @ModelAttribute("cri") Criteria cri, RedirectAttributes rttr) {
        log.info("modify:" + board);
 
        if (service.modify(board)) {
            rttr.addFlashAttribute("result""success");
        }
        return "redirect:/board/list" + cri.getListLink();
    }
cs

1행:수정시 로그인정보를 가져와 작성자와 로그인정보가 같은지 비교를 통해서 수정이 실행되도록 헌다,

728x90
반응형
LIST