springmvc 登录拦截器

1 拦截器概念和struts2一致

2 实现拦截器

  a 实现handlerinterceptor接口

 

 

public class MyInterceptor implements HandlerInterceptor {

    //在请求处理的方法之前执行
    //如果返回true 那么执行下一个拦截器,如果返回false那么不去执行下一个拦截器
    @Override
    public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
            Object arg2) throws Exception {
        // TODO Auto-generated method stub
        System.out.println("-------处理前------");
        return true;
    }
    //在请求处理的方法执行之后执行
    @Override
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
            Object arg2, ModelAndView arg3) throws Exception {
        System.out.println("-------处理后--------");
        // TODO Auto-generated method stub    
    }
    //在DispatcherServlet处理后执行---清理工作
    @Override
    public void afterCompletion(HttpServletRequest arg0,
            HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception {
        
        // TODO Auto-generated method stub
        
    }
}

b 配置拦截器

<?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:p="http://www.springframework.org/schema/p"
    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
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        <!-- configure the InternalResourceViewResolver -->
    
    <context:component-scan base-package="com.sgcc.controller"></context:component-scan>
    <!-- 拦截器的配置 -->
    <mvc:interceptors>
        <mvc:interceptor>
        <!-- 包括路径及其子路径
            如果是/admin/* 拦截的是/admin/add  /admin/list etc /admin/user/add不会被拦截
            如果是/admin/** 上面都能拦截
         -->
            <mvc:mapping path="/**"/>
            <!-- 对应的拦截器 -->
            <bean class="com.sgcc.interceptor.MyInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors>
</beans>

 3 如果被拦截 - 能否到达指定的页面?

使用HttpServletResponse 或者HttpServletRequest可以实现转发或重定向

1 //在请求处理的方法之前执行
2     //如果返回true 那么执行下一个拦截器,如果返回false那么不去执行下一个拦截器
3     @Override
4     public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
5             Object arg2) throws Exception {
6         // TODO Auto-generated method stub
7         System.out.println("-------处理前------");
8         return true;
9     }

4 拦截器的应用--登录拦截器

package com.sgcc.interceptor;

import java.util.List;

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 {

    private List<String> allowedPass;
    public List<String> getAllowedPass() {
        return allowedPass;
    }
    public void setAllowedPass(List<String> allowedPass) {
        this.allowedPass = allowedPass;
    }
    
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
            Object handler) throws Exception {
        // TODO Auto-generated method stub
//        
//        System.out.println("-------处理前------");
//        response.sendRedirect(request.getContextPath()+"/index.jsp");
        
        String url = request.getRequestURL().toString();
        //允许哪些url不被拦截,哪些需要被拦截
        //先判断session中是否有
        Object user = request.getSession().getAttribute("user");
        if (user!=null) {
            return true;
        }
        
        for (String temp :allowedPass) {
            if (url.endsWith(temp)) {
                return true;
            }
        }
        response.sendRedirect(request.getContextPath()+"/login.jsp");
        return false;
    }
    //在请求处理的方法执行之后执行
    @Override
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
            Object arg2, ModelAndView arg3) throws Exception {
        System.out.println("-------处理后--------");
        // TODO Auto-generated method stub    
    }
    //在DispatcherServlet处理后执行---清理工作
    @Override
    public void afterCompletion(HttpServletRequest arg0,
            HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception {
        
        // TODO Auto-generated method stub
        
    }
}

配置

<?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:p="http://www.springframework.org/schema/p"
    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
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        <!-- configure the InternalResourceViewResolver -->
    
    <context:component-scan base-package="com.sgcc.controller"></context:component-scan>
    <!-- 拦截器的配置 -->
    <mvc:interceptors>
        <mvc:interceptor>
        <!-- 包括路径及其子路径         拦截所有controller
            如果是/admin/* 拦截的是/admin/add  /admin/list etc /admin/user/add不会被拦截
            如果是/admin/** 上面都能拦截
         
            <mvc:mapping path="/product/*"/>
            <mvc:mapping path="/cart/*"/> -->
            <mvc:mapping path="/**"/>
            <!-- 对应的拦截器 -->
            <bean class="com.sgcc.interceptor.LoginInterceptor">
                <property name="allowedPass">
                    <list>
                        <value>login.do</value>
                        
                    </list>
                </property>
            </bean>
        </mvc:interceptor>
    </mvc:interceptors>
</beans>

controller

package com.sgcc.controller;


import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sgcc.entity.User;

@Controller
public class UserController {
    
    @RequestMapping("/login")
    public String  Login (User user,HttpSession session){
        if ("sgcc".equals(user.getName())&&"sgcc".equals(user.getPwd())) {
            session.setAttribute("user", user);
            return "redirect:/index.jsp";
        }
        return "redirect:/login.jsp";
    }
    @RequestMapping("/add")
    public String add(){
        System.out.println("add");
        return "redirect:/index.jsp";
    }
    
}

 

posted @ 2016-11-17 23:16  alloevil  阅读(328)  评论(0编辑  收藏  举报