minlog
article thumbnail

시큐리티 로그아웃 (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 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>

 

profile

minlog

@jimin-log

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!