前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springmvc 的登录拦截

springmvc 的登录拦截

作者头像
周杰伦本人
发布2023-10-12 14:33:31
1440
发布2023-10-12 14:33:31
举报
文章被收录于专栏:同步文章同步文章
springmvc 登录拦截

首先写一个拦截器 实现HandlerInterceptor接口

代码语言:javascript
复制
package com.xiepanpan.ecps;

import com.xiepanpan.ecps.model.TsPtlUser;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * describe: 用户登录拦截器
 *
 * @author xiepanpan
 * @date 2018/11/15
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        TsPtlUser user = (TsPtlUser) request.getSession().getAttribute("user");
        if (user==null) {
            String path = request.getContextPath();
            response.sendRedirect(path+"/user/toLogin.do");
            //拦截
            return false;
        }else {
            //放行
            return true;
        }
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}

然后在springmvc.xml中配置

代码语言:javascript
复制
<?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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.1.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">

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

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/shop/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
  <!-- 拦截器拦截配置-->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/user/login/**"/>
            <bean class="com.xiepanpan.ecps.LoginInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors>
</beans>

路径为/user/login开头的所有url都被拦截 完成。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-10-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • springmvc 登录拦截
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档