---
title: "SpringMVC 登陆拦截器实现登陆控制"
author: "xp9802"
date: "2014-11-12T09:31:11Z"
tags: []
categories: ["编程语言"]
slug: "springmvc"
draft: false
lastmod: "2014-11-12T09:31:10Z"
id: 2153321
url: "https://xp9802.iteye.com/blog/2153321"
---

思路，先登陆后，将登陆信息存储在session中，然后通过拦截器，对系统中的页面和资源进行访问拦截，同时对于登陆本身相关的页面和资源不拦截。

 

实现方法：

> 1
> 
> 2
> 
> 3
> 
> 4
> 
> 5
> 
> 6
> 
> 7
> 
> 8
> 
> 9
> 
> 10
> 
> 11
> 
> 12
> 
> 13
> 
> 14
> 
> 15
> 
> 16
> 
> 17
> 
> 18
> 
> 19
> 
> 20
> 
> 21
> 
> 22
> 
> 23
> 
> 24
> 
> 25
> 
> 26
> 
> 27
> 
> 28
> 
> 29
> 
> 30
> 
> 31
> 
> `/**`
> 
> `* 登陆拦截器.`
> 
> `*`
> 
> `* @author leizhimin 2014/6/26 16:08`
> 
> `*/`
> 
> `public` `class` `LoginInterceptor extends` `HandlerInterceptorAdapter {`
> 
> `private` `static` `final` `String[] IGNORE_URI = {"/login.jsp", "/Login/","backui/","frontui/"};`
> 
>  
> 
> `@Override`
> 
> `public` `boolean` `preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws` `Exception {`
> 
> `boolean` `flag = false;`
> 
> `String url = request.getRequestURL().toString();`
> 
> `System.out.println(">>>: "` `+ url);`
> 
> `for` `(String s : IGNORE_URI) {`
> 
> `if` `(url.contains(s)) {`
> 
> `flag = true;`
> 
> `break;`
> 
> `}`
> 
> `}`
> 
> `if` `(!flag) {`
> 
> `T_supplier_user user = LoginController.getLoginUser(request);`
> 
> `if` `(user != null) flag = true;`
> 
> `}`
> 
> `return` `flag;`
> 
> `}`
> 
>  
> 
> `@Override`
> 
> `public` `void` `postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws` `Exception {`
> 
> `super.postHandle(request, response, handler, modelAndView);`
> 
> `}`
> 
> `}`

1

2

3

4

5

6

`<mvc:interceptors>`

`<mvc:interceptor>`

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

`<bean` `id="loginInterceptor"` `class="net.xiucheren.web.interceptor.LoginInterceptor"/>`

`</mvc:interceptor>`

`</mvc:interceptors>`