Skip to content

Commit 5ebcf43

Browse files
committed
fix: Azure Funtion에서 Spring Bean 주입 오류 해결
1 parent b18a508 commit 5ebcf43

File tree

3 files changed

+42
-66
lines changed

3 files changed

+42
-66
lines changed

src/main/java/org/inhahackers/optmo_user_be/function/IncreaseElecFunction.java

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,26 @@
11
package org.inhahackers.optmo_user_be.function;
22

33
import com.microsoft.azure.functions.*;
4-
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
5-
import com.microsoft.azure.functions.annotation.FunctionName;
6-
import com.microsoft.azure.functions.annotation.HttpTrigger;
7-
import lombok.NoArgsConstructor;
84
import lombok.RequiredArgsConstructor;
95
import org.inhahackers.optmo_user_be.dto.ElecRequest;
106
import org.inhahackers.optmo_user_be.exception.JwtAuthenticationException;
117
import org.inhahackers.optmo_user_be.exception.UserNotFoundException;
128
import org.inhahackers.optmo_user_be.service.JwtTokenService;
139
import org.inhahackers.optmo_user_be.service.UserService;
10+
import org.springframework.stereotype.Component;
1411

1512
import java.util.Optional;
13+
import java.util.function.Function;
1614

15+
@Component("increaseElecFunction")
1716
@RequiredArgsConstructor
18-
public class IncreaseElecFunction {
17+
public class IncreaseElecFunction implements Function<HttpRequestMessage<Optional<String>>, HttpResponseMessage> {
1918

2019
private final JwtTokenService jwtTokenService;
2120
private final UserService userService;
2221

23-
@FunctionName("increaseElecFunction")
24-
public HttpResponseMessage run(
25-
@HttpTrigger(
26-
name = "increaseelec",
27-
methods = {HttpMethod.POST},
28-
authLevel = AuthorizationLevel.ANONYMOUS)
29-
HttpRequestMessage<Void> request,
30-
final ExecutionContext context) {
31-
32-
context.getLogger().info("increaseElecFunction called");
33-
22+
@Override
23+
public HttpResponseMessage apply(HttpRequestMessage<Optional<String>> request) {
3424
try {
3525
// 1. Authorization 헤더 추출
3626
String authHeader = request.getHeaders().get("Authorization");
@@ -41,10 +31,30 @@ public HttpResponseMessage run(
4131
}
4232
String accessToken = authHeader.substring("Bearer ".length());
4333

44-
// 2. Request Body 파싱
34+
// 2. 쿼리 파라미터에서 long 값 안전하게 파싱
35+
String useElecStr = request.getQueryParameters().get("useElecEstimate");
36+
String llmElecStr = request.getQueryParameters().get("llmElecEstimate");
37+
38+
if (useElecStr == null || llmElecStr == null) {
39+
return request.createResponseBuilder(HttpStatus.BAD_REQUEST)
40+
.body("Missing required query parameters: useElecEstimate and llmElecEstimate")
41+
.build();
42+
}
43+
44+
long useElecEstimate;
45+
long llmElecEstimate;
46+
try {
47+
useElecEstimate = Long.parseLong(useElecStr);
48+
llmElecEstimate = Long.parseLong(llmElecStr);
49+
} catch (NumberFormatException e) {
50+
return request.createResponseBuilder(HttpStatus.BAD_REQUEST)
51+
.body("Invalid number format in query parameters")
52+
.build();
53+
}
54+
4555
ElecRequest elecRequest = ElecRequest.builder()
46-
.useElecEstimate(Long.getLong(request.getQueryParameters().get("useElecEstimate")))
47-
.llmElecEstimate(Long.getLong(request.getQueryParameters().get("llmElecEstimate")))
56+
.useElecEstimate(useElecEstimate)
57+
.llmElecEstimate(llmElecEstimate)
4858
.build();
4959

5060
// 3. 토큰에서 userId 추출 및 처리
@@ -55,7 +65,7 @@ public HttpResponseMessage run(
5565
// 4. 성공 응답
5666
return request.createResponseBuilder(HttpStatus.OK)
5767
.header("Content-Type", "text/plain")
58-
.body("Successfully Increase Elec and Cost Estimate")
68+
.body("Successfully Increased Elec and Cost Estimate")
5969
.build();
6070

6171
} catch (JwtAuthenticationException e) {
@@ -68,11 +78,6 @@ public HttpResponseMessage run(
6878
.body("User Not Found: " + e.getMessage())
6979
.build();
7080

71-
} catch (IllegalArgumentException e) {
72-
return request.createResponseBuilder(HttpStatus.BAD_REQUEST)
73-
.body(e.getMessage())
74-
.build();
75-
7681
} catch (Exception e) {
7782
return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR)
7883
.body("Internal error: " + e.getMessage())

src/main/java/org/inhahackers/optmo_user_be/function/JWTUserFunction.java

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,29 @@
11
package org.inhahackers.optmo_user_be.function;
22

33
import com.microsoft.azure.functions.*;
4-
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
5-
import com.microsoft.azure.functions.annotation.FunctionName;
6-
import com.microsoft.azure.functions.annotation.HttpTrigger;
7-
import lombok.NoArgsConstructor;
84
import lombok.RequiredArgsConstructor;
95
import org.inhahackers.optmo_user_be.dto.UserResponse;
106
import org.inhahackers.optmo_user_be.entity.User;
117
import org.inhahackers.optmo_user_be.exception.JwtAuthenticationException;
128
import org.inhahackers.optmo_user_be.exception.UserNotFoundException;
139
import org.inhahackers.optmo_user_be.service.JwtTokenService;
1410
import org.inhahackers.optmo_user_be.service.UserService;
11+
import org.springframework.stereotype.Component;
1512

1613
import java.util.Optional;
14+
import java.util.function.Function;
1715

16+
@Component("jwtUserFunction")
1817
@RequiredArgsConstructor
19-
public class JWTUserFunction {
18+
public class JWTUserFunction implements Function<HttpRequestMessage<Optional<String>>, HttpResponseMessage> {
2019

2120
private final JwtTokenService jwtTokenService;
2221
private final UserService userService;
2322

24-
@FunctionName("jwtUserFunction")
25-
public HttpResponseMessage run(
26-
@HttpTrigger(
27-
name = "jwtuser",
28-
methods = {HttpMethod.GET, HttpMethod.POST},
29-
authLevel = AuthorizationLevel.ANONYMOUS)
30-
HttpRequestMessage<Void> request,
31-
final ExecutionContext context) {
32-
33-
context.getLogger().info("jwtUserFunction called");
23+
@Override
24+
public HttpResponseMessage apply(HttpRequestMessage<Optional<String>> request) {
3425

3526
try {
36-
// 1. Authorization 헤더 추출
3727
String authHeader = request.getHeaders().get("Authorization");
3828
if (authHeader == null || !authHeader.startsWith("Bearer ")) {
3929
return request.createResponseBuilder(HttpStatus.BAD_REQUEST)
@@ -42,15 +32,12 @@ public HttpResponseMessage run(
4232
}
4333
String accessToken = authHeader.substring("Bearer ".length());
4434

45-
// 2. 토큰에서 사용자 정보 추출
4635
Long userId = jwtTokenService.extractUserId(accessToken);
4736
String email = jwtTokenService.extractEmail(accessToken);
4837

49-
// 3. 사용자 조회
5038
User user = userService.findByEmail(email)
5139
.orElseThrow(() -> new UserNotFoundException(email));
5240

53-
// 4. 응답 DTO 생성
5441
UserResponse response = UserResponse.builder()
5542
.id(userId)
5643
.email(email)
@@ -61,7 +48,6 @@ public HttpResponseMessage run(
6148
.totalLlmElecEstimate(user.getTotalLlmElecEstimate())
6249
.build();
6350

64-
// 5. 응답 반환 (JSON + 200 OK)
6551
return request.createResponseBuilder(HttpStatus.OK)
6652
.header("Content-Type", "application/json")
6753
.body(response)

src/main/java/org/inhahackers/optmo_user_be/function/OAuthUserFunction.java

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package org.inhahackers.optmo_user_be.function;
22

33
import com.microsoft.azure.functions.*;
4-
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
5-
import com.microsoft.azure.functions.annotation.FunctionName;
6-
import com.microsoft.azure.functions.annotation.HttpTrigger;
74
import lombok.RequiredArgsConstructor;
85
import org.inhahackers.optmo_user_be.dto.OAuthUserInfo;
96
import org.inhahackers.optmo_user_be.dto.UserResponse;
@@ -14,29 +11,22 @@
1411
import org.inhahackers.optmo_user_be.service.JwtTokenService;
1512
import org.inhahackers.optmo_user_be.service.OAuthTokenService;
1613
import org.inhahackers.optmo_user_be.service.UserService;
14+
import org.springframework.stereotype.Component;
1715

1816
import java.util.Optional;
17+
import java.util.function.Function;
1918

19+
@Component("oauthUserFunction")
2020
@RequiredArgsConstructor
21-
public class OAuthUserFunction {
21+
public class OAuthUserFunction implements Function<HttpRequestMessage<Optional<String>>, HttpResponseMessage> {
2222

2323
private final UserService userService;
2424
private final OAuthTokenService tokenService;
2525
private final JwtTokenService jwtTokenService;
2626

27-
@FunctionName("oauthUserFunction")
28-
public HttpResponseMessage run(
29-
@HttpTrigger(
30-
name = "oauthuser",
31-
methods = {HttpMethod.GET, HttpMethod.POST},
32-
authLevel = AuthorizationLevel.ANONYMOUS)
33-
HttpRequestMessage<Void> request,
34-
final ExecutionContext context) {
35-
36-
context.getLogger().info("oauthUserFunction called");
37-
27+
@Override
28+
public HttpResponseMessage apply(HttpRequestMessage<Optional<String>> request) {
3829
try {
39-
// 1. Authorization 헤더 추출 및 검증
4030
String authHeader = request.getHeaders().get("Authorization");
4131
if (authHeader == null || !authHeader.startsWith("Bearer ")) {
4232
return request.createResponseBuilder(HttpStatus.BAD_REQUEST)
@@ -45,7 +35,6 @@ public HttpResponseMessage run(
4535
}
4636
String accessToken = authHeader.substring("Bearer ".length());
4737

48-
// 2. provider 쿼리 파라미터 추출 및 검증
4938
String providerStr = request.getQueryParameters().get("provider");
5039
if (providerStr == null || providerStr.isBlank()) {
5140
return request.createResponseBuilder(HttpStatus.BAD_REQUEST)
@@ -62,15 +51,12 @@ public HttpResponseMessage run(
6251
.build();
6352
}
6453

65-
// 3. 토큰 검증 및 사용자 정보 조회
6654
OAuthUserInfo userInfo = tokenService.verifyAndGetUserInfo(accessToken, provider);
6755
User user = userService.findOrCreateUser(userInfo.toUserOAuthRequest());
6856

69-
// 4. JWT 토큰 생성
7057
String jwtToken = jwtTokenService.generateToken(
7158
user.getId(), user.getEmail(), user.getRole().name());
7259

73-
// 5. 응답 DTO 생성
7460
UserResponse response = UserResponse.builder()
7561
.id(user.getId())
7662
.email(user.getEmail())
@@ -81,7 +67,6 @@ public HttpResponseMessage run(
8167
.totalUseElecEstimate(user.getTotalUseElecEstimate())
8268
.build();
8369

84-
// 6. 응답 반환 (헤더에 Authorization 포함)
8570
return request.createResponseBuilder(HttpStatus.OK)
8671
.header("Content-Type", "application/json")
8772
.header("Authorization", "Bearer " + jwtToken)

0 commit comments

Comments
 (0)