shiro如何获取访问ip
时间: 2024-05-08 14:19:57 浏览: 143
Shiro可以通过获取HttpServletRequest中的请求IP地址来获取访问IP。可以使用如下代码来实现获取IP地址:
```java
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String ipAddress = request.getHeader("X-Forwarded-For");
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
}
```
这个代码可以从HTTP请求头中获取客户端IP地址,包括使用代理服务器访问时的地址。
阅读全文