Thursday, 28 January 2016

How to disable HTTP_TRACE method for Apache httpd

Solution

In latest Apache (2.2 and later) The TraceEnable directive can be used to disable TRACE method: 
 all you have to do is add following line in  conf/httpd.conf:
       TraceEnable Off

Validation

1. Using telnet application, open a connection to you web server:
 go to command prompt adn type 
telnet <server-name-or-IP> <port_number>  : ex - telnet 10.192.152.23 80
2. Once connected, type the following:
   TRACE / HTTP/1.1
   Hit ENTER key twice.

you shoud be seeing something like below.. 
TraceEnable off <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
          <html>
          <head>
          <title>400 Bad Request</title>
          </head>
          <body>
          <h1>Bad Request</h1>
          <p>Your browser sent a request that this server could not understand.<br /></p>
          </body>
          </html>



all done !! go ahead.. 

Spring MVC interceptor

Each interceptor you define must implementorg.springframework.web.servlet.HandlerInterceptor interface. There are three methods that need to be implemented.
preHandle(..) is called before the actual handler is executed;
The preHandle(..) method returns a boolean value. You can use this method to break or continue the processing of the execution chain. When this method returns true, the handler execution chain will continue; when it returns false, the DispatcherServlet assumes the interceptor itself has taken care of requests (and, for example, rendered an appropriate view) and does not continue executing the other interceptors and the actual handler in the execution chain.
postHandle(..) is called after the handler is executed;

afterCompletion(..) is called after the complete request has finished.


How to implement?
1    1.   Spring MVC to configure it via <mvc:interceptors>tag within spring-servlet.xml file.
<mvc:interceptors>
  <bean class="raj.interceptor.HelloWorldInterceptor" />
</mvc:interceptors>


 2.            The Interceptor – Spring MVC HandlerInterceptor
   package raj.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

   import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class HelloWorldInterceptor implements HandlerInterceptor  {

//overriding abstract method (mandatory)

    @Override
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {
         
      System.out.println("Pre-handle");
         
        return true;
    }

//overriding abstract method (mandatory)
     
   @Override
    public void postHandle(HttpServletRequest request,
           HttpServletResponse response, Object handler,
           ModelAndView modelAndView) throws Exception {
       System.out.println("Post-handle");
   }
     //overriding abstract method (mandatory)
   @Override
   public void afterCompletion(HttpServletRequest request,
           HttpServletResponse response, Object handler, Exception ex)
           throws Exception {
       System.out.println("After completion handle");
   }
}



3. use your filter logic in prehandle method of abovesaid class !! 

its done..!!

The matching wildcard is strict, but no declaration can be found for element

Spring Mvc defects

Issue : The matching wildcard is strict, but no declaration can be found for element 'mvc:annotation-driven'

Solution : In your spring context xml mvc namespace url should match url in schemaLocation. Something like this:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
         http://www.springframework.org/schema/mvc
         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

This is a standard XML namespace declaration. The namespace url is sort of an unique id, which is then mapped to the actual schema location in xsi:schemaLocation.