Java Code Examples for cn.hutool.extra.servlet.ServletUtil#getClientIP()
The following examples show how to use
cn.hutool.extra.servlet.ServletUtil#getClientIP() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LoginStatusDTO.java From zuihou-admin-boot with Apache License 2.0 | 6 votes |
private LoginStatusDTO setInfo() { RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); if (requestAttributes == null) { return this; } HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest(); if (request == null) { return this; } String ua = StrUtil.sub(request.getHeader("user-agent"), 0, 500); String ip = ServletUtil.getClientIP(request); String location = AddressUtil.getRegion(ip); this.ua = ua; this.ip = ip; this.location = location; return this; }
Example 2
Source File: LoginStatusDTO.java From zuihou-admin-cloud with Apache License 2.0 | 6 votes |
private LoginStatusDTO setInfo() { RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); if (requestAttributes == null) { return this; } HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest(); if (request == null) { return this; } String ua = StrUtil.sub(request.getHeader("user-agent"), 0, 500); String ip = ServletUtil.getClientIP(request); String location = AddressUtil.getRegion(ip); this.ua = ua; this.ip = ip; this.location = location; return this; }
Example 3
Source File: LogFilter.java From halo with GNU General Public License v3.0 | 6 votes |
@Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { String remoteAddr = ServletUtil.getClientIP(request); log.debug(""); log.debug("Starting url: [{}], method: [{}], ip: [{}]", request.getRequestURL(), request.getMethod(), remoteAddr); // Set start time long startTime = System.currentTimeMillis(); // Do filter filterChain.doFilter(request, response); log.debug("Ending url: [{}], method: [{}], ip: [{}], status: [{}], usage: [{}] ms", request.getRequestURL(), request.getMethod(), remoteAddr, response.getStatus(), System.currentTimeMillis() - startTime); log.debug(""); }
Example 4
Source File: SysLogAspect.java From pre with GNU General Public License v3.0 | 5 votes |
/*** * 拦截控制层的操作日志 * @param joinPoint * @return * @throws Throwable */ @Before(value = "sysLogAspect()") public void recordLog(JoinPoint joinPoint) throws Throwable { SysLog sysLog = new SysLog(); //将当前实体保存到threadLocal sysLogThreadLocal.set(sysLog); // 开始时间 long beginTime = Instant.now().toEpochMilli(); HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest(); PreSecurityUser securityUser = SecurityUtil.getUser(); sysLog.setUserName(securityUser.getUsername()); sysLog.setActionUrl(URLUtil.getPath(request.getRequestURI())); sysLog.setStartTime(LocalDateTime.now()); String ip = ServletUtil.getClientIP(request); sysLog.setIp(ip); sysLog.setLocation(IPUtil.getCityInfo(ip)); sysLog.setRequestMethod(request.getMethod()); String uaStr = request.getHeader("user-agent"); sysLog.setBrowser(UserAgentUtil.parse(uaStr).getBrowser().toString()); sysLog.setOs(UserAgentUtil.parse(uaStr).getOs().toString()); //访问目标方法的参数 可动态改变参数值 Object[] args = joinPoint.getArgs(); //获取执行的方法名 sysLog.setActionMethod(joinPoint.getSignature().getName()); // 类名 sysLog.setClassPath(joinPoint.getTarget().getClass().getName()); sysLog.setActionMethod(joinPoint.getSignature().getName()); sysLog.setFinishTime(LocalDateTime.now()); // 参数 sysLog.setParams(Arrays.toString(args)); sysLog.setDescription(LogUtil.getControllerMethodDescription(joinPoint)); long endTime = Instant.now().toEpochMilli(); sysLog.setConsumingTime(endTime - beginTime); }
Example 5
Source File: IndexController.java From mayday with GNU General Public License v3.0 | 5 votes |
/** * 检测同一IP十分钟以内重复访问同一篇文章 * * @param request * @param id * 文章id * @return */ public boolean checkRepeatIp(HttpServletRequest request, int id) { String value = ServletUtil.getClientIP(request) + ":" + id; Integer count = cache.hget("hits:frequency", value); if (count != null && count > 0) { return true; } cache.hset("hits:frequency", value, 1, MaydayConst.IP_REPEAT_TIME); return false; }
Example 6
Source File: ServerWebSocketInterceptor.java From Jpom with MIT License | 4 votes |
@Override public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception { if (request instanceof ServletServerHttpRequest) { ServletServerHttpRequest serverHttpRequest = (ServletServerHttpRequest) request; HttpServletRequest httpServletRequest = serverHttpRequest.getServletRequest(); // 判断用户 String userId = httpServletRequest.getParameter("userId"); UserService userService = SpringUtil.getBean(UserService.class); RoleService roleService = SpringUtil.getBean(RoleService.class); UserModel userModel = userService.checkUser(userId); if (userModel == null) { return false; } String nodeId = httpServletRequest.getParameter("nodeId"); if (!JpomApplication.SYSTEM_ID.equals(nodeId)) { NodeService nodeService = SpringUtil.getBean(NodeService.class); NodeModel nodeModel = nodeService.getItem(nodeId); if (nodeModel == null || roleService.errorDynamicPermission(userModel, ClassFeature.NODE, nodeId)) { return false; } // attributes.put("nodeInfo", nodeModel); } // 判断拦截类型 String type = httpServletRequest.getParameter("type"); HandlerType handlerType; try { handlerType = HandlerType.valueOf(type); } catch (Exception e) { throw new JpomRuntimeException("type 错误:" + type); } switch (handlerType) { case console: //控制台 String projectId = httpServletRequest.getParameter("projectId"); // 判断权限 if (roleService.errorDynamicPermission(userModel, ClassFeature.PROJECT, projectId)) { return false; } attributes.put("projectId", projectId); break; case script: // 脚本模板 String scriptId = httpServletRequest.getParameter("scriptId"); if (roleService.errorDynamicPermission(userModel, ClassFeature.SCRIPT, scriptId)) { return false; } attributes.put("scriptId", scriptId); break; case tomcat: String tomcatId = httpServletRequest.getParameter("tomcatId"); if (roleService.errorDynamicPermission(userModel, ClassFeature.TOMCAT, tomcatId)) { return false; } attributes.put("tomcatId", tomcatId); break; case ssh: String sshId = httpServletRequest.getParameter("sshId"); if (roleService.errorDynamicPermission(userModel, ClassFeature.SSH, sshId)) { return false; } SshService bean = SpringUtil.getBean(SshService.class); SshModel sshModel = bean.getItem(sshId); if (sshModel == null) { return false; } Map<String, String[]> parameterMap = httpServletRequest.getParameterMap(); attributes.put("parameterMap", parameterMap); attributes.put("sshItem", sshModel); break; default: return false; } // String ip = ServletUtil.getClientIP(httpServletRequest); attributes.put("ip", ip); // String userAgent = ServletUtil.getHeaderIgnoreCase(httpServletRequest, HttpHeaders.USER_AGENT); attributes.put(HttpHeaders.USER_AGENT, userAgent); attributes.put("userInfo", userModel); return true; } return false; }
Example 7
Source File: OperateLogController.java From Jpom with MIT License | 4 votes |
@Override public void before(JoinPoint joinPoint) { Signature signature = joinPoint.getSignature(); if (signature instanceof MethodSignature) { MethodSignature methodSignature = (MethodSignature) signature; Method method = methodSignature.getMethod(); UserOperateLogV1.OptType optType = null; OptLog optLog = method.getAnnotation(OptLog.class); if (optLog != null) { optType = optLog.value(); } if (optType != null) { CacheInfo cacheInfo = new CacheInfo(); cacheInfo.optType = optType; ServletRequestAttributes servletRequestAttributes = BaseServerController.getRequestAttributes(); HttpServletRequest request = servletRequestAttributes.getRequest(); if (optType == UserOperateLogV1.OptType.Login) { // 获取登录人的信息 String userName = request.getParameter("userName"); UserService userService = SpringUtil.getBean(UserService.class); cacheInfo.userModel = userService.getItem(userName); } // 获取ip地址 cacheInfo.ip = ServletUtil.getClientIP(request); // 获取节点 cacheInfo.nodeModel = (NodeModel) request.getAttribute("node"); // cacheInfo.dataId = request.getParameter("id"); // cacheInfo.userAgent = ServletUtil.getHeaderIgnoreCase(request, HttpHeaders.USER_AGENT); // Map<String, String[]> map = ObjectUtil.clone(request.getParameterMap()); // 过滤密码字段 Set<Map.Entry<String, String[]>> entries = map.entrySet(); for (Map.Entry<String, String[]> entry : entries) { String key = entry.getKey(); if (StrUtil.containsAnyIgnoreCase(key, "pwd", "password")) { entry.setValue(new String[]{"***"}); } } cacheInfo.setReqData(JSONObject.toJSONString(map)); CACHE_INFO_THREAD_LOCAL.set(cacheInfo); } } }
Example 8
Source File: ActionAspect.java From kvf-admin with MIT License | 4 votes |
private void saveActionLog(JoinPoint joinPoint, long time, boolean isAround) { User user = (User) SecurityUtils.getSubject().getPrincipal(); if (user == null) { return; } MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); String methodName = method.getName(); if (isAround && "logout".equals(methodName)) { return; } com.kalvin.kvf.modules.sys.entity.Log actionLog = new com.kalvin.kvf.modules.sys.entity.Log(); Log action = method.getAnnotation(Log.class); if(action != null){ // 注解上的描述 if (StrUtil.isNotBlank(action.value())) { actionLog.setOperation(action.value()); } else { // 如果注解上的描述为空,则默认使用方法名 actionLog.setOperation(methodName); } } // 获取request HttpServletRequest request = HttpServletContextKit.getHttpServletRequest(); // 设置IP地址 String clientIP = ServletUtil.getClientIP(request); if ("0:0:0:0:0:0:0:1".equals(clientIP)) { clientIP = "127.0.0.1"; } actionLog.setIp(clientIP); actionLog.setForwardAction(request.getRequestURI()); // 设置浏览器和设备系统 String header = request.getHeader("User-Agent"); Browser browser = UserAgentUtil.parse(header).getBrowser(); Platform platform = UserAgentUtil.parse(header).getPlatform(); String browserName = browser.getName() + " " + browser.getVersion(header); String os = platform.getName(); actionLog.setBrowser(browserName); actionLog.setOs(os); // 设置用户信息 actionLog.setUserId(user.getId()); actionLog.setUsername(user.getUsername()); actionLog.setTime(time); // 保存系统日志 logService.save(actionLog); }