到这段时,
想起来了以前学底层一些的servlet & jsp技术时,
过滤器和拦截器也有相关实现,
但比起来,spring的实现,可用性高很多。
其它代码不再放上来,
只放拦截器实现和xml配置吧。
AuthorizationInterceptor.java
package org.fkit.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.fkit.domain.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class AuthorizationInterceptor implements HandlerInterceptor {
private static final String[] IGNORE_URI = {"/loginForm", "/login"};
@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object Handler, Exception exception) throws Exception {
System.out.println("AuthorizationInterceptor afterCompletion-->");
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response,
Object handler, ModelAndView mv)
throws Exception {
// TODO Auto-generated method stub
System.out.println("AuthorizationInterceptor postCompletion-->");
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
// TODO Auto-generated method stub
System.out.println("AuthorizationInterceptor preCompletion-->");
boolean flag = false;
String servletPath = request.getServletPath();
for (String s : IGNORE_URI) {
if (servletPath.contains(s)) {
flag = true;
break;
}
}
if (!flag) {
User user = (User) request.getSession().getAttribute("user");
if (user == null ) {
System.out.println("AuthorizationInterceptor拦截讲求:");
request.setAttribute("message", "请先登陆再访问网站");
request.getRequestDispatcher("loginForm").forward(request, response);
} else {
System.out.println("AuthorizationInterceptor 放行请求:");
flag = true;
}
}
return flag;
}
}
springmvc-config.xml
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<context:component-scan base-package="org.fkit.controller"/>
<mvc:annotation-driven />
<mvc:default-servlet-handler/>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/*"/>
<bean class="org.fkit.interceptor.AuthorizationInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/content/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize">
<value>10285760</value>
</property>
<property name="defaultEncoding">
<value>UTF-8</value>
</property>
</bean>
</beans>