【问题标题】:Servlet Filter as Security Proxy for Web ServicesServlet 过滤器作为 Web 服务的安全代理
【发布时间】:2012-05-02 16:44:31
【问题描述】:

好时光。

假设一个应用程序中有 8 个 Web 服务。其中 5 个需要授权(客户端必须提供 JSESSIONID cookie,并且相应的 session 不能失效),其他 3 个可以在没有 jsessionid cookie 的情况下调用。我天真的解决方案是编写一个 servlet 过滤器来拦截请求并检索它们的 pathInfos(所有服务都具有相同的 url 结构:/service/serviceSuffix)。有一个枚举,其中包含需要授权的每个 Web 服务的 serviceSuffix。检索请求时,将收集 pathInfo;如果此 pathInfo 包含在枚举中并且存在相应的有效会话,则请求将提前发送到过滤器链。否则,将错误发送回客户端。过了一会儿,我意识到需要添加检索具体服务的 wsdl 和 xsds 的可能性。因此,又添加了两个检查。

public class SecurityFilter implements Filter {

public static final String WSDL = "wsdl";
public static final String XSD = "xsd=";

/**
 * Wittingly left empty
 */
public void init(FilterConfig filterConfig) throws ServletException {}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {        HttpServletRequest servletRequest = (HttpServletRequest) request;
    HttpServletResponse servletResponse = (HttpServletResponse)response;
    String pathInfo = servletRequest.getPathInfo();
    String queryString = servletRequest.getQueryString();

    if (pathInfo != null && SecureWebServices.contains(pathInfo)) {
        if (queryString != null && (queryString.equals(WSDL) || queryString.startsWith(XSD))) {
            // wsdl or xsd is requested
            chain.doFilter(request, response);

        } else {
            // a web service's method is called
            HttpSession requestSession = servletRequest.getSession(false);
            if (requestSession != null) { // the session is valid
                chain.doFilter(request, response);
            } else {
                servletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
                return;
            }
        }
    } else {
        chain.doFilter(request, response);
    }
}

/**
 * Wittingly left empty
 */
public void destroy() {}

}

好像不是很安全,因为如果请求的pathInfo不在枚举中,这个请求就会被传递下去(以防一些意外的系统调用)。

能否请您建议做什么,如何提高安全级别。我想构建一个可配置的系统(这就是我有枚举的原因。可以只在其中添加一个路径来保护 Web 服务,并且不需要在每个 Web 服务中复制安全代码)。如何增加

【问题讨论】:

    标签: web-services security jakarta-ee


    【解决方案1】:

    也许我不明白,但是。

    jsessionid 与安全无关。你只是得到它。

    接下来我不确定您是否需要身份验证或授权。所提供的代码不会为您提供安全功能。

    我想你还是对身份验证感兴趣。可以通过标准 Web 容器功能提供安全逻辑。只需在请求标头中发送身份验证数据即可。 Web 容器可以配置为仅保护选定的资源 (url)

    【讨论】:

      猜你喜欢
      • 2011-04-04
      • 1970-01-01
      • 2013-11-06
      • 2017-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-25
      • 2017-02-20
      相关资源
      最近更新 更多