springboot如何獲取請(qǐng)求者的ip地址
在Spring框架中,可以使用攔截器(Interceptor)來監(jiān)聽每個(gè)控制器(Controller)的請(qǐng)求,并記錄請(qǐng)求者的IP地址。
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; public class IpLoggingInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // 在請(qǐng)求處理之前調(diào)用,可以記錄IP地址等信息 String clientIpAddress = getClientIpAddress(request); System.out.println("IP地址:" + clientIpAddress); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // 在請(qǐng)求處理之后調(diào)用,但在視圖渲染之前 } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // 在整個(gè)請(qǐng)求完成后調(diào)用,可以進(jìn)行一些清理工作 } private String getClientIpAddress(HttpServletRequest request) { String ipAddress = request.getHeader("X-Forwarded-For"); if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) { ipAddress = request.getHeader("Proxy-Client-IP"); } if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) { ipAddress = request.getHeader("WL-Proxy-Client-IP"); } if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) { ipAddress = request.getRemoteAddr(); } return ipAddress; } }
上述代碼中的 IpLoggingInterceptor 類實(shí)現(xiàn)了 HandlerInterceptor 接口,其中的 preHandle 方法在請(qǐng)求處理之前被調(diào)用。在該方法中,我們獲取了請(qǐng)求者的IP地址,并進(jìn)行了簡(jiǎn)單的打印??梢愿鶕?jù)需要,將這些信息記錄到日志文件或其他存儲(chǔ)設(shè)備中。
接下來,需要將這個(gè)攔截器注冊(cè)到Spring應(yīng)用中。在Spring Boot項(xiàng)目中,可以使用WebMvcConfigurer來注冊(cè)攔截器。
import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new IpLoggingInterceptor()); } }
到此這篇關(guān)于springboot如何獲取請(qǐng)求者的ip地址的文章就介紹到這了,更多相關(guān)springboot請(qǐng)求者的ip地址內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot自動(dòng)配置原理,你真的懂嗎?(簡(jiǎn)單易懂)
這篇文章主要介紹了SpringBoot自動(dòng)配置原理,你真的懂嗎?本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-05-05Java使用觀察者模式實(shí)現(xiàn)氣象局高溫預(yù)警功能示例
這篇文章主要介紹了Java使用觀察者模式實(shí)現(xiàn)氣象局高溫預(yù)警功能,結(jié)合完整實(shí)例形式分析了java觀察者模式實(shí)現(xiàn)氣象局高溫預(yù)警的相關(guān)接口定義、使用、功能操作技巧,并總結(jié)了其設(shè)計(jì)原則與適用場(chǎng)合,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2018-04-04新建springboot項(xiàng)目時(shí),entityManagerFactory報(bào)錯(cuò)的解決
這篇文章主要介紹了新建springboot項(xiàng)目時(shí),entityManagerFactory報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01httpclient getPoolEntryBlocking連接池方法源碼解讀
這篇文章主要為大家介紹了httpclient getPoolEntryBlocking連接池方法源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11java使用RestTemplate封裝post請(qǐng)求方式
這篇文章主要介紹了java使用RestTemplate封裝post請(qǐng)求方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10簡(jiǎn)單了解Java方法的定義和使用實(shí)現(xiàn)詳解
這篇文章主要介紹了簡(jiǎn)單了解Java方法的定義和使用實(shí)現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12