0. 자동 로그인 정보를 저장할 DB 테이블
CREATE TABLE PERSISTENT_LOGINS(
USERNAME VARCHAR2(50),
SERIES VARCHAR2(200),
TOKEN VARCHAR2(200),
LAST_USED TIMESTAMP,
CONSTRAINT PK_PL PRIMARY KEY (SERIES)
);
1. security-context.xml 설정
<security:http> 안에 <security:remember-me> 를 추가로 작성한다.
<security:http>
<!--
자동 로그인이 체크되었을 경우 쿠키를 생성해 로컬에 저장하고
위에서 작성한 테이블에 자동으로 데이터를 저장
dataSource : root-context.xml 에서 MyBatis를 설정하기 위해 사용한
BasicDataSource 빈의 id
token-validity-seconds : 쿠키의 유효기간 (초)
604800초 = 7일
-->
<security:remember-me data-source-ref="dataSource" token-validity-seconds="604800" />
<!-- 로그아웃 했을 때 쿠키가 삭제될 수 있도록 delete-cookies 속성을 추가 -->
<security:logout logout-url="/logout" invalidate-session="true"
delete-cookies="remember-me, JSESSION_ID" />
</security:http>
2. 로그인 form 작성
자동 로그인 요청을 위한 <input type="checkbox" /> 태그를 추가한다. 이 때 태그의 name 속성을 'remember-me'로 설정해야 한다.
<form action="/login" method="post">
<input type="text" id="username" name="username" aria-describedby="username"
class="form-control" placeholder="아이디" required />
<input type="password" id="password" name="password" aria-describedby="password"
class="form-control" placeholder="비밀번호" required />
<input type="checkbox" id="remember-me" name="remember-me" class="form-check-input" />
<label class="form-check-label" for="remember-me">자동 로그인</label>
<button type="submit" class="btn btn-primary btn-block">로그인</button>
<button type="button" class="btn btn-secondary btn-block">취소</button>
<sec:csrfInput/>
</form>
이후 자동 로그인을 체크하고 로그인한 뒤 브라우저를 종료하고 다시 접속해도 로그인이 유지되어있으면 성공이다. 또한, 맨 위에서 생성한 테이블에 데이터가 추가되어있다면 성공이다.
'Java > Spring Framework' 카테고리의 다른 글
[Spring Framework] 어노테이션을 사용한 시큐리티 지정 (0) | 2023.02.15 |
---|---|
[Spring Framework] 시큐리티 설정 정리 (0) | 2023.02.15 |
[Spring Framework] 스프링 시큐리티 태그 라이브러리 (0) | 2023.02.14 |
[Spring Framework] 스프링 시큐리티와 데이터베이스 연동 (0) | 2023.02.14 |
@PathVariable (0) | 2023.02.08 |