Java Code Examples for cn.hutool.extra.servlet.ServletUtil#getHeaderIgnoreCase()

The following examples show how to use cn.hutool.extra.servlet.ServletUtil#getHeaderIgnoreCase() . 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: AuthorizeInterceptor.java    From Jpom with MIT License 6 votes vote down vote up
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    super.preHandle(request, response, handler);
    if (handler instanceof HandlerMethod) {
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        NotAuthorize notAuthorize = handlerMethod.getMethodAnnotation(NotAuthorize.class);
        if (notAuthorize == null) {
            String authorize = ServletUtil.getHeaderIgnoreCase(request, ConfigBean.JPOM_AGENT_AUTHORIZE);
            if (StrUtil.isEmpty(authorize)) {
                this.error(response);
                return false;
            }
            if (!AgentAuthorize.getInstance().checkAuthorize(authorize)) {
                this.error(response);
                return false;
            }
        }
    }
    return true;
}
 
Example 2
Source File: UrlRedirectUtil.java    From Jpom with MIT License 6 votes vote down vote up
/**
 * 获取 protocol 协议完全跳转
 *
 * @param request 请求
 * @param url     跳转url
 * @see javax.servlet.http.HttpUtils#getRequestURL
 */
public static String getRedirect(HttpServletRequest request, String url, int port) {
    String proto = ServletUtil.getHeaderIgnoreCase(request, "X-Forwarded-Proto");
    if (proto == null) {
        return url;
    } else {
        String host = request.getHeader(HttpHeaders.HOST);
        if (StrUtil.isEmpty(host)) {
            throw new RuntimeException("请配置host header");
        }
        if ("http".equals(proto) && port == 0) {
            port = 80;
        } else if ("https".equals(proto) && port == 0) {
            port = 443;
        }
        String format = StrUtil.format("{}://{}:{}{}", proto, host, port, url);
        return URLUtil.normalize(format);
    }
}
 
Example 3
Source File: UrlRedirectUtil.java    From Jpom with MIT License 6 votes vote down vote up
/**
 * 二级代理路径
 *
 * @param request req
 * @return context-path+nginx配置
 */
public static String getHeaderProxyPath(HttpServletRequest request, String headName, Function<String, String> function) {
    String proxyPath = ServletUtil.getHeaderIgnoreCase(request, headName);
    //
    if (StrUtil.isEmpty(proxyPath)) {
        return request.getContextPath();
    }
    // 回调处理
    if (function != null) {
        proxyPath = function.apply(proxyPath);
    }
    //
    proxyPath = FileUtil.normalize(request.getContextPath() + StrUtil.SLASH + proxyPath);
    if (proxyPath.endsWith(StrUtil.SLASH)) {
        proxyPath = proxyPath.substring(0, proxyPath.length() - 1);
    }
    return proxyPath;
}
 
Example 4
Source File: BaseAgentController.java    From Jpom with MIT License 5 votes vote down vote up
/**
 * 获取server 端操作人
 *
 * @param request req
 * @return name
 */
private static String getUserName(HttpServletRequest request) {
    String name = ServletUtil.getHeaderIgnoreCase(request, ConfigBean.JPOM_SERVER_USER_NAME);
    name = CharsetUtil.convert(name, CharsetUtil.CHARSET_ISO_8859_1, CharsetUtil.CHARSET_UTF_8);
    name = StrUtil.emptyToDefault(name, StrUtil.DASHED);
    return URLUtil.decode(name, CharsetUtil.CHARSET_UTF_8);
}
 
Example 5
Source File: BaseJpomInterceptor.java    From Jpom with MIT License 5 votes vote down vote up
static String getHeaderProxyPath(HttpServletRequest request) {
    String proxyPath = ServletUtil.getHeaderIgnoreCase(request, PROXY_PATH);
    if (StrUtil.isEmpty(proxyPath)) {
        return StrUtil.EMPTY;
    }
    if (proxyPath.endsWith(StrUtil.SLASH)) {
        proxyPath = proxyPath.substring(0, proxyPath.length() - 1);
    }
    return proxyPath;
}
 
Example 6
Source File: UrlRedirectUtil.java    From Jpom with MIT License 5 votes vote down vote up
private static int getPort(HttpServletRequest request) {
    String proxyPort = ServletUtil.getHeaderIgnoreCase(request, "X-Forwarded-Port");
    int port = 0;
    if (StrUtil.isNotEmpty(proxyPort)) {
        port = Integer.parseInt(proxyPort);
    }
    return port;
}
 
Example 7
Source File: ServerWebSocketInterceptor.java    From Jpom with MIT License 4 votes vote down vote up
@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 8
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);
        }
    }
}