Java Code Examples for cn.hutool.core.util.StrUtil#containsAnyIgnoreCase()

The following examples show how to use cn.hutool.core.util.StrUtil#containsAnyIgnoreCase() . 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: ServletUtils.java    From datax-web with MIT License 6 votes vote down vote up
/**
 * 是否是Ajax异步请求
 *
 * @param request
 */
public static boolean isAjaxRequest(HttpServletRequest request) {

    String accept = request.getHeader("accept");
    if (accept != null && accept.indexOf("application/json") != -1) {
        return true;
    }

    String xRequestedWith = request.getHeader("X-Requested-With");
    if (xRequestedWith != null && xRequestedWith.indexOf("XMLHttpRequest") != -1) {
        return true;
    }

    String uri = request.getRequestURI();
    if (StrUtil.containsAnyIgnoreCase(uri, ".json", ".xml")) {
        return true;
    }

    String ajax = request.getParameter("__ajax");
    if (StrUtil.containsAnyIgnoreCase(ajax, "json", "xml")) {
        return true;
    }

    return false;
}
 
Example 2
Source File: ValidateCodeFilter.java    From spring-microservice-exam with MIT License 6 votes vote down vote up
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    // 当前请求
    ServerHttpRequest request = exchange.getRequest();
    // 请求的URI
    URI uri = request.getURI();
    if (HttpMethod.POST.matches(request.getMethodValue())
            && StrUtil.containsAnyIgnoreCase(uri.getPath(), GatewayConstant.OAUTH_TOKEN_URL, GatewayConstant.REGISTER, GatewayConstant.MOBILE_TOKEN_URL)) {
        String grantType = request.getQueryParams().getFirst(GatewayConstant.GRANT_TYPE);
        // 授权类型为密码模式、手机号、注册才校验验证码
        if (CommonConstant.GRANT_TYPE_PASSWORD.equals(grantType) || CommonConstant.GRANT_TYPE_MOBILE.equals(grantType) || StrUtil.containsAnyIgnoreCase(uri.getPath(), GatewayConstant.REGISTER)) {
            // 校验验证码
            checkCode(request, getLoginType(grantType));
        }
    }
    return chain.filter(exchange);
}
 
Example 3
Source File: Wrapper.java    From yue-library with Apache License 2.0 6 votes vote down vote up
/**
 * 包装字段名<br>
 * 有时字段与SQL的某些关键字冲突,导致SQL出错,因此需要将字段名用单引号或者反引号包装起来,避免冲突
 * @param field 字段名
 * @return 包装后的字段名
 */
public String wrap(String field){
	if(preWrapQuote == null || sufWrapQuote == null || StrUtil.isBlank(field)) {
		return field;
	}
	
	//如果已经包含包装的引号,返回原字符
	if(StrUtil.isSurround(field, preWrapQuote, sufWrapQuote)){
		return field;
	}
	
	//如果字段中包含通配符或者括号(字段通配符或者函数),不做包装
	if(StrUtil.containsAnyIgnoreCase(field, "*", "(", " ", " as ")) {
		return field;
	}
	
	//对于Oracle这类数据库,表名中包含用户名需要单独拆分包装
	if(field.contains(StrUtil.DOT)){
		final Collection<String> target = CollectionUtil.filter(StrUtil.split(field, StrUtil.C_DOT), (Editor<String>) t -> StrUtil.format("{}{}{}", preWrapQuote, t, sufWrapQuote));
		return CollectionUtil.join(target, StrUtil.DOT);
	}
	
	return StrUtil.format("{}{}{}", preWrapQuote, field, sufWrapQuote);
}
 
