Skip to content

Commit cc8b1f1

Browse files
committed
(TP-77) feat: add cookies handling
1 parent f3fbd3c commit cc8b1f1

2 files changed

Lines changed: 32 additions & 10 deletions

File tree

rentplace/src/main/java/kattsyn/dev/rentplace/configs/SecurityConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.springframework.http.HttpMethod;
1010
import org.springframework.http.HttpStatus;
1111
import org.springframework.security.authentication.AuthenticationManager;
12+
import org.springframework.security.config.Customizer;
1213
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
1314
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
1415
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
@@ -65,6 +66,7 @@ public void init() {
6566
@Bean
6667
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
6768
http
69+
.cors(Customizer.withDefaults())
6870
.csrf(CsrfConfigurer::disable)
6971
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
7072
.authorizeHttpRequests(

rentplace/src/main/java/kattsyn/dev/rentplace/controllers/AuthController.java

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import io.swagger.v3.oas.annotations.Operation;
44
import io.swagger.v3.oas.annotations.tags.Tag;
55
import jakarta.security.auth.message.AuthException;
6+
import jakarta.servlet.http.Cookie;
7+
import jakarta.servlet.http.HttpServletResponse;
68
import kattsyn.dev.rentplace.dtos.CodeRequest;
79
import kattsyn.dev.rentplace.dtos.JwtRequest;
810
import kattsyn.dev.rentplace.dtos.JwtResponse;
@@ -11,10 +13,7 @@
1113
import kattsyn.dev.rentplace.services.VerificationCodeService;
1214
import lombok.RequiredArgsConstructor;
1315
import org.springframework.http.ResponseEntity;
14-
import org.springframework.web.bind.annotation.PostMapping;
15-
import org.springframework.web.bind.annotation.RequestBody;
16-
import org.springframework.web.bind.annotation.RequestMapping;
17-
import org.springframework.web.bind.annotation.RestController;
16+
import org.springframework.web.bind.annotation.*;
1817

1918
@RestController
2019
@RequestMapping("${api.path}/auth")
@@ -40,9 +39,21 @@ public ResponseEntity<JwtResponse> login(@RequestBody CodeRequest codeRequest) {
4039
description = "Получает email и код с почты. Возвращает JWT токены"
4140
)
4241
@PostMapping("/login")
43-
public ResponseEntity<JwtResponse> login(@RequestBody JwtRequest authRequest) throws AuthException {
44-
final JwtResponse token = authService.login(authRequest);
45-
return ResponseEntity.ok(token);
42+
public ResponseEntity<JwtResponse> login(@RequestBody JwtRequest authRequest,
43+
HttpServletResponse response) throws AuthException {
44+
JwtResponse tokens = authService.login(authRequest);
45+
46+
// Настройка cookie для refresh token
47+
Cookie refreshTokenCookie = new Cookie("refreshToken", tokens.getRefreshToken());
48+
refreshTokenCookie.setHttpOnly(true);
49+
refreshTokenCookie.setSecure(true); // Для HTTPS
50+
refreshTokenCookie.setPath("/");
51+
refreshTokenCookie.setMaxAge(30 * 24 * 60 * 60); // 30 дней
52+
53+
response.addCookie(refreshTokenCookie);
54+
55+
return ResponseEntity.ok()
56+
.body(new JwtResponse(tokens.getAccessToken(), null));
4657
}
4758

4859
@Operation(
@@ -60,9 +71,18 @@ public ResponseEntity<JwtResponse> getNewAccessToken(@RequestBody RefreshJwtRequ
6071
description = "Принимает еще не истекший RefreshToken и возвращает новый, продленный."
6172
)
6273
@PostMapping("/refresh")
63-
public ResponseEntity<JwtResponse> getNewRefreshToken(@RequestBody RefreshJwtRequest request) throws AuthException {
64-
final JwtResponse token = authService.refresh(request.getRefreshToken());
65-
return ResponseEntity.ok(token);
74+
public ResponseEntity<JwtResponse> refresh(@CookieValue(name = "refreshToken") String refreshToken, HttpServletResponse response) throws AuthException {
75+
JwtResponse jwtResponse = authService.refresh(refreshToken);
76+
77+
Cookie refreshCookie = new Cookie("refreshToken", jwtResponse.getRefreshToken());
78+
refreshCookie.setHttpOnly(true);
79+
refreshCookie.setSecure(true);
80+
refreshCookie.setPath("/");
81+
refreshCookie.setMaxAge(30 * 24 * 60 * 60);
82+
response.addCookie(refreshCookie);
83+
84+
return ResponseEntity.ok()
85+
.body(new JwtResponse(jwtResponse.getAccessToken(), null));
6686
}
6787

6888
}

0 commit comments

Comments
 (0)