• StringMVC做登录拦截


     

     

    StringMVC做登录拦截

    1:拦截类

    package cn.sjt.ums.interceptor;

    import javax.servlet.http.HttpServletRequest;

    import javax.servlet.http.HttpServletResponse;

    import org.springframework.web.servlet.HandlerInterceptor;

    import org.springframework.web.servlet.ModelAndView;

    public class LoginInterceptor implements HandlerInterceptor{

    @Override

    public void afterCompletion(HttpServletRequest request,

    HttpServletResponse response, Object obj, Exception err)

    throws Exception {

    }

    @Override

    public void postHandle(HttpServletRequest request, HttpServletResponse response,

    Object obj, ModelAndView mav) throws Exception {

    }

     

    @Override

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,

    Object obj) throws Exception {

            // session 里面获取用户ID

            String userId = (String) request.getSession().getAttribute("userid");  

            // 判断如果没有取到用户ID,就跳转到登陆页面,提示用户进行登陆  

            if (userId == null || "".equals(userId)) {

                response.sendRedirect("/login");

                return false;

            }

            return true;

    }

    }

    2applicationContext-springmvc.xml的配置

    <!-- 端口后加"/",跳转至登录页 -->

    <mvc:view-controller path="/" view-name="redirect:/login" /> 

        <mvc:interceptors>  

            <mvc:interceptor>  

                <mvc:mapping path="/**" /> 

                 

            <!-- 不需要拦截的配置 -->

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

                <mvc:exclude-mapping path="/login" />

                <mvc:exclude-mapping path="/captcha-image" />

                <mvc:exclude-mapping path="/user/inneruser/login" />

                <mvc:exclude-mapping path="/css/**" />

              <mvc:exclude-mapping path="/images/**" /> 

              <mvc:exclude-mapping path="/js/**"/>   

                <bean class="cn.sjt.ums.interceptor.LoginInterceptor"/>  

            </mvc:interceptor>  

        </mvc:interceptors>  

     

  • 相关阅读:
    最大组合的数 A
    2106. 求对称字符串的最大长度 A
    内存分配 A
    242. 子串匹配 A
    【LeetCode 1055】形成字符串的最短路径 A
    给定差值的组合 A
    1791 设备编号 A
    最长的指定瑕疵度的元音子串 A
    1898. 【认证试题】遥控小车 A
    1792 服务器集群网络延迟 A
  • 原文地址:https://www.cnblogs.com/ai211234/p/5620759.html
Copyright © 2020-2023  润新知