【问题标题】:Spring Security Filter and Posted DataSpring Security 过滤器和发布的数据
【发布时间】:2023-03-03 22:24:01
【问题描述】:

我编写了一个自定义 Spring Security 过滤器,它需要用户发布到它的 XML 数据。过滤器如何获取发布的数据?

【问题讨论】:

    标签: spring spring-security


    【解决方案1】:

    这不是一个真正的 Spring Security 特定问题,因为您可能只是实现了javax.servlet.Filter 接口。在这种情况下,您正在实现该方法:

    public void doFilter ( ServletRequest request, ServletResponse response, 
       FilterChain chain ) throws IOException, ServletException;
    

    如果您需要特定于 HTTP 的数据(通常是必需的),您可以将 ServletRequest 转换为 HttpServletRequest

    import javax.servlet.http.HttpServletRequest;
    // ...
    HttpServletRequest httpRequest = (HttpServletRequest) request;
    String xml = httpRequest.getParameter("xml");
    

    如果您要扩展标准 Spring Security 过滤器之一,请确保查看您要扩展的过滤器的源代码!他们中的许多人已经重写了doFilter,并期望您将重写另一种方法来增强他们的行为。

    【讨论】:

      【解决方案2】:

      阅读AbstractAuthenticationProcessingFilter的文档(根据你之前的问题,我猜这是你的自定义过滤器扩展的类),实现的抽象方法是attemptAuthentication,它接收HttpServletRequest和HttpServletResponse作为参数:

      Parameters:
          request - from which to extract parameters and perform the authentication
          response - the response, which may be needed if the implementation has to do a redirect as part of a multi-stage authentication process (such as OpenID). 
      

      【讨论】:

        猜你喜欢
        • 2015-10-13
        • 2021-12-06
        • 2013-04-13
        • 2015-11-11
        • 2019-04-13
        • 2013-02-13
        • 2016-06-24
        • 2012-09-28
        • 2016-03-11
        相关资源
        最近更新 更多