Example 4
Source File: SshHandler.java    From Jpom with MIT License 6 votes vote down vote up
private boolean checkCommand(HandlerItem handlerItem, String data) throws Exception {
    UserModel userInfo = (UserModel) handlerItem.session.getAttributes().get("userInfo");
    if (!userInfo.isDemoUser()) {
        // demo user判断
        return false;
    }
    if (data == null) {
        data = handlerItem.dataToDst.toString();
    }
    if (StrUtil.containsAnyIgnoreCase(data, "rm ")) {
        //
        handlerItem.dataToDst.setLength(0);
        this.call(handlerItem.session, "没有权限");
        this.call(handlerItem.session, "\\u0016");
        this.call(handlerItem.session, StrUtil.CR);
        return true;
    }
    return false;
}
 
Example 5
Source File: PreRequestLogFilter.java    From Taroco with Apache License 2.0 6 votes vote down vote up
/**
 * 添加系统日志
 *
 * @param request 请求对象
 * @param requestContext     RequestContext
 */
private void addLog(HttpServletRequest request, RequestContext requestContext) {
    final SysLog log = new SysLog();
    log.setCreateTime(new Date());
    log.setRemoteAddr(request.getRemoteAddr());
    log.setRequestUri(request.getRequestURI());
    log.setMethod(request.getMethod());

    if (StrUtil.containsAnyIgnoreCase(request.getRequestURI(),
            SecurityConstants.OAUTH_TOKEN_URL)) {
        // 记录登录日志
        log.setType(LogType.Login.name());
        log.setTitle(LogType.Login.name());
        log.setParams(queryParam(request));
        log.setCreateBy(request.getParameter("username"));
        logService.add(log);
    } else {
        if (!HttpMethod.GET.matches(request.getMethod())) {
            // 记录操作日志
            log.setType(LogType.Operation.name());
            log.setTitle(LogType.Operation.name());
            log.setCreateBy(requestContext.getZuulRequestHeaders().get(SecurityConstants.USER_HEADER));
            logService.add(log);
        }
    }
}
 
Example 6
Source File: ServletUtils.java    From RuoYi with Apache License 2.0 6 votes vote down vote up
/**
 * 是否是Ajax异步请求
 *
 * @param request
 */
public static boolean isAjaxRequest(HttpServletRequest request) {

    String accept = request.getHeader("accept");
    if (accept != null && accept.contains("application/json")) {
        return true;
    }

    String xRequestedWith = request.getHeader("X-Requested-With");
    if (xRequestedWith != null && xRequestedWith.contains("XMLHttpRequest")) {
        return true;
    }

    String uri = request.getRequestURI();
    if (StrUtil.containsAnyIgnoreCase(uri, ".json" , ".xml")) {
        return true;
    }

    String ajax = request.getParameter("__ajax");
    return StrUtil.containsAnyIgnoreCase(ajax, "json", "xml");

}
 
Example 7
Source File: PasswordDecoderFilter.java    From albedo with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
	// 不是登录请求,直接向下执行
	if (!StrUtil.containsAnyIgnoreCase(request.getRequestURI(), applicationProperties.getAdminPath(SecurityConstants.AUTHENTICATE_URL))) {
		filterChain.doFilter(request, response);
		return;
	}

	String queryParam = request.getQueryString();
	Map<String, String> paramMap = HttpUtil.decodeParamMap(queryParam, CharsetUtil.CHARSET_UTF_8);

	String password = request.getParameter(PASSWORD);
	if (StrUtil.isNotBlank(password)) {
		try {
			password = decryptAes(password, applicationProperties.getSecurity().getEncodeKey());
		} catch (Exception e) {
			log.error("密码解密失败:{}", password);
			throw e;
		}
		paramMap.put(PASSWORD, password.trim());
	}
	ParameterRequestWrapper requestWrapper = new ParameterRequestWrapper(request, paramMap);
	filterChain.doFilter(requestWrapper, response);
}
 
