zoukankan      html  css  js  c++  java
  • spring的权限控制,过滤器

    spring的过滤器可以实现登录状态问题

    1.创建一个AccessFilter类,基础代码

    package com.ujia.util.access;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    
    import com.ujia.entity.User;
    
    public class AccessFilter implements HandlerInterceptor {
    
        @Override
        public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception {
            
        }
    
        @Override
        public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception {
            
        }
    
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            //获取登录session
            User user=(User) request.getSession().getAttribute("user");
            //如果访问路径为登录首页,通过
            if(request.getRequestURI().startsWith(request.getContextPath()+"/user/login.htm")){
                return true;
            }
            //如果请求路径为登录,通过
            else if(request.getRequestURI().startsWith(request.getContextPath()+"/user/login")){
                return true;
            }
            //如果没有登录session,则返回到登录页面
            else if(null==user){
                response.sendRedirect(request.getContextPath()+"/user/login.htm");
                return false;
            }
            return true;
        }
    }

    springMVC配置

    <!-- 过滤器 -->
            <mvc:interceptors>
                <bean class="com.ujia.util.access.AccessFilter"></bean>
            </mvc:interceptors>
  • 相关阅读:
    一个简单的CSS3+js 实现3D BOX
    jquery $.extend()扩展插件获取焦点或失去焦点事件
    菜单滑动
    全选反选
    纯css,编写菜单移入效果
    登录窗口抖动效果
    [WCF]WCF起航
    FastReport 数据过滤
    [Oracle]TRIGGER
    两种递归方法的比较
  • 原文地址:https://www.cnblogs.com/angto64/p/5194442.html
Copyright © 2011-2022 走看看