-
Notifications
You must be signed in to change notification settings - Fork 16
[안중원] Sprint9 #131
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
joongwonAn
wants to merge
47
commits into
codeit-bootcamp-spring:안중원
Choose a base branch
from
joongwonAn: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 #131
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
Security filter chain: [ DisableEncodeUrlFilter WebAsyncManagerIntegrationFilter SecurityContextHolderFilter HeaderWriterFilter CsrfFilter LogoutFilter RequestCacheAwareFilter SecurityContextHolderAwareRequestFilter AnonymousAuthenticationFilter ExceptionTranslationFilter ]
- CsrfTokenRepository 구현체를 CookieCsrfTokenRepository로 설정 - 디폴트 구현체는 HttpSessionCsrfTokenRepository - (CSR) 클라이언트에서 쿠키에 저장된 CSRF 토큰에 접근해야 하므로 Http Only는 false로 설정
* csrfTokenRequestHandler - Spring Security에서 CSRF(크로스 사이트 요청 위조) 공격을 방지하기 위해 사용되는 인터페이스(CSRF 토큰을 읽고 검증할 때 사용) - 기본적으로 CsrfTokenRequestAttributeHandler 구현체 사용
- SPA 환경에서는 헤더 기반 CSRF 토큰 검증을 지원하고, 동시에 기존 방식(파라미터)도 fallback으로 지원
- API 스펙 - 엔드포인트: GET /api/auth/csrf-token - 요청: 없음 - 응답: 203 Void
- 필터 목록 Security filter chain: [ DisableEncodeUrlFilter WebAsyncManagerIntegrationFilter SecurityContextHolderFilter HeaderWriterFilter CsrfFilter LogoutFilter UsernamePasswordAuthenticationFilter // 추가 DefaultResourcesFilter // 추가 DefaultLoginPageGeneratingFilter // 추가 DefaultLogoutPageGeneratingFilter // 추가 RequestCacheAwareFilter SecurityContextHolderAwareRequestFilter AnonymousAuthenticationFilter ExceptionTranslationFilter ]
POST /api/auth/login ↓ UsernamePasswordAuthenticationFilter ↓ AuthenticationManager (ProviderManager) ↓ DiscodeitUserDetailsService.loadUserByUsername() ↓ DiscodeitUserDetails 생성 + 비밀번호 검증 ↓ Authentication 성공 → SecurityContextHolder 저장 ↓ LoginSuccessHandler.onAuthenticationSuccess() 호출
* 인증 X - Csrf Token 발급 - 회원가입 - 로그인 - 로그아웃 - API가 아닌 요청(Swagger, Actuator 등)
- admin 계정을 초기화할 떼 userstatus가 없어서 오류 발생
- 퍼블릭 채널 생성, 수정, 삭제는 `CHANNEL_MANAGER` 권한을 가져야합니다. - 사용자 권한 수정은 `ADMIN` 권한을 가져야합니다.
다음의 요청은 인증 X - Csrf Token 발급 - 회원가입 - 로그인 - 로그아웃 - API가 아닌 요청(Swagger, Actuator 등)
- 관리자 > 채널 매니저 > 일반 사용자 - 관리자 권한은 채널 매니저, 일반 사용자 권한을 포함합니다. - 채널 매니저 권한은 일반 사용자 권한을 포함합니다.
- 사용자 정보 수정, 삭제는 본인만 할 수 있습니다. - 메시지 수정, 삭제는 해당 메시지를 작성한 사람만 할 수 있습니다.
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는 SSR 환경을 가정한 기본 설정이 많습니다. 따라서 본 미션을 수행할 때 기본 설정을 사용하지 않고 커스터마이징 해야하는 설정이 다소 있는 점을 유의하기 바랍니다.
단, Spring Security의 기본 인증 플로우는 최대한 유지하도록 노력합니다.
Spring Security 환경설정
프로젝트에 Spring Security 의존성을 추가하세요.
Security 설정 클래스를 생성하세요.
com.sprint.mission.discodeit.config
SecurityConfig
SecurityFilterChain
Bean을 선언하세요.SecurityFilterChain
을 등록하고, 이때 등록되는 필터 목록을 디버깅해보세요. 필터 목록은 PR에 첨부하세요.개발 환경에서 Spring Security 모듈의 로깅 레벨을
trace
로 설정하세요.CSRF 보호 설정하기
디스코드잇은 CSR 방식이기 때문에 CSRF 토큰은 다음과 같이 처리합니다.
Set-Cookie
)를 통해 쿠키에 저장X-XSRF-TOKEN
)에 포함X-XSRF-TOKEN
,Cookie
)을 비교해 유효성 검증CsrfTokenRepository
구현체를CookieCsrfTokenRepository
로 설정하세요.HttpSessionCsrfTokenRepository
입니다.false
로 설정합니다.CsrfTokenRequestHandler
컴포넌트를 대체하세요.디폴트 구현체는
XorCsrfTokenRequestAttributeHandler
입니다.[Spring 공식문서](https://docs.spring.io/spring-security/reference/servlet/exploits/csrf.html#csrf-integration-javascript-spa)에서 권장하는 CSR+SPA(Single Page Application) 환경에 적합한 구현체를 정의하세요.
CSRF 토큰을 발급하는 API를 구현하세요.
GET /api/auth/csrf-token
203 Void
CsrfToken
파라미터를 메서드 인자로 선언하면,HandlerMethodArgumentResolver
를 통해 자동으로 주입됩니다. ([공식문서](https://docs.spring.io/spring-security/reference/servlet/integrations/mvc.html#mvc-csrf-resolver))회원가입
POST /api/users
Body UserCreateRequest, MultipartFile
200 UserDto
PasswordEncoder
를 통해 해시로 저장하세요.PasswordEncoder
의 구현체는BCryptPasswordEncoder
를 활용하세요.인증 - 로그인
formLogin
을 기본값으로 활성화하고, 추가된 필터를 확인해보세요.Spring Security의 formLogin 인증 흐름은 그대로 유지하면서 필요한 부분만 대체합니다.
UserDetails
UserDetailsService
PasswordEncoder
: 이전에 정의한BCryptPasswordEncoder
로 대체됩니다.AuthenticationSuccessHandler
AuthenticationFailureHandler
로그인을 처리할 url을
/api/auth/login
로 설정하세요.UserDetailsService
컴포넌트를 대체하세요.디폴트 구현체는
InMemoryUserDetailsManager
입니다.DiscodeitUserDetailsService
를 정의하세요.UserDetails
객체를 생성합니다.UserDetails
컴포넌트를 대체하세요.디폴트 구현체는
org.springframework.security.core.userdetails.User
입니다.DiscodeitUserDetails
를 정의하세요.Principal
)에 담을 수 있는 정보를 자유롭게 확장할 수 있습니다.UserDto
와 비밀번호 정보를 저장하세요.앞서 정의한
DiscodeitUserDetailsService
에서DiscodeitUserDetails
를 생성 후 반환하세요.AuthenticationSuccessHandler
컴포넌트를 대체하세요.디폴트 구현체는
SavedRequestAwareAuthenticationSuccessHandler
입니다.LoginSuccessHandler
를 정의하고 대체하세요.200 UserDto
로 응답합니다.설정에 추가하세요.
AuthenticiationFailureHandler
컴포넌트를 대체하세요.디폴트 구현체는
SimpleUrlAuthenticationFailureHandler
입니다.LoginFailureHandler
를 정의하고 대체하세요.401 ErrorResponse
로 응답합니다.설정에 추가하세요.
이제 로그인 처리는 SecurityFilterChain에서 모두 처리되기 때문에 기존에 구현했던 로그인 관련 코드는 제거하세요.
AuthApi.login
,AuthController.login
AuthService.login
LoginRequest
인증 - 세션을 활용한 현재 사용자 정보 조회
UserDto
)를 가져올 수 있도록 API를 정의하세요.GET /api/auth/me
Header(자동 포함) Cookie: JSESSIONID=…
200 UserDto
SecurityFilterChain
의 필터를 통해 인증에 성공하면Controller
에서@AuthenticationPrincipal
를 통해 인증 정보에 접근할 수 있습니다.인증 - 로그아웃
Spring Security의 logout 흐름은 그대로 유지하면서 필요한 부분만 대체합니다.
이번 미션에서는 2가지 요소를 대체합니다.
LogoutSuccessHandler
로그아웃을 처리할 url을
/api/auth/logout
로 설정하세요.LogoutSuccessHandler
컴포넌트를 대체하세요.디폴트 구현체는
SimpleUrlLogoutSuccessHandler
입니다.HttpStatusReturningLogoutSuccessHandler
로 대체하세요.204 Void
응답을 반환하세요.인가 - 권한 정의
다음과 같이 권한을 정의하세요.
ADMIN
CHANNEL_MANAGER
USER
데이터베이스 스키마를 변경하세요.
회원 가입 시 모든 사용자는
USER
권한을 기본 권한으로 설정하세요.사용자 권한을 수정하는 API를 구현하세요.
PUT /api/auth/role
Body UserRoleUpdateRequest
200 UserDto
애플리케이션 실행 시
ADMIN
권한을 가진 어드민 계정이 초기화되도록 구현하세요.DiscodietUserDetails.getAuthorities
를 수정하세요.인가 - 권한 적용
authorizeHttpRequests
를 활성화하고, 모든 요청을 인증하도록 설정하세요.다음의 요청은 인증하지 않도록 설정하세요.
Method Security를 활성화하세요.
Service의 메소드 별로 아래의 조건에 맞게 권한을 수정하세요.
CHANNEL_MANAGER
권한을 가져야합니다.ADMIN
권한을 가져야합니다.적절한 권한이 없는 경우 403 응답을 반환하세요.
SecurityFilterChain
GlobalExceptionHandler
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
로 설정되어 요청합니다.[otona45pe-image.png](https://bakey-api.codeit.kr/api/files/resource?root=static&seqId=14411&version=1&directory=/otona45pe-image.png&name=otona45pe-image.png)
remeberMe
설정을 활용하세요.로그인 상태에서
JESSIONID
쿠키를 삭제 후 새로고침했을 때 인증 상태가 유지 되는지 확인해보세요.[aroseetgw-image.png](https://bakey-api.codeit.kr/api/files/resource?root=static&seqId=14411&version=1&directory=/aroseetgw-image.png&name=aroseetgw-image.png)
권한 적용 고도화
SpEL
을 활용해 Method Security 기반 리소스 보호 정책을 강화해보세요.