Spring MVC interceptor 01 and spring MVC blocker 01

Source: Internet
Author: User

Spring MVC interceptor 01 and spring MVC blocker 01

Spring MVC Interception
Role: identity verification and permission check to prevent unauthorized access.
Scenario: In a bbs system, users cannot post or delete comments without logon;
A blog system cannot post a blog without logon, add a category, or delete a blog.

Spring MVC interception implementation is divided into two steps
(1) Compile the interceptor classMust inherit from org. springframework. web. servlet. HandlerInterceptor
Core methods:

public boolean preHandle(HttpServletRequest request,            HttpServletResponse response, Object arg2) throws Exception {

In this method, perform permission verification. To put it bluntly, check whether the logon is successful. The core code is as follows:

@ Override public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {response. setCharacterEncoding ("UTF-8"); HttpSession session = request. getSession (true); String loginFlag = (String) session. getAttribute (Constant2.SESSION _ KEY_LOGINED_FLAG); if (loginFlag = null | (! LoginFlag. equalsIgnoreCase (Constant2.FLAG _ LOGIN_SUCCESS) {String path = request. getRequestURI (); // "/demo_channel_terminal/news/list" System. out. println ("You are not authorized to access:" + path); String contextPath = request. getContextPath (); request. setCharacterEncoding ("UTF-8"); response. setStatus (401); response. sendRedirect (contextPath); return false;} return true ;}

(2) configure the spring MVC configuration file
My spring MVC configuration file named spring2-servlet.xml
Interceptor Configuration:

<mvc:interceptors>        <mvc:interceptor>            <mvc:mapping path="/bbs/json_add_bbs"></mvc:mapping>            <mvc:mapping path="/news/json_add_tips"></mvc:mapping>            <bean class="com.web.controller.intercept.MemberInterceptor">            </bean>        </mvc:interceptor>    </mvc:interceptors>


Explanation: when accessing/bbs/json_add_bbs and/news/json_add_tips, the interceptor class com. web. controller. intercept. MemberInterceptor (custom) will be applied)
This interceptor will not be applied when accessing other paths !!!

(3) project structure
The project is built using maven

Note:
If false is returned in the preHandle method, the request process is terminated, that is, no action is executed;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.