Skip to content

Commit

Permalink
feat:#2 logout 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
jinakim8 committed Aug 1, 2023
1 parent c1fa648 commit 9695006
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
return http.build();
}

protected void configure(HttpSecurity http)throws Exception {
http.logout()
.logoutSuccessUrl("/") //로그아웃 성공시 리다이렉트주소
.invalidateHttpSession(true) //로그아웃 이후 세션 전체 삭제 여부
.deleteCookies("JSSIONID");
}

public CorsConfigurationSource configurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.addAllowedHeader("*");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.time.LocalDateTime;

Expand Down Expand Up @@ -67,4 +71,14 @@ private void addLoginHistory(HttpServletRequest request, CustomUserDetails myUse

loginHistoryService.save(loginHistoryDTO);
}

@GetMapping(value = "/logout")
public String logout(HttpServletRequest request, HttpServletResponse response) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null) {
new SecurityContextLogoutHandler().logout(request, response, authentication);
}
return "redirect:/login";
}

}

0 comments on commit 9695006

Please sign in to comment.