Skip to content

Commit 47c0189

Browse files
committed
[feat] : 로그인시 ContentType/Form-Data뿐만 아니라 application-json 적용
1 parent 047364e commit 47c0189

File tree

4 files changed

+178
-10
lines changed

4 files changed

+178
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.cona.KUsukKusuk.global.dto;
2+
3+
import lombok.Builder;
4+
5+
@Builder
6+
public record LoginRequest(String username, String password) {
7+
}

src/main/java/com/cona/KUsukKusuk/global/security/LoginFilter.java

+50-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package com.cona.KUsukKusuk.global.security;
22

3+
import com.cona.KUsukKusuk.global.dto.LoginRequest;
34
import com.cona.KUsukKusuk.global.redis.RedisService;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
46
import jakarta.servlet.FilterChain;
7+
import jakarta.servlet.http.Cookie;
58
import jakarta.servlet.http.HttpServletRequest;
69
import jakarta.servlet.http.HttpServletResponse;
710
import java.io.IOException;
811
import java.util.Collection;
912
import java.util.Iterator;
1013
import java.util.concurrent.TimeUnit;
1114
import org.springframework.beans.factory.annotation.Autowired;
15+
import org.springframework.http.HttpStatus;
16+
import org.springframework.http.MediaType;
1217
import org.springframework.security.authentication.AuthenticationManager;
1318
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
1419
import org.springframework.security.core.Authentication;
@@ -22,6 +27,8 @@ public class LoginFilter extends UsernamePasswordAuthenticationFilter {
2227
private final AuthenticationManager authenticationManager;
2328

2429
private final JWTUtil jwtUtil;
30+
private final ObjectMapper objectMapper = new ObjectMapper();
31+
2532

2633

2734
public LoginFilter(AuthenticationManager authenticationManager, JWTUtil jwtUtil) {
@@ -33,19 +40,40 @@ public LoginFilter(AuthenticationManager authenticationManager, JWTUtil jwtUtil)
3340
@Override
3441
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
3542

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+
4160

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);
4365

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+
}
4572
}
4673

4774
@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 {
4977

5078
//UserDetailsS
5179
CustomUserDetails customUserDetails = (CustomUserDetails) authentication.getPrincipal();
@@ -60,8 +88,7 @@ protected void successfulAuthentication(HttpServletRequest request, HttpServletR
6088
String refreshToken = jwtUtil.createRefreshToken(username, password, 86400000*7L);
6189

6290

63-
response.addHeader("Authorization", "Bearer " + accessToken);
64-
response.addHeader("RefreshToken","Bearer "+refreshToken);
91+
sendTokenResponse(response,accessToken,refreshToken);
6592

6693
}
6794

@@ -72,4 +99,17 @@ protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServle
7299
response.getWriter().write("해당 사용자의 아이디나 비밀번호가 옳지 않습니다. 다시 확인해주세요");
73100
response.setStatus(400);
74101
}
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+
}
75115
}

src/main/java/com/cona/KUsukKusuk/global/security/SecurityConfig.java

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public CorsConfiguration getCorsConfiguration(HttpServletRequest request) {
7979
http
8080
.authorizeHttpRequests((auth) -> auth
8181
.requestMatchers("/login", "/users/join","/health","/","/users/refresh").permitAll()
82+
.requestMatchers("/**").permitAll()
8283
.requestMatchers( "/swagger-ui/**", "/v3/api-docs/**").permitAll()
8384
//스웨거 접근권한 허용
8485
.anyRequest().authenticated());

src/main/resources/static/index.html

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Welcome to 동훈's Adventure</title>
7+
<style>
8+
body {
9+
margin: 0;
10+
overflow: hidden;
11+
}
12+
13+
#ocean {
14+
background: #3498db;
15+
height: 100vh;
16+
animation: wave 1s infinite linear;
17+
}
18+
19+
.boat {
20+
width: 100px;
21+
height: 100px;
22+
position: absolute;
23+
bottom: 10px;
24+
left: 50%;
25+
transform: translateX(-50%);
26+
animation: boatMove 5s infinite alternate ease-in-out;
27+
}
28+
29+
.sun {
30+
width: 100px;
31+
height: 100px;
32+
background: #f39c12;
33+
border-radius: 50%;
34+
position: absolute;
35+
top: 50px;
36+
left: 50%;
37+
transform: translateX(-50%);
38+
animation: sunMove 10s infinite linear;
39+
}
40+
41+
.cloud {
42+
width: 150px;
43+
height: 70px;
44+
background: #ecf0f1;
45+
position: absolute;
46+
top: 20px;
47+
left: 10%;
48+
border-radius: 10px;
49+
animation: cloudMove 15s infinite linear;
50+
}
51+
52+
h1 {
53+
position: absolute;
54+
top: 30%;
55+
left: 50%;
56+
transform: translate(-50%, -50%);
57+
font-family: 'Arial', sans-serif;
58+
font-size: 3em;
59+
color: #fff;
60+
}
61+
62+
h2 {
63+
position: absolute;
64+
top: 35%;
65+
left: 50%;
66+
transform: translate(-50%, -50%);
67+
font-family: 'Arial', sans-serif;
68+
font-size: 1em;
69+
color: #fff;
70+
}
71+
72+
@keyframes wave {
73+
0%, 100% {
74+
transform: translateY(0);
75+
}
76+
50% {
77+
transform: translateY(-300px);
78+
}
79+
}
80+
81+
@keyframes boatMove {
82+
0%, 100% {
83+
transform: translateX(-500%);
84+
}
85+
50% {
86+
transform: translateX(-450%);
87+
}
88+
}
89+
90+
@keyframes sunMove {
91+
0% {
92+
transform: translateY(500px);
93+
}
94+
50% {
95+
transform: translateY(600px);
96+
}
97+
100% {
98+
transform: translateY(500px);
99+
}
100+
}
101+
102+
@keyframes cloudMove {
103+
0%, 100% {
104+
transform: translateX(10%);
105+
}
106+
50% {
107+
transform: translateX(100%);
108+
}
109+
}
110+
</style>
111+
</head>
112+
<body>
113+
<div id="ocean"></div>
114+
<div class="boat"></div>
115+
<div class="sun"></div>
116+
<div class="cloud"></div>
117+
<h1>쿠석쿠석 서버</h1>
118+
<h2>powered by 동훈</h2>
119+
</body>
120+
</html>

0 commit comments

Comments
 (0)