spring实现拦截登录请求

来源:互联网 发布:淘宝6.1.7版本下载官方 编辑:程序博客网 时间:2024/06/05 08:31

在做项目的过程中,都会涉及到用户的登录注册,访问一个请求时,有时还要判断用户是否已经登录。现在我们一拦截器的方式,来实现拦截操作。
首先需要定义要拦截哪些请求地址

// 拦截的地址private static final String[] IGNORE_URL={"SecurySet.action","SetMobileSecond.action","MyInvest.action","MyFlowFund.action","InvestStatistics.action","myAllGiftVoucher.action","AutomaticBidSet.action"};

这一块是我的要拦截的地址
具体的方法实现如下:

public class LoginInterceptor extends HandlerInterceptorAdapter{    // 拦截的地址    private static final String[] IGNORE_URL={"SecurySet.action","SetMobileSecond.action","MyInvest.action","MyFlowFund.action","InvestStatistics.action","myAllGiftVoucher.action","AutomaticBidSet.action"};    @Override    public boolean preHandle(HttpServletRequest request,            HttpServletResponse response, Object handler) throws Exception {        boolean falg=false;        String url=request.getRequestURL().toString();        //拦截上面的路径        for (String str : IGNORE_URL) {            if(url.contains(str)){                falg=true;                break;            }        }        if(falg){            Aaccount account = (Aaccount) request.getSession().getAttribute("accountSession");            if(account!=null){                falg=true;            }else {                response.sendRedirect("/Apcgc/Login.action");                return false;            }        }        return true;    }}

在spring中配置
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.immense.tw.interceptor.LoginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>

原创粉丝点击