
스프링 시큐리티 사용하여 인증 인가 구현하기 순서: 회원 엔티티 -> 리포지토리 -> 서비스 1. 의존성 추가 build.gradle dependencies { // 스프링 시큐리티를 사용하기 위한 스타터 추가 implementation 'org.springframework.boot:spring-boot-starter-security' // 타임리프에서 스프링 시큐리티를 사용하기 위한 의존성 추가 implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6' // 스프링 시큐리티를 테스트하기 위한 의존성 추가 testimplementation 'org.springframework.security:spring-security-test' } 2..

스프링 시큐리티 스프링 기반의 애플리케이션 보안(인증,인가,권한)을 담당하는 스프링 하위 프레임워크. 스프링 시큐리티를 이해하려면 인증과 인가에 대한 개념을 알아야 한다. 인증과 인가 인증 인증(authentication)은 사용자의 신원을 입증하는 과정. 예를 들어 사용자가 사이트에 로그인을 할 때 누구인지 확인하는 과정. 인가(authchzation)와는 다르다 인가 인가는 사이트의 특정 부분에 접근할 수 있는지에 권한을 확인하는 작업. 예를 들어 관리자는 관리자 페이지에 들어갈 수 있지만 일반 사용자는 관리자 페이지에 들어갈 수 없는 것. 이런 권한을 확인하는 과정을 인가라고 한다. 스프링 시큐리티 스프링 시큐리티는 스프링 기반 애플리케이션의 보안을 담당하는 스프링 하위 프레임워크. 보안 관련 옵션..

1. 블로그 글 목록 뷰 구현하기 1-1. 컨트롤러 메서드 1) DTO 구현 - ArticleListViewResponse @Getter public class ArticleListViewResponse { private final Long id; private final String title; private final String content; public ArticleListViewResponse(Article article) { this.id = article.getId(); this.title = article.getTitle(); this.content = article.getContent(); } } 2) BlogViewController - getArticles() 메서드 구현하기 @Re..

템플릿 엔진 : HTML 상에 데이터를넣어 보여주는 도구 다만 HTML과 함께 템플릿 엔진을 위한 문법을 살짝 섞어 사용해야 한다. 템플릿 엔진 개념 잡기 간단한 예시 h1 태그에는 ${이름} 이 text 어트리뷰트로 할당 되어있고, p태그도 비슷하다. 이러한 형태가 템플릿 문법 이렇게 해 두면 서버에서 이름, 나이라는 키로 데이터를 템플릿 엔진에 넘겨주고, 템플릿 엔진은 이를 받아 HTML에 값을 적용한다. 서버에서 보내준 데이터의 예시 - 값이 달라지면 그 때마다 화면에 반영하니 동적인 웹 페이지를 만들 수 있게 되는 것이다. 그림 타임리프 표현식 타임리프 문법 표현식 설명 예제 th:text 텍스트 표현 시 사용 th:text=${person.name} th:each 컬렉션 반복 시 사용 th:ea..

블로그 글 삭제 API 구현하기 1. 서비스 메서드 코드 BlogService @RequiredArgsConstructor // final이 붙거나 @NotNull이 붙은 필드의 생성자 추가 @Service public class BlogService { private final BlogRepository blogRepository; // 게시글을 작성한 유저인지 확인 private static void authorizeArticleAuthor(Article article) { // 현재 사용자 이름을 가져와 게시물의 작성자와 비교하고, 다른 경우에 예외 던짐 String userName = SecurityContextHolder.getContext().getAuthentication().getName(..

글 수정 API 1. 서비스 코드 구현 - update() => 특정 아이디의 글 수정 1) Articles.java - 엔티티에도 update() 메서드 구현 public void update(String title, String content) { this.title = title; this.content = content; } 2) DTO 생성 - request.UpdateArticleRequest @Getter @NoArgsConstructor @AllArgsConstructor public class UpdateArticleRequest { private String title; private String content; } 3) 서비스 코드 구현 - BlogService @RequiredArg..

블로그 글 목록 조회 API 구현하기 1. 서비스 코드 구현 BlogService - findAll() 추가 @RequiredArgsConstructor // final이 붙거나 @NotNull이 붙은 필드의 생성자 추가 @Service public class BlogService { private final BlogRepository blogRepository; // AddArticleRequest를 사용하여 새 게시물을 생성하고, // 지정된 사용자 이름으로 게시물을 저장 public Article save(AddArticleRequest request, String username) { return blogRepository.save(request.toEntity(username)); } //추가 ..

1. 프로젝트 세팅 build.gradle plugins { id 'java' id 'org.springframework.boot' version '3.0.2' id 'io.spring.dependency-management' version '1.1.3' } group = 'com.example' version = '0.0.1-SNAPSHOT' java { sourceCompatibility = '17' } configurations { compileOnly { extendsFrom annotationProcessor } } repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-s..