springmvc里使用註解進行攔截器配置
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的攔截器一樣好用了?
打開今日頭條,查看更多圖片※非對稱加密演算法RSA 學習
※spring mvc 中對靜態資源的訪問配置
TAG:程序員小新人學習 |