net.guerlab.cloud.web.webmvc.support.WebMvcRequestIpHandlerMethodArgumentResolver Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2018-2025 guerlab.net and other contributors.
*
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.gnu.org/licenses/lgpl-3.0.html
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.guerlab.cloud.web.webmvc.support;
import jakarta.annotation.Nullable;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
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 net.guerlab.cloud.commons.ip.IpUtils;
import net.guerlab.cloud.web.core.annotation.RequestIp;
/**
* 请求IP参数处理.
*
* @author guer
*/
@Slf4j
public class WebMvcRequestIpHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {
@Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.getParameterAnnotation(RequestIp.class) != null;
}
@Nullable
@Override
public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) {
Object nativeRequest = webRequest.getNativeRequest();
log.debug("nativeRequest: {}", nativeRequest);
String ip = IpUtils.getIp(nativeRequest);
log.debug("get ip in nativeRequest: {}", ip);
return ip;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy