Announcement Announcement Module
Collapse
No announcement yet.
Adding a simple servlet filter using Spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Adding a simple servlet filter using Spring

    I'm experienced with Spring but not with using it in a web context. I have implemented a Servlet and have to authenticate users by parsing credentials and the invoking a remote HTTP authentication service which will return a pass or fail response. (Not the greatest security, but this is a requirement to deal with legacy issues). I was thinking of adding a servlet filter that intercepts all requests to my Servlet and handles the authentication. My servlet implements the spring HttpRequestHandler and is already managed using Spring configuration. Can someone recommend how to add my custom authentication Filter using Spring configuration such that it gets invoked whenever a user tries to access my servlet?

    My simple web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app>
    <display-name>rd-springwebservice</display-name>
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/beans.xml</param-value>
    </context-param>

    <listener>
    <description>Spring Listener</description>
    <listener-class>org.springframework.web.context.ContextLoade rListener</listener-class>
    </listener>

    <servlet>
    <servlet-name>SpringTestServlet</servlet-name>
    <servlet-class>org.springframework.web.context.support.Http RequestHandlerServlet</servlet-class>
    </servlet>

    <servlet-mapping>
    <servlet-name>SpringTestServlet</servlet-name>
    <url-pattern>/SpringTestServlet</url-pattern>
    </servlet-mapping>
    </web-app>

    And my simple spring configuration:


    <beans>

    <bean id="SpringTestServlet" class="springtest.servlet.SpringTestServlet">
    </bean>

    </beans>


    Thanks!

  • #2
    Please use [ code][/code ] tags when posting code.

    1) Your Servlet doesn't have to be a servlet it can simply be a plain class implementing the HttpRequestHandler interface
    2) Simply implement the Filter interface and register it in your web.xml
    3) Create a filter-mapping

    Also I'm still not sure what your servlet does, is it calling the authentication service is it doing something else? At least it looks strange.

    Comment


    • #3
      Thanks for the tip, i'll use the code tags next time.

      The design is as follows - I'm creating a servlet that will receive Jar uploads from remote clients via HTTP. The job of the servlet is to accept the Jar upload, unpack it, and process the contents within. The next piece is to authenticate the client requests. I can't disclose all of the details, but the credentials passed in the client requests will have to be forwarded to a remote HTTP service that will return a pass or fail response. After the pass or fail is received, my servlet can then process the Jar upload. I want the authentication logic completely abstracted from the Servlet itself, so I figured this could be handled from within a Servlet Filter.

      I did some experimenting and I believe I have the solution. It uses Spring's DelegatingFilterProxy.

      I added this to my web.xml
      Code:
      	 <filter>
      	   <filter-name>SpringTestFilter</filter-name>
      	   <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
      	</filter>
      
      	<filter-mapping>
      	   <filter-name>SpringTestFilter</filter-name>
      	   <servlet-name>SpringTestServlet</servlet-name>
      	</filter-mapping>
      And added a bean that implements the Filter interface to my spring xml:

      Code:
      	<bean id="SpringTestFilter" class="com.antennasoftware.app.sprinttest.servlet.SpringTestFilter">  
      	</bean>
      When client requests come in, the DelegatingFilterProxy invokes my spring managed Filter bean to handle the authentication prior to the invocation my Jar upload servlet.

      Comment

      Working...
      X