SpringMVC HandlerInterceptorAdapter
2017-08-10 10:40
453 查看
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name></display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <session-config> <session-timeout>240</session-timeout> </session-config> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-*.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/mgr/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>login.html</welcome-file> </welcome-file-list> </web-app>
spring-context.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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <mvc:annotation-driven/> <context:component-scan base-package="com.empl.mgr"/> <context:property-placeholder location="classpath:config.properties"/> <context:component-scan base-package="org.springframework.web.fileupload"/> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="100000"/> </bean> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> <property name="initialPoolSize" value="${jdbc.initialPoolSize}"></property> <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property> <property name="minPoolSize" value="${jdbc.minPoolSize}"></property> <property name="maxStatements" value="${jdbc.maxStatements}"></property> <property name="maxStatementsPerConnection" value="${jdbc.maxStatementsPerConnection}"></property> <property name="acquireIncrement" value="${jdbc.acquireIncrement}"></property> <property name="acquireRetryAttempts" value="${jdbc.acquireRetryAttempts}"></property> <property name="autoCommitOnClose" value="${jdbc.autoCommitOnClose}"></property> <property name="acquireRetryDelay" value="${jdbc.acquireRetryDelay}"></property> <property name="maxIdleTimeExcessConnections" value="${jdbc.maxIdleTimeExcessConnections}"></property> <property name="maxIdleTime" value="${jdbc.maxIdleTime}"></property> <property name="idleConnectionTestPeriod" value="${jdbc.idleConnectionTestPeriod}"></property> <property name="breakAfterAcquireFailure" value="${jdbc.breakAfterAcquireFailure}"></property> </bean> <!-- 配置sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> <prop key="hibernate.hbm2ddl">${hibernate.hbm2ddl}</prop> </props> </property> <property name="packagesToScan" value="com.empl.mgr.model"></property> </bean> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="nestedTransactionAllowed" value="true"></property> <property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:annotation-driven transaction-manager="txManager"/> <mvc:interceptors> <bean class="com.empl.mgr.intercept.SecureValidInterceptor"></bean> </mvc:interceptors> </beans>
SecureValidInterceptor.java
package com.empl.mgr.intercept; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONObject; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import com.empl.mgr.annotation.SecureValid; import com.empl.mgr.constant.LoginState; import com.empl.mgr.constant.SessionKey; import com.empl.mgr.model.TeAccount; import com.empl.mgr.service.AccountService; import com.empl.mgr.service.ModuleService; import com.empl.mgr.support.JSONReturn; import com.empl.mgr.utils.CompareUtil; public class SecureValidInterceptor extends HandlerInterceptorAdapter { @Autowired private ModuleService moduleService; @Autowired private AccountService accountService; /** * * @param request * @param response * @param handle */ public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handle) throws Exception { // 为了模仿实际网络中请求的速度, 这里让每一个请求的线程都睡眠50毫秒, 项目真正上线需删除 // Thread.sleep(50); if (request.getRequestURI().contains("mgr/0")) { return true; } //从session中获取属性为acctName的值,这里指登录用户名 String userName = (String) request.getSession().getAttribute(SessionKey.MODULEACCTNAME); if (StringUtils.isEmpty(userName)) { response.getOutputStream().print(JSONObject.fromObject(JSONReturn.buildFailure(LoginState.UNLOGIN)).toString()); return false; } HandlerMethod handlerMethod = (HandlerMethod) handle; SecureValid secureValid = handlerMethod.getMethod().getAnnotation(SecureValid.class);//注解类 if (CompareUtil.isEmpty(secureValid)) { return true; } // 从数据库中获取当前账户是否存在, 如果不存在, 提示未登录 TeAccount teAccount = accountService.findAccountByName(userName); if (CompareUtil.isEmpty(teAccount)) { response.getOutputStream().print(JSONObject.fromObject(JSONReturn.buildFailure(LoginState.UNLOGIN)).toString()); return false; } // 如果是超管, 直接通过拦截器 if (teAccount.getAcctSuper()) { return true; } // 没有权限 if (!moduleService.secureValid(userName, secureValid.code(), secureValid.type())) { response.getOutputStream().print(JSONObject.fromObject(JSONReturn.buildFailure(LoginState.PERMISSION_DENIED)).toString()); return false; } // 通过 return true; } /** * */ public void postHandle(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse, Object obj, ModelAndView modelandview) throws Exception { } public void afterCompletion(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse, Object obj, Exception exception) throws Exception { } public void afterConcurrentHandlingStarted(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse, Object obj) throws Exception { } }
继承关系:
/*jadclipse*/// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov. // Jad home page: http://www.kpdus.com/jad.html // Decompiler options: packimports(3) radix(10) lradix(10) // Source File Name: HandlerInterceptorAdapter.java package org.springframework.web.servlet.handler; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.AsyncHandlerInterceptor; import org.springframework.web.servlet.ModelAndView; public abstract class HandlerInterceptorAdapter implements AsyncHandlerInterceptor { public HandlerInterceptorAdapter() { } public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { return true; } public void postHandle(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse, Object obj, ModelAndView modelandview) throws Exception { } public void afterCompletion(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse, Object obj, Exception exception) throws Exception { } public void afterConcurrentHandlingStarted(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse, Object obj) throws Exception { } } /* DECOMPILATION REPORT Decompiled from: E:\PRJ_J2EE\HRMS\WebContent\WEB-INF\lib\spring-webmvc-3.2.5.RELEASE.jar Total time: 94 ms Jad reported messages/errors: Exit status: 0 Caught exceptions: */
/*jadclipse*/// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov. package org.springframework.web.servlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; // Referenced classes of package org.springframework.web.servlet: // HandlerInterceptor public interface AsyncHandlerInterceptor extends HandlerInterceptor { public abstract void afterConcurrentHandlingStarted(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse, Object obj) throws Exception; } /* DECOMPILATION REPORT Decompiled from: E:\PRJ_J2EE\HRMS\WebContent\WEB-INF\lib\spring-webmvc-3.2.5.RELEASE.jar Total time: 45 ms Jad reported messages/errors: Exit status: 0 Caught exceptions: */
/*jadclipse*/// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov. package org.springframework.web.servlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; // Referenced classes of package org.springframework.web.servlet: // ModelAndView public interface HandlerInterceptor { public abstract boolean preHandle(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse, Object obj) throws Exception; public abstract void postHandle(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse, Object obj, ModelAndView modelandview) throws Exception; public abstract void afterCompletion(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse, Object obj, Exception exception) throws Exception; } /* DECOMPILATION REPORT Decompiled from: E:\PRJ_J2EE\HRMS\WebContent\WEB-INF\lib\spring-webmvc-3.2.5.RELEASE.jar Total time: 19 ms Jad reported messages/errors: Exit status: 0 Caught exceptions: */
执行顺序:
参考前辈:
http://blog.csdn.net/cws1214/article/details/52151535
相关文章推荐
- SpringMVC HandlerInterceptorAdapter登陆验证拦截器
- Spring MVC HandlerInterceptorAdapter
- springmvc HandlerInterceptoer WebRequestInterceptor MethodInterceptor使用
- 废弃的org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
- spring HandlerInterceptorAdapter拦截ajax异步请求,报错ERR_INCOMPLETE_CHUNKED_ENCODING
- org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
- 废弃的org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
- Spring MVC HandlerInterceptor 实现原理(源码)
- spring拦截器,HandlerInterceptorAdapter配置后未生效
- Spring Boot使用HandlerInterceptorAdapter和WebMvcConfigurerAdapter实现原始的登录验证
- SPRING IN ACTION 第4版笔记-第八章Advanced Spring MVC-001- 配置SpringFlow(flow-executor、flow-registry、FlowHandlerMapping、FlowHandlerAdapter)
- springmvc HandlerMapping HandlerAdapter
- Spring HandlerInterceptor的使用
- maven Spring 4.2+SpringMVC+dubbo解决TypeProxyInvocationHandler.invoke(SerializableTypeWrapper.java:239
- HandlerInterceptorAdapter
- spring mvc interceptor 与 aop 执行顺序
- springMvc拦截器(HandlerInterceptorAdapter)
- Spring HandlerInterceptor的使用
- Spring mvc <mvc:resources /> 和<mvc:default-servlet-handler />
- SpringMvc Interceptor拦截器的配置与使用