프로젝트 정리/스프링과 JPA 기반 웹 애플리케이션 개발
21. 로그인 기억하기 (RememberMe)
JunGi Jeong
2021. 12. 11. 01:24
세션이 만료 되더라도 로그인을 유지하고 싶을 때 사용하는 방법
- 쿠키에 인증 정보를 남겨두고 세션이 만료 됐을 때에는 쿠키에 남아있는 정보로 인증한다.
application.properties
server.servlet.session.timeout=30m
// session 유지 기본 30분으로 설정되어있음 -> 시간 늘리면 메모리 부담 ↑
해시 기반의 쿠키
- Username
- Password
- 만료 기간
- Key (애플리케이션 마다 다른 값을 줘야 한다.)
- 치명적인 단점, 쿠키를 다른 사람이 가져가면... 그 계정은 탈취당한 것과 같다.
조금 더 안전한 방법은?
- 쿠키안에 랜덤한 문자열(토큰)을 만들어 같이 저장하고 매번 인증할 때마다 바꾼다.
- Username, 토큰
- 문제는, 이 방법도 취약하다. 쿠키를 탈취 당하면, 해커가 쿠키로 인증을 할 수 있고, 희생자는 쿠키로 인증하지 못한다.
조금 더 개선한 방법
- Username, 토큰(랜덤, 매번 바뀜), 시리즈(랜덤, 고정)
- 쿠키를 탈취 당한 경우, 희생자는 유효하지 않은 토큰과 유효한 시리즈와 Username으로 접속하게 된다.
- ex) hacker = {username: hello, token: 123, series: @!#}, user = {username: hello, token: 456, series: @!#} -> delete
- 이 경우, 모든 토큰을 삭제하여 해커가 더이상 탈취한 쿠키를 사용하지 못하도록 방지할 수 있다.
스프링 시큐리티 설정: 해시 기반 설정
http.rememberMe().key("랜덤한 키 값")
스프링 시큐리티 설정: 보다 안전한 영속화 기반 설정
http.rememberMe()
.userDetailsService(accountService)
.tokenRepository(tokenRepository());
@Bean
public PersistentTokenRepository tokenRepository() {
JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
jdbcTokenRepository.setDataSource(dataSource);
return jdbcTokenRepository;
}
persistent_logins 테이블 만들기
create table persistent_logins
(username varchar(64) not null, series varchar(64) primary key,
token varchar(64) not null, last_used timestamp not null)
또는 @Entity 맵핑으로 생성.
실습
- 시작 커밋: https://github.com/devjun63/whiteship-studyolle/commit/f2d307ce5861e52e0b87e25053bd27e886232ae7
- 완료 커밋:https://github.com/devjun63/whiteship-studyolle/commit/fb9abd45d3eef033fd4ab80d7f95c54391541140
21. 로그인 기억하기 (RememberMe) · devjun63/whiteship-studyolle@fb9abd4
및 index Page dropdown toggle 작동하도록 코드 변경
github.com
로그인 로그아웃 테스트 · devjun63/whiteship-studyolle@f2d307c
Permalink This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository. Browse files 로그인 로그아웃 테스트 Loading branch information Showing 13 changed files with 357 additions and 45 deletion
github.com