SpringMVC拦截器-实例展示-放行登录

SpringMVC拦截器-实例展示-放行登录_第1张图片
maven项目结构

关键部分

  
            

pom.xml


    4.0.0
    com.ssttisme
    spring-mvc-interceptor
    0.0.1-SNAPSHOT
    war
    
        
            org.springframework
            spring-webmvc
            4.3.9.RELEASE
        
        
            javax.servlet
            servlet-api
            2.5
            provided
        
    

web.xml



    spring-mvc-interceptor
    
        index.jsp
    
    
        DispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
        
        
            contextConfigLocation
            classpath:spring-*.xml
        
        1
    
    
    
        DispatcherServlet
        *.do
    

spring-mvc.xml



    
    
    
    
        
        
        
    
    
    
        
            
            
            
            
            
        
    

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




首页


    

请登录

用户名
密码

UserInterceptor.java

package com.mvc.interceptor;

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

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

public class UserInterceptor implements HandlerInterceptor{

    /**
     * Controller业务方法执行之前执行
     * @return 返回值决定请求是放行还是拦截
     * false:表示拦截
     * true:表示放行
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        System.out.println("UserInterceptor.preHandle()");
        HttpSession session=request.getSession();
        String username=(String) session.getAttribute("username");
        if(username==null||"".equals(username)){
            response.sendRedirect(request.getContextPath()+"/system/login.do");
            return false;
        }
        return true;
    }
    /**
     * 此方法在Controller的业务方法执行之后执行
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        System.out.println("UserInterceptor.postHandle()");
    }
    /**
     * 此方法在Controller的业务方法执行结束并且视图解析完成后执行
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        System.out.println("UserInterceptor.afterCompletion()");
    }

}

UserController.java

package com.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/system/")
@Controller
public class UserController {
    @RequestMapping("update")
    @ResponseBody
    public String update(){
        System.out.println("UserController.update()");
        return "Update success";
    }
}

LoginController.java

package com.mvc.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping("/system/")
@Controller
public class LoginController {
    @RequestMapping("login")
    public String doLogin(HttpServletRequest request){
        System.out.println("LoginController.doLogin()");
        request.getSession().setAttribute("username", request.getParameter("username"));
        return "index";
    }
}

http://localhost/spring-mvc-interceptor/system/update.do

SpringMVC拦截器-实例展示-放行登录_第2张图片

控制台

信息: Server startup in 3200 ms
UserInterceptor.preHandle()
LoginController.doLogin()
SpringMVC拦截器-实例展示-放行登录_第3张图片

控制台

LoginController.doLogin()

http://localhost/spring-mvc-interceptor/system/update.do


控制台

UserInterceptor.preHandle()
UserController.update()
UserInterceptor.postHandle()
UserInterceptor.afterCompletion()

你可能感兴趣的:(SpringMVC拦截器-实例展示-放行登录)