SSM框架10 登录拦截器

1、创建过滤器实现类

LoginInterceptor.java

package com.qst.interceptor;

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

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

//登录拦截
public class LoginInterceptor implements HandlerInterceptor {
    //执行controller之前
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        response.setContentType("text/html; charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        HttpSession session = request.getSession();
        Integer uid = (Integer) session.getAttribute("uid");
        if (uid==null){
            //表明登录失效或者未登录
            response.getWriter().println("<script>alert('尚未登录 请登录');window.location.href = \"/user/login\"</script>");
            return false;//表示不再执行下面的代码
        }
        return true;//放行
    }
    //进入controller之前,执行完controller之前
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }
    //执行controller之后
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}

2、在springmvc.xml中配置映射信息

springmvc.xml

<?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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/context
                  http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<!--注解驱动  spring提供的注解 比如@Component @Controller @Service @Repository
    在springMVC中 能够将扫描到的带有该注解的类扫描给spring容器 进行管理
    但是要想springMVC实现更加完整的功能,springMVC又封装了自身的注解
    比如@RequestMapping @GetMapping-->
    <mvc:annotation-driven/>

<!--        包扫描 将带有spring注解的类交给spring容器 进行管理
            建议扫描根包即可-->
    <context:component-scan base-package="com.qst"></context:component-scan>
    <mvc:default-servlet-handler/>



    <!--内部资源视图解析器 最终实现拼装成页面正确的路径-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".html"></property>
    </bean>

    <!-- 拦截器-->
    <!--  <mvc:interceptors><!-需要拦截哪些请求 .一个* 表示一级  ** 多级-->
    <mvc:interceptors>
         <mvc:interceptor>

             <mvc:mapping path="/user/*"/>
             <mvc:mapping path="/shop/*"/>
              <!--放行部分请求-->
             <mvc:exclude-mapping path="/user/reg"/>
             <mvc:exclude-mapping path="/user/login"/>
             <mvc:exclude-mapping path="/user/checkname"/>
             <mvc:exclude-mapping path="/user/reguser"/>
             <mvc:exclude-mapping path="/user/checkLohinname"/>

             <mvc:exclude-mapping path="/user/doLogin"/>
             <mvc:exclude-mapping path="/shop/toShop"/>
             <mvc:exclude-mapping path="/shop/westom"/>
             <mvc:exclude-mapping path="/shop/hongkong"/>
             <mvc:exclude-mapping path="/shop/nut"/>

             <mvc:exclude-mapping path="/shop/snack"/>

             <bean id="loginIntercepter" class="com.qst.interceptor.LoginInterceptor"/>
         </mvc:interceptor>

      </mvc:interceptors>
    <!-- 上传文件配置-->

    <!--&lt;!&ndash;文件上传&ndash;&gt;-->
    <!-- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
         &lt;!&ndash;设置上传最大尺寸为50MB&ndash;&gt;
         <property name="maxUploadSizePerFile" value="52428800"/>
         <property name="defaultEncoding" value="UTF-8"/>
         <property name="resolveLazily" value="true"/>
     </bean>-->




</beans>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值