시큐리티 로그아웃 (Logout)
1) 로그아웃 설정
📑 SecurityConfig.java
- logoutRequestMatcher(new AntPathRequestMatcher("주소")) : 로그아웃 주소
- logoutSuccessUrl("주소") : 로그아웃 성공 시 이동할 경로
package com.example.travel.config;
...
@Log4j2
@Configuration
public class SecurityConfig {
@Autowired
private UserTravelDetailsService userDetailService; //로그인기능
@Bean
PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
...
http.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/loginForm");
return http.build();
}
}
2) 로그인 상태에 따른 버튼 노출
로그인 상태이면 로그아웃 버튼, 로그인이 되어 있지 않으면 로그인 버튼을 보여주는 설정하기
📑 header.html
- html 마크업에 security관련 속성을 추가해준다.
- xmlns:sec="http://www.thymeleaf.org/extras/spring-security"
<html lang="en" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
- isAnonymous
- isAnonymous()는 로그인 하지 않은 Anonymous User인지 확인하여
- Anonymous User인 경우 true를 return한다.
- isAuthenticated
- 로그인 후 보여지는것들에 주로 사용된다.
- 마이페이지 , 로그아웃 등 ...
<li>
<a class="dropdown-item" sec:authorize="isAnonymous()" th:href="@{/loginForm}">로그인</a>
<a class="dropdown-item" sec:authorize="isAuthenticated()" th:href="@{/logout}" href="/logout">로그아웃</a>
</li>
'BackEnd > Security · JPA' 카테고리의 다른 글
[ Security ] 04. 시큐리티 - 자동 로그인 설정 / Remember Me (1) | 2023.06.01 |
---|---|
[ Security ] 03. 시큐리티 소셜 로그인 처리 - Google oauth2 (0) | 2023.05.24 |
[ Security ] 02. 로그인 - 시큐리티 권한에 따른 페이지 접속 (0) | 2023.05.23 |
[ Security ] 01. 시큐리티 사용하기 - WebSecurityConfigurerAdapter 권한 설정 (스프링 부트 버전 2.7.0이전) (0) | 2023.05.23 |
[ Security ] 01. 시큐리티 사용하기 - 설정 파일 (0) | 2023.05.03 |