Skip to content

Commit 33f63e4

Browse files
committed
refactor(auth): change endpoint do auth/logout
1 parent 404b071 commit 33f63e4

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.github.renancvitor.inventory.application.authentication.controller;
2+
3+
import org.springframework.http.ResponseEntity;
4+
import org.springframework.web.bind.annotation.PostMapping;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
import jakarta.servlet.http.Cookie;
9+
import jakarta.servlet.http.HttpServletResponse;
10+
import lombok.RequiredArgsConstructor;
11+
12+
@RestController
13+
@RequestMapping("/auth")
14+
@RequiredArgsConstructor
15+
public class LogoutController {
16+
17+
@PostMapping("/logout")
18+
public ResponseEntity<Void> logout(HttpServletResponse response) {
19+
20+
Cookie cookie = new Cookie("access_token", null);
21+
cookie.setHttpOnly(true);
22+
cookie.setSecure(true);
23+
cookie.setPath("/");
24+
cookie.setMaxAge(0);
25+
26+
cookie.setAttribute("SameSite", "Lax");
27+
28+
response.addCookie(cookie);
29+
30+
return ResponseEntity.noContent().build();
31+
}
32+
33+
}

src/main/java/com/github/renancvitor/inventory/infra/security/SecurityConfiguration.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws
4040
.requestMatchers("/actuator/**").permitAll()
4141
.requestMatchers("/v3/api-docs/**", "/swagger-ui/**", "/swagger-ui.html").permitAll()
4242
.requestMatchers(HttpMethod.POST, "/login").permitAll()
43+
.requestMatchers(HttpMethod.POST, "/auth/logout").permitAll()
4344
.anyRequest().authenticated())
4445
.addFilterBefore(securityFilter, UsernamePasswordAuthenticationFilter.class)
4546
.build();

src/main/java/com/github/renancvitor/inventory/infra/security/SecurityFilter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ protected boolean shouldNotFilter(HttpServletRequest request) {
3232

3333
return request.getMethod().equals("OPTIONS")
3434
|| path.equals("/login")
35+
|| path.equals("/auth/logout")
3536
|| path.startsWith("/health")
3637
|| path.startsWith("/actuator")
3738
|| path.startsWith("/swagger-ui")

0 commit comments

Comments
 (0)