springSecurityFilterChain它是如何工作的?

时间:2014-06-15 13:55:06

标签: java spring spring-mvc

我尝试使用springSecurityFilterChain在spring框架中实现自己的过滤器。我会在spring内容中实现该过滤器,在 web.xml 中没有。在 web.xml 中,我只使用一个过滤器,它将所有请求委托给位于spring内容中的过滤器。例如:

我的web.xml文件:

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

现在所有请求都将委托给位于spring内容中的过滤器。

好的,我们开始创建一个实现Filter

的bean过滤器类
package myapplication.filters

public class ExampleFilter implements Filter {

    private ServletContext context;

    public void init(FilterConfig fConfig) throws ServletException {
        // some code here
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        // some filter here
    }

    public void destroy() {
        //we can close resources here
    }

现在,当某些(人或其他应用程序)想要从url: / example 获取资源时,我会使用我的过滤器来调用doFilter方法。所以我们需要在 secruity.xml 文件中配置spring。在这一步,我会添加我不想使用html授权。我将通过接近POST请求获得用户名和密码等参数

我的secruity.xml:

 <http use-expressions="true" auto-config="false" >

    <!-- 
       HERE I DECLARE MY CUSTOM FILTER BU WHERE IS PATTERN.
       FROM WHERE SPRING WILL KNOWN THAT IF REQUEST COME /example
       THAN CALL METHOD doFilter FROM CLASS myapplication.filters.ExampleFilter
     -->
    <custom-filter ref="ExampleFilter " />


</http>

 <bean id="ExampleFilter " class="myapplication.filters.ExampleFilter"></bean>

0 个答案:

没有答案