当前位置:  开发笔记 > 编程语言 > 正文

使用不带web.xml的Spring(Boot)配置CAS

如何解决《使用不带web.xml的Spring(Boot)配置CAS》经验,为你挑选了1个好方法。

我正在使用Boot将WebApp从Spring 3移植到Spring 4.

下面的原始web.xml


 org.jasig.cas.client.session.SingleSignOutHttpSessionListener



 CAS Authentication Filter
 org.jasig.cas.client.authentication.AuthenticationFilter
 
    casServerLoginUrl
    https://casserver/login
 
 
    serverName
    http://myapp
 



 CAS Validation Filter
 org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter
 
    casServerUrlPrefix
    https://casserver/login
 
 
    serverName
    http://myapp
 
 
    encoding
    UTF-8
 



 CAS Single Sign Out Filter
 org.jasig.cas.client.session.SingleSignOutFilter



 CAS Single Sign Out Filter
 /*



 CAS Authentication Filter
 /*


 CAS Validation Filter
 /*



 90



 javax.servlet.jsp.jstl.fmt.localizationContext
 messages

我正在尝试在AppConfic类中注册所有过滤器,监听器和映射(下面并没有完成,因为它已经无法正常工作......)

@Configuration
public class MyWebApplicationInitializer implements ServletContextInitializer  {

    @Override
    public void onStartup(ServletContext servletContext) {
        servletContext.addListener(new SingleSignOutHttpSessionListener());

        Cas20ProxyReceivingTicketValidationFilter cas20 = new Cas20ProxyReceivingTicketValidationFilter();
        cas20.setServerName("http://myapp");

        AuthenticationFilter authenticationFilter = new AuthenticationFilter();
        authenticationFilter.setCasServerLoginUrl("https://casserver");
        authenticationFilter.setServerName("http://myapp");

        servletContext.addFilter("CAS Authentication Filter", authenticationFilter);
        servletContext.addFilter("CAS Validation Filter", cas20);
        servletContext.addFilter("CAS Single Sign Out Filter", new SingleSignOutFilter());
    }
}

1/CAS20实例化问题

我无法定义cas20 casServerUrlPrefix ......没有setter?

2/AuthenticationFilter问题

即使定义了serverName,在启动期间也会发生以下错误:

java.lang.IllegalArgumentException: serverName or service must be set.
    at org.jasig.cas.client.util.CommonUtils.assertTrue(CommonUtils.java:116) ~[cas-client-core-3.2.1.jar:3.2.1]
    at org.jasig.cas.client.util.AbstractCasFilter.init(AbstractCasFilter.java:103) ~[cas-client-core-3.2.1.jar:3.2.1]
    at org.jasig.cas.client.authentication.AuthenticationFilter.init(AuthenticationFilter.java:96) ~[cas-client-core-3.2.1.jar:3.2.1]
    at org.jasig.cas.client.util.AbstractCasFilter.init(AbstractCasFilter.java:84) ~[cas-client-core-3.2.1.jar:3.2.1]
    at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279) ~[tomcat-embed-core-8.0.28.jar:8.0.28]
    at org.apache.catalina.core.ApplicationFilterConfig.(ApplicationFilterConfig.java:109) ~[tomcat-embed-core-8.0.28.jar:8.0.28]

是否有可能在ServletContextInitializer内部定义这种过滤器?我是否必须加载xml(dispatcher-servlet.xml之类的?)



1> M. Deinum..:

我强烈建议阅读Servlet 3.0规范,以及如果你知道自己在做什么,web.xml可以完全移植的工作原理.您只需要在注册时指定init参数.


 CAS Validation Filter
 org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter
 
    casServerUrlPrefix
    https://casserver/login
 
 
    serverName
    http://myapp
 
 
    encoding
    UTF-8
 

有了这个


 CAS Validation Filter
 /*

将导致Java中的大致以下内容并使用WebApplicationInitializer.

@Configuration
public class MyWebApplicationInitializer implements ServletContextInitializer  {

    @Override
    public void onStartup(ServletContext sc) {
        FilterRegistration.Dynamic cas20Registration = sc.addFilter("CAS Validation Filter", Cas20ProxyReceivingTicketValidationFilter.class);
        cas20Registration.setInitParameter("casServerUrlPrefix casServerUrlPrefix", "https://casserver/login");
        cas20Registration.setInitParameter("serverName", "http://myapp");
        cas20Registration.setInitParameter("encoding", "UTF-8");
        cas20Registration.addMappingForUrlPatterns(null, false, "/*");
    }
}

以上是xml与java的一对一翻译.哪个应该可以让您了解其他过滤器.

但是,您使用Spring Boot会更容易.只需创建一个@Bean返回FilterRegistrationBean所需过滤器的方法.另请参阅"Spring Boot参考指南"中的相应部分.

@Bean
public FilterRegistrationBean cas20Registration() {
    FilterRegistrationBean cas20 = new FilterRegistrationBean();
    cas20.setFilter(new Cas20ProxyReceivingTicketValidationFilter());
    cas20.addUrlPatterns("/*");
    cas20.addInitParameter("casServerUrlPrefix casServerUrlPrefix", "https://casserver/login");
    cas20.addInitParameter("serverName", "http://myapp");
    cas20.addInitParameter("encoding", "UTF-8");        
    return cas20;
}

您可以对其他过滤器,侦听器和所需组件执行相同的操作.

但有一件事你不能用基于java的配置,那就是设置会话超时值.但是,您只需将其移动到application.properties并添加server.session.timeout属性即可.您将需要使用60多个值,因为在web.xml中它是几分钟,在属性文件中它应该是以秒为单位.

server.session.timeout=5400 # 90 minutes

推荐阅读
sx-March23
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有