카테고리 없음
0922. 게시글 버튼 수정(권한에 따라 숨기기)
삼월은마치
2023. 9. 23. 11:07
상세보기에서 로그인을 안 해도 수정이나 삭제 버튼이 보이는 문제를 수정해야 한다.
get.jsp
=> menu.jsp 에서 principal을 username으로 저장해 놓았기 때문에 get.jsp에서 if로 쓸 수 있는 것이다.
modify에서 수정기능도 고치기
modify.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@include file="../layouts/header.jsp"%>
<link rel="stylesheet" href="/resources/css/summernote/summernote-lite.min.css">
<script src="/resources/js/summernote/summernote-lite.min.js"></script>
<script src="/resources/js/summernote/lang/summernote-ko-KR.min.js"></script>
<script>
$(document).ready(function() {
$('#content').summernote({
height: 300, // 에디터 높이
lang: "ko-KR", // 한글 설정
});
/*
$('.get').click(function(){
document.forms.getForm.submit();
});
*/
});
</script>
<h1 class="page-header"><i class="far fa-edit"></i> 글 수정</h1>
<div class="panel panel-default">
<div class="panel-body">
<form:form modelAttribute="board" role="form">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token }" />
<form:hidden path="bno"/>
<form:hidden path="writer"/> <!-- board의 writer항목으로만 쓰고 수정 못 하게 -->
<div class="form-group">
<form:label path="title">제목</form:label>
<form:input path="title" cssClass="form-control" />
<form:errors path="title" cssClass="error"/>
</div>
<div class="form-group">
<form:label path="content">내용</form:label>
<form:textarea path="content" class="form-control"></form:textarea>
<form:errors path="content" cssClass="error"/>
</div>
<button type="submit" class="btn btn-primary">
<i class="fas fa-check"></i> 확인</button>
<button type="reset" class="btn btn-primary">
<i class="fas fa-undo"></i> 취소</button>
<a href="${cri.getLinkWithBno('get', board.bno)}" class="btn btn-primary get">
<i class="fas fa-file-alt"></i> 돌아가기</a>
</form:form>
</div>
</div>
<%@include file="../layouts/footer.jsp"%>
+) post 전송했는데 한글 안 깨지는 이유
package org.galapagos.config;
import javax.sql.DataSource;
import org.galapagos.security.CustomUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;
import org.springframework.security.web.csrf.CsrfFilter;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import lombok.extern.log4j.Log4j;
@Configuration
@EnableWebSecurity
@EnableWebMvc
@Log4j
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
protected void configure(HttpSecurity http) throws Exception {
//csrf가 동작하기 전에 필터가 동작해서 한글이 깨지지 않는다.
CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding("UTF-8");
filter.setForceEncoding(true);
http.addFilterBefore(filter, CsrfFilter.class);
travel
글 수정 삭제를 admin과 manager만 가능하게 하기
그리고 이번 경우는 권한이 없으면 아예 버튼이 안 보이게 해야 한다.
SecurityConfig
.antMatchers(
"/travel/register",
"/travel/modify",
"/travel/remove").access("hasRole('ROLE_MANAGER')");
//manager선에서 부여하면 윗선 admin도 같이 권한 생김
참고
travel의 list.jsp 에서 맨 위에 sec taglib 태그 추가하고
<%@ taglib uri="http://www.springframework.org/security/tags" prefix="sec"%>
<sec:authorize access> 코드 추가 div 태그 감싸주기
<!-- manager 이상 권한만 추가버튼 보이게 하기 -->
<sec:authorize access="hasRole('ROLE_MANAGER')">
<div class="text-right">
<a href="register" class="btn btn-primary"> <i class="far fa-edit"></i>
추가
</a>
</div>
</sec:authorize>
똑같은 작업을 상세보기에서도 해야 한다.
get.jsp
<%@ taglib uri="http://www.springframework.org/security/tags" prefix="sec"%>
<sec:authorize access="hasRole('ROLE_MANAGER')">
<a href="${cri.getLink('modify')}&no=${travel.no}" class="btn btn-primary modify">
<i class="far fa-edit"></i> 수정</a>
<a href="#" class="btn btn-danger remove">
<i class="fas fa-trash-alt"></i> 삭제</a>
</sec:authorize>
그 후 csrf 토큰을 modify, get, register jsp에 추가해줘야한다. 그래야 일반유저한테 버튼 안 보인다.
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token }" />