Intercepting requests before moving to a controller in an annotation-based Spring web application

I am developing a web application and I am using Spring framework. The fact is that I never learned to use Spring, and I lost it all.

However, using annotation-based controllers, I was able to build most of my application!

Now the problem is that I will need to process ton requests before they are sent to the controllers (I need so that I can check if the user has access to the page that he is requesting). I spent about 5 hours searching for information about this, and actually I found quite a lot, none of them worked properly, I could never make my interceptor a simple "Hello World".

Here is what I have in my * -servlet.xml (I also have another beans definition):

<!-- this should be the class that contains the "hello world" -->
<bean id="myInterceptor" class="com.ibm.brmt.srb.admin.web.controller.TimeBasedAccessInterceptor"/>

<!-- this should "map" my interceptor no? -->
<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="interceptors">
        <list>
            <ref bean="myInterceptor"/>
        </list>
    </property>
</bean> 

and here is my TimeBasedAccessInterceptor class (the name doesn't matter and will probably be changed)

package web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;



public class TimeBasedAccessInterceptor extends HandlerInterceptorAdapter{

    public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception {
        System.out.println("-------------------------------------------------------------------------------------------");      
        }

    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception {
        System.out.println("-------------------------------------------------------------------------------------------");      
        }

    public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
        System.out.println("-------------------------------------------------------------------------------------------");      
        return false;
    }
}

codes are compiled and executed, but the fact that the TimeBasedAccessInterceptor class is never called (I even used breakpoints). can someone help me?

upon request, here is a "preview" of how I implement the controller in the * -servlet.xml file

<bean id="controllerName" class="web.controller.controllerNameController">  
    <property name="property1" ref="beanRef" />
    <property name="property1" ref="beanRef2"/>
</bean>

and in the controller NameController.java:

  package web.controller;

    @Controller
    public class controllerNameController{

        @RequestMapping
        public void find(String[] enabledLvlCodes, String reset, String readRuleId, Filter filter, Errors errors,
                Model model, HttpSession session) {

       //Code goes here
       }
}
+3
source share
1 answer

Your mapping looks correct.

, , DefaultAnnotationHandlerMapping ( )?

, , (, , , :-))

: "", AbstractHandlerMapping.getHandler() , HandlerExecutionChain , .

+1

Source: https://habr.com/ru/post/1715006/


All Articles