SpringMVC基础(四)

SpringMVC基础(四)

拦截器

拦截器类似于过滤器,但是与过滤器不同的是:拦截器是基于AOP实现的!

拦截器作用于:客户端请求服务端的请求过程中。

定义一个拦截器

自定义拦截器必须实现 HandlerInterceptor 接口!

  • 写一个类实现 HandlerInterceptor 接口
  • 将 Interceptor 配置进 spring-mvc.xml 中,实现AOP

true:是拦截放行;false:是拦截住

public class MyIntercepetor implements HandlerInterceptor {
    
    
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    
    
        System.out.println("处理前");
        return true;
    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    
    
        System.out.println("处理后");
    }

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    
    
        System.out.println("清理");
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.qiu.controller"/>

    <mvc:default-servlet-handler/>

    <mvc:annotation-driven/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    
    <mvc:interceptors>
        <mvc:interceptor>
            <!--
            要拦截的路径 /**:根请求下的所有请求,也就是所有请求。
            /user/**:根请求下的user请求下的所有请求,也就是user请求下的所有子请求
            -->
            <mvc:mapping path="/**"/>
            <bean class="com.qiu.config.MyIntercepetor"/>
        </mvc:interceptor>
    </mvc:interceptors>
</beans>

一般写一个处理前的拦截就可以了!

但是它不像过滤器需要执行下面的代码,拦截器中return后下面的代码就不执行了!

拦截器的业务场景之一:用户只有登录之后才能访问主页(main.jsp),用户在无登录状态进入主页会拦截并跳转到登录页面;用户不能直接访问登录页,只有在index页面点击才能进入登录页。

@Controller
@RequestMapping("/user")
public class UserController {
    
    
    // 直接进入main页面
    @RequestMapping("goMain")
    public String goMain(){
    
    
        return "main";
    }

    // 直接进入login页面
    @RequestMapping("/goLogin")
    public String goLogin(){
    
    
        return "login";
    }

    // 登陆后,将判别用户登录字段放到session中,然后转发到main页面
    @RequestMapping("/login")
    public String login(HttpSession httpSession, String username, String pwd){
    
    
        // 将用户的用户名放进session,用来判别用户是否登录
        httpSession.setAttribute("userLoginInfo", username);
        return "main";
    }
}
public class LoginInterceptor implements HandlerInterceptor {
    
    
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    
    
        HttpSession session = request.getSession();
        // 如果用户登录后,放行
        if (session.getAttribute("userLoginInfo") != null){
    
    
            return true;
            // 如果用户请求login处理器(就是在提交登录时),放行
        }else if (request.getRequestURI().contains("login")){
    
    
            return true;
            // 如果用户请求goLogin处理器(跳转到login页面),放行
        }else if (request.getRequestURI().contains("goLogin")){
    
    
            return true;
        }

        // 其他情况都转发到登录页面:像直接访问goMain请求(直接进入首页)
        // 使用request进行请求转发,因为重定向客户端是访问不到WEB-INF下的
        request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
        return false;
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.qiu.controller"/>
    <mvc:default-servlet-handler/>
    <mvc:annotation-driven/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    
    
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean class="com.qiu.config.MyIntercepetor"/>
        </mvc:interceptor>
        
        <mvc:interceptor>
            <!--
            要拦截的路径 /**:根请求下的所有请求,也就是所有请求。
            /user/**:根请求下的user请求下的所有请求,也就是user请求下的所有子请求
            -->
            <mvc:mapping path="/user/**"/>
            <bean class="com.qiu.config.LoginInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>
</beans>

猜你喜欢

转载自blog.csdn.net/qq_43477218/article/details/114263451