μ΄μ μ νμ¬μμ λ€λ₯Έ νλ‘μ νΈ μμ€λ₯Ό 보λ€κ° http ν€λμμ λ³΄λΈ μ 보λ₯Ό controller λ¨μμ λ°λ‘ vo λ‘ λ§€νν΄μ£Όλ μ½λλ₯Ό λ°κ²¬νλ€.
λΉμμ μ΄κ±° κ½€ μ μ©νκ² μΈ μ μκ² λ€. νκ³ μ§λκ°κΈ°λ§ νμλλ°, μ΄λ²μ κΈ°νκ° μκ²Όλ€.
λͺ©ν
- ν΄λΌμ΄μΈνΈμ IP λ₯Ό 컨νΈλ‘€λ¬ λ¨μμ λ°λ‘ λ°μμ μ²λ¦¬νκ³ μ ν¨.
1. ClientInfo μμ±
1
2
3
4
5
6
|
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ClientInfo {
private String clientIp;
}
|
- λ°μΈλ©λ κ°μ²΄λ₯Ό μμ±
2. ClientResolver μ΄λ
Έν
μ΄μ
μμ±
1
2
3
4
|
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface ClientResolver {
}
|
3. ClientHandlerMethodArgumentResolver μμ±
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
public class ClientHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {
@Override
public boolean supportsParameter(final MethodParameter methodParameter) {
return methodParameter.getParameterType().equals(ClientInfo.class);
}
@Override
public Object resolveArgument(final MethodParameter methodParameter,
final ModelAndViewContainer modelAndViewContainer,
final NativeWebRequest nativeWebRequest,
final WebDataBinderFactory webDataBinderFactory) throws Exception {
ClientInfo clientInfo = new ClientInfo(clientIp(nativeWebRequest));
return clientInfo;
}
private String clientIp(final NativeWebRequest request) {
String clientIp = request.getHeader("X-Forwarded-For");
if (StringUtils.isEmpty(clientIp) || "unknown".equalsIgnoreCase(clientIp)) {
clientIp = request.getHeader("Proxy-Client-IP");
}
if (StringUtils.isEmpty(clientIp) || "unknown".equalsIgnoreCase(clientIp)) {
clientIp = request.getHeader("WL-Proxy-Client-IP");
}
if (StringUtils.isEmpty(clientIp) || "unknown".equalsIgnoreCase(clientIp)) {
clientIp = request.getHeader("HTTP_CLIENT_IP");
}
if (StringUtils.isEmpty(clientIp) || "unknown".equalsIgnoreCase(clientIp)) {
clientIp = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (StringUtils.isEmpty(clientIp) || "unknown".equalsIgnoreCase(clientIp)) {
HttpServletRequest nativeRequest = (HttpServletRequest) request.getNativeRequest();
clientIp = nativeRequest.getRemoteAddr();
}
return clientIp;
}
}
|
- supportsParameter λ νμ¬ νλΌλ―Έν°λ₯Ό resolverμ΄ μ§μνλμ§μ λν boolean
- resolveArgument λ λ°μΈλ©ν κ°μ²΄
4. resolver λ±λ‘
1
2
3
4
5
6
7
8
9
10
|
@Configuration
public class WebMvcCustomConfiguration extends WebMvcConfigurerAdapter {
...
@Override
public void addArgumentResolvers(final List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(new ClientHandlerMethodArgumentResolver());
}
}
|
- μ€νλ§4.x λ₯Ό μ¬μ©νκ³ μμ΄μ
WebMvcConfigurerAdapter
λ₯Ό μμλ°μ μ¬μ©ν κ² κ°μ§λ§,
spring boot μμλ WebMvcConfigurer μ ꡬνν΄μ£Όλ©΄ λλ κ² κ°λ€.
1
2
3
4
5
6
7
8
9
10
11
|
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Autowired
private ClientHandlerMethodArgumentResolver clientHandlerMethodArgumentResolver;
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(clientHandlerMethodArgumentResolver);
}
}
|
5. Controller μμ±
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
@Controller
public class IntroCreationController {
private final IntroCreationService introCreationService;
@Autowired
public IntroCreationController(IntroCreationService introCreationService) {
this.introCreationService = introCreationService;
}
@RequestMapping(value = "/api/save/intro", method = RequestMethod.POST)
public ApiResponse createIntro(@RequestBody IntroCreationRequest request, @ClientResolver ClientInfo clientInfo) {
ApiResponse apiResponse;
try {
AdminIntroData intro = introCreationService.createIntro(request, clientInfo);
apiResponse = ApiResponse.success(intro);
} catch (Exception e) {
apiResponse = ApiResponse.fail(e.getMessage());
}
return apiResponse;
}
}
|
- 컨νΈλ‘€λ¬ 맀μλμ νλΌλ―Έν°μ 컀μ€ν
μ΄λ
Έν
μ΄μ
μ μ¬μ©νλ©΄ λλ€.