springmvc里使用注解进行拦截器配置

haidaoxianzi 2018-12-03

springmvc本身不能像struts2那样使用注解拦截action,这在使用中非常不方便,我们来扩展下springmvc,增加一个类,就可以实现注解拦截器.

首先把下面这个类拷贝进去:

package interceptor;

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.method.HandlerMethod;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public abstract class BaseInterceptor extends HandlerInterceptorAdapter {

public String success;

public String failed;

public boolean isMyHandler(Object handler) {

if (!(handler instanceof HandlerMethod))

return false;

HandlerMethod handlerMethod = (HandlerMethod) handler;

Interceptor interceptor = handlerMethod.getMethodAnnotation(Interceptor.class);

if (interceptor == null)

return false;

if (!interceptor.name().equals(this.getClass().getSimpleName()) && !interceptor.name().equals(this.getClass().getName()))

return false;

success = interceptor.success();

failed = interceptor.failed();

return true;

}

@Override

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

if (isMyHandler(handler)) {

return runHandler(response, runHandler(request, response));

}

return super.preHandle(request, response, handler);

}

public abstract boolean runHandler(HttpServletRequest request, HttpServletResponse response);

/**

* 运行结果

*

* @param response

* @param isInterceptor

* 是否拦截,true拦截,跳转向failed指向页面,false:不拦截,跳转向success指向页面;

* success和failed为空时不做任何操作

* @return

* @throws Exception

*/

public boolean runHandler(HttpServletResponse response, boolean isInterceptor) throws Exception {

if (!isInterceptor) {

if (!success.equals("")) {

response.sendRedirect(success);

return false;

}

} else {

if (!failed.equals("")) {

response.sendRedirect(failed);

return false;

}

}

return true;

}

@Target(ElementType.METHOD)

@Retention(RetentionPolicy.RUNTIME)

public static @interface Interceptor {

public String name();

public String success() default "";

public String failed() default "";

}

}

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

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

以上代码仅支持spring3.1.6以上版本,低一些的版本赶快升级吧.说真的,如果不能注解拦截器,开发时候像是登录验证/权限认证太费事了.

自己写一个拦截器,继承与上面的类,如下:

package interceptor;

import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import service.UserServiceImpl;

public class LoginCheck extends BaseInterceptor {

@Resource

private UserServiceImpl userService;

@Override

public boolean runHandler(HttpServletRequest request, HttpServletResponse response) {

//.....判断是否登陆,判断是否有权限等逻辑代码

return false;

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

在springmvc配置文件中配置拦截器:

<mvc:interceptors>

<mvc:interceptor>

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

<bean class="interceptor.LoginCheck"></bean>

</mvc:interceptor>

</mvc:interceptors>

1

2

3

4

5

6

拦截器配置为拦截所有controller.

在controller方法上写

@Interceptor(name = "LoginCheck", failed = "/user/login")

1

如:

@Interceptor(name = "LoginCheck", failed = "/user/login")

@RequestMapping("/user/del/{id}")

public void del(@PathVariable int id, HttpServletResponse response) throws IOException {

userService.del(id);

response.sendRedirect("/users/1");

}

1

2

3

4

5

6

7

这样在访问del方法的时候,就会先运行logincheck拦截器,如果拦截器拦截了,就会跳转到user/login页面,是不是和struts2的拦截器一样好用了?

springmvc里使用注解进行拦截器配置

相关推荐