-
Notifications
You must be signed in to change notification settings - Fork 16
[이지현] Sprint9 #125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
devlee1011
wants to merge
50
commits into
codeit-bootcamp-spring:이지현
Choose a base branch
from
devlee1011:sprint9
base: 이지현
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[이지현] Sprint9 #125
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
prod: validate, dev: update
하드 코딩된 dev 프로필의 데이터소스 정보 수정 gitignore에 .env 추가
설정 프로필 prod로 변경 compose 설정 파일에 volume 추가 schema 파일 위치 루트 폴더로 변경
JpaClockConfig 클래스 추가 각 Instant 타임 정보를 사용하는 Request 클래스의 JsonFormat의 timezone을 Asia/Seoul로 변경 Dockerfile, docker-compose에 타임존 설정 추가 application 디폴트 설정 파일에 타임존 설정 추가 AWS S3 관련 의존성 추가
AWSS3Test 서비스 클래스 생성 (S3 테스트용) - 업로드, 다운로드, 프리사인드 URL 생성 기능 설정 파일(application.yaml)에 AWS S3 관련 환경변수 설정 값 추가 docker-compose 설정 파일에 AWS S3 환경 변수 추가 BasicBinaryContentService에 S3를 사용해 파일을 생성,저장하는 로직 추가 BinaryContentController에 AWS S3용 create, donwolad API 메서드 추가 AWS S3 의존성 추가, Spring Boot 버전 변경(3.4.0 -> 3.5.5)
S3BinaryContentStorage 구현, S3Config 로직 내장(S3Client, S3Presigner 생성 로직) S3Config, AWSS3Test 제거
AWSS3Test 클래스 재생성, 외부 연동 테스트 코드 추가
application-prod 설정 파일의 ddl-auto를 validate로 변경 shcema.sql 오타 수정
.env 환경 변수 파일과 값이 겹치는 것에 의한 배포 오류 해결을 위함
8081:8081 -> 80:8081로 변경
Windows 환경에서 CRLF 개행으로 인한 docker buildx 실패 오류 해결
.gitattributes로 CRLF를 기본 LF로 변경 Dockerfile의 dos2unix 설치, 줄바꿈 변환 파트 제거
환경변수가 인식되지 않는 오류 수정
.gitignore에 CI/CD 워크플로우 폴더(.github) 대상 제외
테스트 코드에 환경 변수 주입용도 코드 추가
빌드 단계에 env로 추가
AWS 자격 증명 설정 추가로 로직 변경
다시 env 주입 로직으로 변경하고 gradle에서 test 환경에서 주입받도록 하는 코드 추가
GitHub Actions - Secrets에서 주입받도록 변경 AwsProperties 클래스 삭제 (로컬 env 주입 로직 삭제) @value를 통해 CI로 주입받은 환경변수 값을 application.yml로부터 직접 주입 CI 주입 방식 변경, 빌드 전에 주입
설정 파일을 통한 주입에서 System.getenve()로 변경
기본 시큐리티 설정 변경 logoutUrl: /api/auth/logout logoutSuccessHandler: SimpleUrlLogoutSuccessHandler -> HttpStatusReturningLogoutSuccessHandler
ADMIN, CHANNEL_MANAGER, USER 권한 추가 기본 권한: USER ADMIN 자동 생성 (1회에 한함) 권한 업데이트 API 추가
메서드 단위 권한 추가 ADMIN 계정이 로그인되지 않는 오류 해결 String role -> Role role로 변경
권한 업데이트 시 세션 만료
UserStatus 관련 코드 모두 삭제 isLoggedIn() 로직이 AuthService, UserService에 중복되어 추후 리팩토링 필요
maxSessionsPreventsLogin: true -> false
Auth, Channel에서 Controller와 Service에 동시에 적용되어 있던 메서드 단위 권한을 Controller에만 적용
CI 트리거 브랜치 변경: main -> dev BasicAuthService 형변환 코드 제거: (DiscodeitUserDetails)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
기본 요구사항
Spring Security 환경설정
프로젝트에 Spring Security 의존성을 추가하세요.
Security 설정 클래스를 생성하세요.
com.sprint.mission.discodeit.config
SecurityConfig
SecurityFilterChain Bean을 선언하세요.
가장 기본적인 SecurityFilterChain을 등록하고, 이때 등록되는 필터 목록을 디버깅해보세요. 필터 목록은 PR에 첨부하세요.
필터 목록
DisableEncodeUrlFilter
WebAsyncManagerIntegrationFilter
SecurityContextHolderFilter
HeaderWriterFilter
CsrfFilter
LogoutFilter
RequestCacheAwareFilter
SecurityContextHolderAwareRequestFilter
AnonymousAuthenticationFilter
ExceptionTranslationFilter
개발 환경에서 Spring Security 모듈의 로깅 레벨을 trace로 설정하세요.
CSRF 보호 설정하기
CsrfTokenRepository
구현체를CookieCsrfTokenRepository
로 설정하세요.디폴트 구현체는
HttpSessionCsrfTokenRepository
입니다.이때 클라이언트에서 쿠키에 저장된 CSRF 토큰에 접근해야 하므로 Http Only는 false로 설정합니다.
CsrfTokenRequestHandler
컴포넌트를 대체하세요.XorCsrfTokenRequestAttributeHandler
입니다.Spring 공식문서에서 권장하는 CSR+SPA(Single Page Application) 환경에 적합한 구현체를 정의하세요.
CSRF 토큰을 발급하는 API를 구현하세요.
API 스펙
CsrfToken 파라미터를 메서드 인자로 선언하면, HandlerMethodArgumentResolver를 통해 자동으로 주입됩니다. (공식문서)
GET 요청에는 CSRF 인증이 이루어지지 않기 때문에 토큰이 초기화되지 않습니다. 따라서 명시적으로 메소드에서 토큰을 호출합니다.
회원가입
PasswordEncoder
를 통해 해시로 저장하세요.PasswordEncoder
의 구현체는BCryptPasswordEncoder
를 활용하세요.로그인
UserDetails
UserDetailsService
PasswordEncoder
: 이전에 정의한 BCryptPasswordEncoder로 대체됩니다.AuthenticationSuccessHandler
AuthenticationFailureHandler
각 컴포넌트의 기본 구현체가 무엇인지 디버깅해보세요.
UserDetails
:org.springframework.security.core.userdetails.User
, Spring Security가 기본으로 제공하는User
클래스UserDetailsService
:InMemoryUserDetailsManager
,username
으로UserDetails
를 로드하는 서비스.application.yml
에spring.security.user.*
로 설정된 사용자 정보를 이 Manager가 메모리에 저장하고 불러옴PasswordEncoder
:DelegatingPasswordEncoder
, 비밀번호 암호화/검증{id}encodedPassword
형태를 지원한다.AuthenticationSuccessHandler
:SavedRequestAwareAuthenticationSuccessHandler
, 로그인 성공 시 동작 정의 (redirect, JSON 응답 등)AuthenticationFailureHandler
:SimpleUrlAuthenticationFailureHandler
, 기본적으로 록읜 폼 페이지에?error
파라미터를 붙여서 redirect로그인을 처리할 url을 /api/auth/login로 설정하세요.
UserDetailsService 컴포넌트를 대체하세요.
UserDetails 컴포넌트를 대체하세요.
AuthenticationSuccessHandler 컴포넌트를 대체하세요.
AuthenticiationFailureHandler 컴포넌트를 대체하세요.
이제 로그인 처리는 SecurityFilterChain에서 모두 처리되기 때문에 기존에 구현했던 로그인 관련 코드는 제거하세요.
AuthApi.login
,AuthController.login
AuthService.login
LoginRequest
인증 - 세션을 활용한 현재 사용자 정보 조회
UserDto
)를 가져올 수 있도록 API를 정의하세요.GET /api/auth/me
JSESSIONID=…
200 UserDto
SecurityFilterChain
의 필터를 통해 인증에 성공하면 Controller에서@AuthenticationPrincipal
를 통해 인증 정보에 접근할 수 있습니다.인증 - 로그아웃
Spring Security의 logout 흐름은 그대로 유지하면서 필요한 부분만 대체합니다.
로그아웃을 처리할 url을
/api/auth/logout
로 설정하세요.LogoutSuccessHandler
컴포넌트를 대체하세요.SimpleUrlLogoutSuccessHandler
입니다.HttpStatusReturningLogoutSuccessHandler
로 대체하세요.204 Void
응답을 반환하세요.인가 - 권한 정의
다음과 같이 권한을 정의하세요.
ADMIN
CHANNEL_MANAGER
USER
데이터베이스 스키마를 변경하세요.
회원 가입 시 모든 사용자는 USER 권한을 기본 권한으로 설정하세요.
사용자 권한을 수정하는 API를 구현하세요.
API 스펙
PUT /api/auth/role
Body UserRoleUpdateRequest
200 UserDto
애플리케이션 실행 시 ADMIN 권한을 가진 어드민 계정이 초기화되도록 구현하세요.
DiscodietUserDetails.getAuthorities()
를 수정하세요.인가 - 권한 적용
authorizeHttpRequests
를 활성화하고, 모든 요청을 인증하도록 설정하세요.다음의 요청은 인증하지 않도록 설정하세요.
Method Security를 활성화하세요.
Service의 메소드 별로 아래의 조건에 맞게 권한을 수정하세요.
CHANNEL_MANAGER
권한을 가져야합니다.ADMIN
권한을 가져야합니다.적절한 권한이 없는 경우 403 응답을 반환하세요.
RoleHierarchy
를 활용해 권한의 계층 구조를 정의하세요.심화 요구사항
세션 관리 고도화
동일한 계정으로 동시 로그인할 수 없도록 설정하세요.
sessionConcurrency
설정을 활용하세요.DiscodeitUserDetails
의equals()
,hashcode()
메소드를 오버라이딩하세요.권한이 변경된 사용자가 로그인 상태라면 세션을 무효화하세요.
sessionRegistry
를 활용하세요.httpSessionEventPublisher
: HttpSession이 만료된 경우 이벤트를 통해 SessionRegistry의 SessionInformation도 자동으로 만료하기 위해 필요한 Bean입니다.UserStatus 엔티티 대신 SessionRegistry를 활용해 사용자의 로그인 여부를 판단하도록 리팩토링하세요.
HttpSession
만료 시SessionRegistry
의SessionInformation
도 자동으로 만료 처리할 수 있도록HttpSessionEventPublisher
를 Bean으로 등록합니다.로그인 고도화 - RememberMe
로그인 요청 파라미터(
remember-me
)가 true인 경우 세션이 무효화되어도 자동으로 다시 로그인되도록 하세요.로그인 화면에서 로그인 유지 체크 후 로그인하면
remember-me
파라미터가true
로 설정되어 요청합니다.remeberMe 설정을 활용하세요.
로그인 상태에서
JESSIONID
쿠키를 삭제 후 새로고침했을 때 인증 상태가 유지 되는지 확인해보세요.권한 적용 고도화
SpEL
을 활용해 Method Security 기반 리소스 보호 정책을 강화해보세요.주요 변경사항
UserStatus
->SessionRegistry
기반 세션 조회로 로그인 여부를 확인하게 변경하였습니다.UserService
와AuthService
에isLoggedInByUserId(UUID userId)
메서드를 추가하여SessionRegistry
를 조회하였습니다.UserMapper
의toDto(User user)
메서드의 파라미터를 변경하였습니다. (User user
->User user, boolean online
)SessionRegistry
조회로 로그인 여부를 확인한 뒤,boolean
값을 주입합니다.DiscodeitUserDetailService.loadUserByUsername(String username)
메서드의 로직 일부를 변경하였습니다.userMapper.toDto()
의 파라미터로online
값은false
를 전달합니다. (UserDetails
로딩중이므로 초기값인false
를 주입합니다.)ADMIN
계정을 최초 1회에 한하여 자동으로 생성되게 구현하였습니다.AdminInitializer
클래스가 추가되었습니다.메시지 수정, 삭제를 작성자만 할 수 있도록 권한을 적용하였습니다.
MessageService
에isAuthor(UUID messageId, Authentication authentication)
메서드를 추가하여@PreAuthorize
에서SpEL
문법으로 현재 로그인된 사용자가 메시지 작성자와 동일한지 검사할 수 있도록 구현하였습니다.스크린샷
ADMIN
권한을 가진 계정은 다른 사용자의 권한을 변경할 수 있습니다.ADMIN
과CHANNEL_MANAGER
는 공개 채널을 생성하거나 변경할 수 있습니다.멘토에게