Servlet3.0动态配置过滤器、监听器、Servlet

网友投稿 641 2022-09-09

Servlet3.0动态配置过滤器、-、Servlet

Servlet3.0动态配置过滤器、-、Servlet

Servlet 3.0 作为 Java EE 6 规范体系中一员,随着 Java EE 6 规范一起发布。该版本在前一版本(Servlet 2.5)的基础上提供了若干新特性用于简化 Web 应用的开发和部署。然后本博客就其中的一个特征,动态配置过滤器、-、Servlet,进行介绍

javax.servlet javax.servlet-api provided 3.1.0

实现AbstractAnnotationConfigDispatcherServletInitializer类:

package com.extra.login.core.servlet;import com.extra.login.cas.client.filter.authentication.AuthenticationFilter;import com.extra.login.cas.client.filter.session.SingleSignOutFilter;import com.extra.login.cas.client.util.AssertionThreadLocalFilter;import com.extra.login.cas.client.util.CasParamKeyEnum;import com.extra.login.cas.client.util.CasPropertiesLoader;import com.extra.login.cas.client.util.HttpServletRequestWrAPPerFilter;import org.jasig.cas.client.session.SingleSignOutHttpSessionListener;import org.jasig.cas.client.validation.Cas30ProxyReceivingTicketValidationFilter;import org.springframework.stereotype.Component;import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;import javax.servlet.*;import java.util.EnumSet;/** *

 *      基于Servlet3.0实现动态配置过滤器、-、Servlet * 
* *
 * @author mazq * 修改记录 *    修改后版本:     修改人:  修改日期: 2020/08/27 14:33  修改内容: * 
*/@Componentpublic class WebServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class[] getRootConfigClasses() { return new Class[0]; } @Override protected Class[] getServletConfigClasses() { return new Class[0]; } @Override protected String[] getServletMappings() { return new String[0]; } @Override public void onStartup(ServletContext servletContext) throws ServletException { this.registerCasFilter(servletContext); this.registerCasListener(servletContext); super.onStartup(servletContext); } /** * 动态注册CAS过滤器 * @Author mazq * @Date 2020/08/27 16:41 * @Param [servletContext] * @return void */ protected void registerCasFilter(ServletContext servletContext) { /* CAS单点登录校验过滤器 */ FilterRegistration casFilter = servletContext.addFilter("casFilter", AuthenticationFilter.class); casFilter.setInitParameter("casServerLoginUrl" , CasPropertiesLoader.getValue(CasParamKeyEnum.CAS_SERVER_HOST_LOGIN_URL.getCasParamKey())); casFilter.setInitParameter("serverName" , CasPropertiesLoader.getValue(CasParamKeyEnum.APP_SERVER_HOST_URL.getCasParamKey())); casFilter.setInitParameter("ignorePattern" , "/static/*"); casFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class) , true, "/*"); /* CAS单点登录ticket校验过滤器 */ FilterRegistration casValidationFilter = servletContext.addFilter("casValidationFilter", Cas30ProxyReceivingTicketValidationFilter.class); casValidationFilter.setInitParameter("casServerUrlPrefix" , CasPropertiesLoader.getValue(CasParamKeyEnum.CAS_SERVER_HOST_URL.getCasParamKey())); casValidationFilter.setInitParameter("serverName" , CasPropertiesLoader.getValue(CasParamKeyEnum.APP_SERVER_HOST_URL.getCasParamKey())); casValidationFilter.setInitParameter("redirectAfterValidation" , "true"); casValidationFilter.setInitParameter("useSession" , "true"); casValidationFilter.setInitParameter("encoding" , "UTF-8"); casValidationFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class) , true, "/*"); /* CAS单点登出过滤器 */ FilterRegistration singleSignOutFilter = servletContext.addFilter("singleSignOutFilter", SingleSignOutFilter.class); singleSignOutFilter.setInitParameter("casServerUrlPrefix" , CasPropertiesLoader.getValue(CasParamKeyEnum.CAS_SERVER_HOST_URL.getCasParamKey())); singleSignOutFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class) , true, "/*"); /* HttpServletRequestWrapper过滤器 */ FilterRegistration = servletContext.addFilter("HttpServletRequestWrapperFilter.class); , true, "/*"); /* AssertionThreadLocal过滤器 */ FilterRegistration assertionThreadLocalFilter = servletContext.addFilter("assertionThreadLocalFilter", AssertionThreadLocalFilter.class); assertionThreadLocalFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class) , true, "/*"); } /** * 注册CAS- * @Author mazq * @Date 2020/08/27 16:43 * @Param [servletContext] * @return void */ protected void registerCasListener(ServletContext servletContext){ //注册- servletContext.addListener(SingleSignOutHttpSessionListener.class); } @Override protected FilterRegistration.Dynamic registerServletFilter(ServletContext servletContext, Filter filter) { return super.registerServletFilter(servletContext, filter); } @Override protected void registerContextLoaderListener(ServletContext servletContext) { super.registerContextLoaderListener(servletContext); }}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:面试题:python 中 staticmethod 和 classmethod有什么区别(面试题:python学到啥程度,说下自己知道的)
下一篇:python 中的__init__.py的用法与个人理解(python123平台登录)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~