Example 8
Source File: PasswordDecoderFilter.java    From smaker with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public GatewayFilter apply(Object config) {
	return (exchange, chain) -> {
		ServerHttpRequest request = exchange.getRequest();

		// 不是登录请求,直接向下执行
		if (!StrUtil.containsAnyIgnoreCase(request.getURI().getPath(), SecurityConstants.OAUTH_TOKEN_URL)) {
			return chain.filter(exchange);
		}

		URI uri = exchange.getRequest().getURI();
		String queryParam = uri.getRawQuery();
		Map<String, String> paramMap = HttpUtil.decodeParamMap(queryParam, CharsetUtil.UTF_8);

		String password = paramMap.get(PASSWORD);
		if (StrUtil.isNotBlank(password)) {
			try {
				password = decryptAES(password, encodeKey);
			} catch (Exception e) {
				log.error("密码解密失败:{}", password);
				return Mono.error(e);
			}
			paramMap.put(PASSWORD, password.trim());
		}

		URI newUri = UriComponentsBuilder.fromUri(uri)
			.replaceQuery(HttpUtil.toParams(paramMap))
			.build(true)
			.toUri();

		ServerHttpRequest newRequest = exchange.getRequest().mutate().uri(newUri).build();
		return chain.filter(exchange.mutate().request(newRequest).build());
	};
}
 
Example 9
Source File: DecodePasswordFilter.java    From spring-microservice-exam with MIT License 5 votes vote down vote up
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    // 当前请求
    ServerHttpRequest request = exchange.getRequest();
    // 请求的URI
    URI uri = request.getURI();
    // 获取token的请求
    if (HttpMethod.POST.matches(request.getMethodValue()) && StrUtil.containsAnyIgnoreCase(uri.getPath(), GatewayConstant.OAUTH_TOKEN_URL, GatewayConstant.REGISTER,
            GatewayConstant.MOBILE_TOKEN_URL)) {
        String grantType = request.getQueryParams().getFirst(GatewayConstant.GRANT_TYPE);
        // 授权类型为密码模式则解密
        if (CommonConstant.GRANT_TYPE_PASSWORD.equals(grantType) || StrUtil.containsAnyIgnoreCase(uri.getPath(), GatewayConstant.REGISTER)) {
            String credential = request.getQueryParams().getFirst(CREDENTIAL);
            if (StringUtils.isNotBlank(credential)) {
                try {
                    // 开始解密
                    credential = AesUtil.decryptAES(credential, sysProperties.getKey());
                    credential = credential.trim();
                } catch (Exception e) {
                    log.error("credential decrypt fail:{}", credential);
                }
                URI newUri = UriComponentsBuilder.fromUri(uri)
                        // 替换password字段
                        .replaceQueryParam(PASSWORD, credential)
                        // 替换credential字段
                        .replaceQueryParam(CREDENTIAL, credential)
                        .build(true).toUri();
                request = request.mutate().uri(newUri).build();
                return chain.filter(exchange.mutate().request(request).build());
            }
        }
    }
    return chain.filter(exchange);
}
 
Example 10
Source File: ValidateCodeGatewayFilter.java    From sophia_scaffolding with Apache License 2.0 5 votes vote down vote up
@Override
public GatewayFilter apply(Object config) {
	return (exchange, chain) -> {
		ServerHttpRequest request = exchange.getRequest();

		// 不是登录请求,直接向下执行
		if (!StrUtil.containsAnyIgnoreCase(request.getURI().getPath()
			, OAUTH_TOKEN_URL)) {
			return chain.filter(exchange);
		}

		// 刷新token,直接向下执行
		String grantType = request.getQueryParams().getFirst("grant_type");
		if (StrUtil.equals(REFRESH_TOKEN, grantType)) {
			return chain.filter(exchange);
		}

		// 终端设置不校验, 直接向下执行
		try {
			//校验验证码
			checkCode(request);
		} catch (Exception e) {
			ServerHttpResponse response = exchange.getResponse();
			response.setStatusCode(HttpStatus.PRECONDITION_REQUIRED);
			try {
				Map map= new HashMap<>();
				map.put("code",-1);
				map.put("msg","失败");
				return response.writeWith(Mono.just(response.bufferFactory()
					.wrap(objectMapper.writeValueAsBytes(map))));
			} catch (JsonProcessingException e1) {
				log.error("对象输出异常", e1);
			}
		}

		return chain.filter(exchange);
	};
}
 
