-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5844c69
commit 140a3eb
Showing
3 changed files
with
60 additions
and
1 deletion.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
src/main/java/com/_8attery/seesaw/annotation/ClientIp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com._8attery.seesaw.annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.PARAMETER) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface ClientIp { | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/com/_8attery/seesaw/config/resolver/ClientIpResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com._8attery.seesaw.config.resolver; | ||
|
||
import com._8attery.seesaw.annotation.ClientIp; | ||
import org.springframework.core.MethodParameter; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.bind.support.WebDataBinderFactory; | ||
import org.springframework.web.context.request.NativeWebRequest; | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||
import org.springframework.web.method.support.ModelAndViewContainer; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
@Component | ||
public class ClientIpResolver implements HandlerMethodArgumentResolver { | ||
private static final String[] IP_HEADER_CANDIDATES = { | ||
"X-Forwarded-For", | ||
"Proxy-Client-IP", | ||
"WL-Proxy-Client-IP", | ||
"HTTP_X_FORWARDED_FOR", | ||
"HTTP_X_FORWARDED", | ||
"HTTP_X_CLUSTER_CLIENT_IP", | ||
"HTTP_CLIENT_IP", | ||
"HTTP_FORWARDED_FOR", | ||
"HTTP_FORWARDED", | ||
"HTTP_VIA", | ||
"REMOTE_ADDR" | ||
}; | ||
|
||
@Override | ||
public boolean supportsParameter(MethodParameter parameter) { | ||
return parameter.hasParameterAnnotation(ClientIp.class); | ||
} | ||
|
||
@Override | ||
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { | ||
HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest(); | ||
|
||
for (String header : IP_HEADER_CANDIDATES) { | ||
String ip = request.getHeader(header); | ||
|
||
if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) { | ||
return ip; | ||
} | ||
} | ||
|
||
return request.getRemoteAddr(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters