java web客户端集成cas,web.xml通过编码的方式来加载配置

前言:

项目集成cas单点登录,需要在web.xml里加上cas-client的过滤器配置,但是配置地址太繁琐,而且有重复配置,能不能让web.xml去读properties文件,只做一次配置。在porperties里配置cas单点开关参数,开就加载cas配置,集成cas,关就不集成。

实现思路:

通过查找资料,web.xml可以以编码的方式来实现配置。spring4.0以上的版本支持web.xml的编码配置。实现AbstractAnnotationConfigDispatcherServletInitializer接口,在servlet3.0中web.xml启动时会检测该接口实现类,从能够在实现类中去配置filter。

代码实现:

 

public class WebAppLoadInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected String[] getServletMappings() {
        return new String[0];
    }

    @Override
    protected Class[] getRootConfigClasses() {
        return new Class[0];
    }

    @Override
    protected Class[] getServletConfigClasses() {
        return new Class[0];
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        System.out.print(servletContext.getContextPath());
        PropertiesLoader propertiesLoader = new PropertiesLoader("classpath:wdim.properties");
        String casStartup = propertiesLoader.getProperty("cas.startup");
        if("true".equals(casStartup)){
            String casServerUrl = propertiesLoader.getProperty("cas.server.url");
            String casProjectUrl = propertiesLoader.getProperty("cas.project.url");
            // 系统启动时注册cas登出filter
            FilterRegistration casSingleSignOutFilter = servletContext.addFilter("CAS Single Sign Out Filter", SingleSignOutFilter.class);
            casSingleSignOutFilter.setInitParameter("casServerUrlPrefix", casServerUrl+"/");
            casSingleSignOutFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class) , true, "/*");
            //注册Apache Shiro Filter
            FilterRegistration apacheShiroFilter = servletContext.addFilter("shiroFilter",DelegatingFilterProxy.class);
            apacheShiroFilter.setInitParameter("targetFilterLifecycle","true");
            apacheShiroFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class) , true, "/*");
            //注册CAS Authentication Filter
            FilterRegistration casAuthenticationFilter = servletContext.addFilter("CAS Authentication Filter",AuthenticationFilter.class);
            Map casAuthFilterParas = new HashMap();
            casAuthFilterParas.put("casServerLoginUrl",casServerUrl+"/login");
            //casAuthFilterParas.put("renew","false");
            casAuthFilterParas.put("gateway","false");
            casAuthFilterParas.put("serverName",casProjectUrl);
            casAuthFilterParas.put("ignorePattern","/statistic/*|/static/*|/js/*|/img/*|/views/*|/css/*|webservice/*|/cas/changeCenter/*");
            casAuthenticationFilter.setInitParameters(casAuthFilterParas);
            casAuthenticationFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class) , true, "/*");
            //注册CAS Validation Filter
            FilterRegistration casValidationFilter = servletContext.addFilter("CAS Validation Filter",Cas20ProxyReceivingTicketValidationFilter.class);
            Map casValidationFilterParas = new HashMap();
            casValidationFilterParas.put("casServerUrlPrefix",casServerUrl);
            casValidationFilterParas.put("serverName",casProjectUrl);
            casValidationFilterParas.put("useSession","true");
            casValidationFilterParas.put("redirectAfterValidation","true");
            casValidationFilterParas.put("encoding","UTF-8");
            casValidationFilter.setInitParameters(casValidationFilterParas);
            casValidationFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class) , true, "/*");
            //注册CAS HttpServletRequest Wrapper Filter
            FilterRegistration casHttpWrapperFilter = servletContext.addFilter("CAS HttpServletRequest Wrapper Filter",HttpServletRequestWrapperFilter.class);
            casHttpWrapperFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class) , true, "/*");
            //注册CAS Assertion Thread Local Filter
            FilterRegistration casAssertionFilter = servletContext.addFilter("CAS Assertion Thread Local Filter",AssertionThreadLocalFilter.class);
            casAssertionFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class) , true, "/*");
        }else if("false".equals(casStartup)){
            //注册Apache Shiro Filter
            FilterRegistration apacheShiroFilter = servletContext.addFilter("shiroFilter",DelegatingFilterProxy.class);
            apacheShiroFilter.setInitParameter("targetFilterLifecycle","true");
            apacheShiroFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class) , true, "/*");
        }

        super.onStartup(servletContext);
    }

    @Override
    protected FilterRegistration.Dynamic registerServletFilter(ServletContext servletContext, Filter filter) {
        return super.registerServletFilter(servletContext, filter);
    }
}

读取项目的配置文件,当配置文件cas.startup配置为true时,加载cas客户端的而配置,从未集成cas,当配置为false时,不去加载cas的配置,还是原来的项目,并没有集成cas.

#单点cas开关设置  开启设为true,关闭设为false
cas.startup=true

cas.server.url,cas.project.url都只在配置文件中配一遍即可。

#单点登录CAS设置
cas.server.url=http://localhost:8081/cas
cas.project.url=http://localhost:8888

通过编码方式配置web.xml,简化了配置,项目想集成或者不集成cas,只需要在配置文件中配置开关即可,不需要再去动web。xml文件。

你可能感兴趣的:(cas单点登录)