Example 11
Source File: ValidateCodeGatewayFilter.java    From sophia_scaffolding with Apache License 2.0 5 votes vote down vote up
@Override
public GatewayFilter apply(Object config) {
	return (exchange, chain) -> {
		ServerHttpRequest request = exchange.getRequest();

		// 不是登录请求,直接向下执行
		if (!StrUtil.containsAnyIgnoreCase(request.getURI().getPath()
			, OAUTH_TOKEN_URL)) {
			return chain.filter(exchange);
		}

		// 刷新token,直接向下执行
		String grantType = request.getQueryParams().getFirst("grant_type");
		if (StrUtil.equals(REFRESH_TOKEN, grantType)) {
			return chain.filter(exchange);
		}

		// 终端设置不校验, 直接向下执行
		try {
			//校验验证码
			checkCode(request);
		} catch (Exception e) {
			ServerHttpResponse response = exchange.getResponse();
			response.setStatusCode(HttpStatus.PRECONDITION_REQUIRED);
			try {
				Map map= new HashMap<>();
				map.put("code",-1);
				map.put("msg","失败");
				return response.writeWith(Mono.just(response.bufferFactory()
					.wrap(objectMapper.writeValueAsBytes(map))));
			} catch (JsonProcessingException e1) {
				log.error("对象输出异常", e1);
			}
		}

		return chain.filter(exchange);
	};
}
 
Example 12
Source File: ValidateCodeFilter.java    From Taroco with Apache License 2.0 5 votes vote down vote up
/**
 * 是否校验验证码
 * 1. 判断验证码开关是否开启
 * 2. 判断请求是否登录请求
 * 3. 判断终端是否支持
 *
 * @return true/false
 */
@Override
public boolean shouldFilter() {
    final HttpServletRequest request = RequestContext.getCurrentContext().getRequest();

    if (RequestMethod.OPTIONS.toString().equalsIgnoreCase(request.getMethod())) {
        return false;
    }

    // 对指定的请求方法 进行验证码的校验
    return StrUtil.containsAnyIgnoreCase(request.getRequestURI(), SecurityConstants.OAUTH_TOKEN_URL);
}
 
Example 13
Source File: ValidateCodeGatewayFilter.java    From smaker with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public GatewayFilter apply(Object config) {
	return (exchange, chain) -> {
		ServerHttpRequest request = exchange.getRequest();

		// 不是登录请求,直接向下执行
		if (!StrUtil.containsAnyIgnoreCase(request.getURI().getPath()
				, SecurityConstants.OAUTH_TOKEN_URL)) {
			return chain.filter(exchange);
		}

		// 刷新token,直接向下执行
		String grantType = request.getQueryParams().getFirst("grant_type");
		if (StrUtil.equals(SecurityConstants.REFRESH_TOKEN, grantType)) {
			return chain.filter(exchange);
		}

		// 终端设置不校验, 直接向下执行
		try {
			String[] clientInfos = WebUtils.getClientId(request);
			if (filterIgnorePropertiesConfig.getClients().contains(clientInfos[0])) {
				return chain.filter(exchange);
			}

			//校验验证码
			checkCode(request);
		} catch (Exception e) {
			ServerHttpResponse response = exchange.getResponse();
			response.setStatusCode(HttpStatus.PRECONDITION_REQUIRED);
			try {
				return response.writeWith(Mono.just(response.bufferFactory()
						.wrap(objectMapper.writeValueAsBytes(
								SmakerResult.builder().msg(e.getMessage())
										.code(CommonConstants.FAIL).build()))));
			} catch (JsonProcessingException e1) {
				log.error("对象输出异常", e1);
			}
		}

		return chain.filter(exchange);
	};
}
 
Example 14
Source File: OperateLogController.java    From Jpom with MIT License 4 votes vote down vote up
@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);
        }
    }
}