1
1
package com .cona .KUsukKusuk .global .security ;
2
2
3
+ import com .cona .KUsukKusuk .global .dto .LoginRequest ;
3
4
import com .cona .KUsukKusuk .global .redis .RedisService ;
5
+ import com .fasterxml .jackson .databind .ObjectMapper ;
4
6
import jakarta .servlet .FilterChain ;
7
+ import jakarta .servlet .http .Cookie ;
5
8
import jakarta .servlet .http .HttpServletRequest ;
6
9
import jakarta .servlet .http .HttpServletResponse ;
7
10
import java .io .IOException ;
8
11
import java .util .Collection ;
9
12
import java .util .Iterator ;
10
13
import java .util .concurrent .TimeUnit ;
11
14
import org .springframework .beans .factory .annotation .Autowired ;
15
+ import org .springframework .http .HttpStatus ;
16
+ import org .springframework .http .MediaType ;
12
17
import org .springframework .security .authentication .AuthenticationManager ;
13
18
import org .springframework .security .authentication .UsernamePasswordAuthenticationToken ;
14
19
import org .springframework .security .core .Authentication ;
@@ -22,6 +27,8 @@ public class LoginFilter extends UsernamePasswordAuthenticationFilter {
22
27
private final AuthenticationManager authenticationManager ;
23
28
24
29
private final JWTUtil jwtUtil ;
30
+ private final ObjectMapper objectMapper = new ObjectMapper ();
31
+
25
32
26
33
27
34
public LoginFilter (AuthenticationManager authenticationManager , JWTUtil jwtUtil ) {
@@ -33,19 +40,40 @@ public LoginFilter(AuthenticationManager authenticationManager, JWTUtil jwtUtil)
33
40
@ Override
34
41
public Authentication attemptAuthentication (HttpServletRequest request , HttpServletResponse response ) throws AuthenticationException {
35
42
36
- //클라이언트 요청에서 username, password 추출
37
- String username = obtainUsername (request );
38
- String password = obtainPassword (request );
39
- logger .info ("추출한 username : " +username );
40
- logger .info ("추출한 비밀번호 : " +password );
43
+ if (!request .getContentType ().equals (MediaType .APPLICATION_JSON_VALUE )) {
44
+ // Content-Type이 "application/x-www-form-urlencoded"인 경우
45
+
46
+ String username = obtainUsername (request );
47
+ String password = obtainPassword (request );
48
+ logger .info ("추출한 username : " +username );
49
+ logger .info ("추출한 비밀번호 : " +password );
50
+
51
+ UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken (username , password , null );
52
+
53
+ return authenticationManager .authenticate (authToken );
54
+ }
55
+
56
+ try {
57
+ // Content-Type이 "application/json"일 경우
58
+ LoginRequest loginRequest = objectMapper .readValue (request .getInputStream (), LoginRequest .class );
59
+
41
60
42
- UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken (username , password , null );
61
+ String username = loginRequest .username ();
62
+ String password = loginRequest .password ();
63
+ logger .info ("추출한 username : " +username );
64
+ logger .info ("추출한 비밀번호 : " + password );
43
65
44
- return authenticationManager .authenticate (authToken );
66
+ UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken (username , password , null );
67
+
68
+ return authenticationManager .authenticate (authToken );
69
+ } catch (IOException e ) {
70
+ throw new RuntimeException (e );
71
+ }
45
72
}
46
73
47
74
@ Override
48
- protected void successfulAuthentication (HttpServletRequest request , HttpServletResponse response , FilterChain chain , Authentication authentication ) {
75
+ protected void successfulAuthentication (HttpServletRequest request , HttpServletResponse response , FilterChain chain , Authentication authentication )
76
+ throws IOException {
49
77
50
78
//UserDetailsS
51
79
CustomUserDetails customUserDetails = (CustomUserDetails ) authentication .getPrincipal ();
@@ -60,8 +88,7 @@ protected void successfulAuthentication(HttpServletRequest request, HttpServletR
60
88
String refreshToken = jwtUtil .createRefreshToken (username , password , 86400000 *7L );
61
89
62
90
63
- response .addHeader ("Authorization" , "Bearer " + accessToken );
64
- response .addHeader ("RefreshToken" ,"Bearer " +refreshToken );
91
+ sendTokenResponse (response ,accessToken ,refreshToken );
65
92
66
93
}
67
94
@@ -72,4 +99,17 @@ protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServle
72
99
response .getWriter ().write ("해당 사용자의 아이디나 비밀번호가 옳지 않습니다. 다시 확인해주세요" );
73
100
response .setStatus (400 );
74
101
}
102
+ private void setResponse (HttpServletResponse response ,int status , String message ) throws RuntimeException , IOException {
103
+ response .setContentType ("application/json;charset=UTF-8" );
104
+ response .setStatus (status );
105
+ response .getWriter ().print (message );
106
+ }
107
+ private void sendTokenResponse (HttpServletResponse response , String AT ,String RT ) throws IOException {
108
+ String jsonResponse = "{\" accessToken\" : \" " +"Bearer " + AT +
109
+ "\" , \" refreshToken\" : \" " +"Bearer " + RT + "\" }" ;
110
+
111
+ response .setContentType ("application/json;charset=UTF-8" );
112
+ response .setStatus (HttpStatus .OK .value ());
113
+ response .getWriter ().print (jsonResponse );
114
+ }
75
115
}
0 commit comments