我是靠谱客的博主 安静网络,最近开发中收集的这篇文章主要介绍cas 5.1.x 关于配置集群代理的那些坑-2,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Apereo cas 的代理功能是非常常用和重要的,尤其是企业内部系统,各个系统间的无缝安全切换全部依靠这个代理功能,cas从4升级到5的转变就是配置更新可以及时生效,完全取消了xml等诸多的配置。 在配置代理功能时遇到了不小的坑,千万不要依赖官方文档!!,这里有很多次的失败和尝试,希望他人千万不要掉进去。

官方说明,举例如下:

Regex

A proxy policy that only allows proxying to PGT urls that match the specified regex pattern.

{
  "@class" : "org.apereo.cas.services.RegexRegisteredService",
  "serviceId" : "testId",
  "name" : "testId",
  "id" : 1,
  "proxyPolicy" : {
    "@class" : "org.apereo.cas.services.RegexMatchingRegisteredServiceProxyPolicy",
    "pattern" : "^https?://.*"
  }
}

我们重点看这个参数:pattern ,依据官方给的样例是个正则表达式字符串,如果你按照这个配置就惨了,

启动cas时你会遇到这个错误:

Caused by: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [java.util.regex.Pattern]
	at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:324)
	at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:206)
	at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:187)
	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.getPotentiallyConvertedSimpleRead(MappingMongoConverter.java:829)
	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readValue(MappingMongoConverter.java:1220)
	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.access$200(MappingMongoConverter.java:85)
	at org.springframework.data.mongodb.core.convert.MappingMongoConverter$MongoDbPropertyValueProvider.getPropertyValue(MappingMongoConverter.java:1166)
	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.getValueInternal(MappingMongoConverter.java:877)
	at org.springframework.data.mongodb.core.convert.MappingMongoConverter$1.doWithPersistentProperty(MappingMongoConverter.java:290)
	at org.springframework.data.mongodb.core.convert.MappingMongoConverter$1.doWithPersistentProperty(MappingMongoConverter.java:278)
	at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:330)
	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:278)
	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:238)
	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readValue(MappingMongoConverter.java:1218)

如果你把pattern字段取消或者改名,那么你会遇到这个错误:

java.lang.NullPointerException: null
	at org.apereo.cas.services.RegexMatchingRegisteredServiceProxyPolicy.hashCode(RegexMatchingRegisteredServiceProxyPolicy.java:62) ~[cas-server-core-services-5.1.2.jar!/:5.1.2]
	at org.apache.commons.lang3.builder.HashCodeBuilder.append(HashCodeBuilder.java:851) ~[commons-lang3-3.5.jar!/:3.5]
	at org.apereo.cas.services.AbstractRegisteredService.hashCode(AbstractRegisteredService.java:260) ~[cas-server-core-services-5.1.2.jar!/:5.1.2]
	at org.apache.commons.lang3.builder.HashCodeBuilder.append(HashCodeBuilder.java:851) ~[commons-lang3-3.5.jar!/:3.5]
	at org.apereo.cas.authentication.HttpBasedServiceCredential.hashCode(HttpBasedServiceCredential.java:91) ~[cas-server-core-authentication-5.1.2.jar!/:5.1.2]
	at java.util.HashMap.hash(HashMap.java:338) ~[?:1.8.0_121]
	at java.util.HashMap.put(HashMap.java:611) ~[?:1.8.0_121]
	at java.util.HashSet.add(HashSet.java:219) ~[?:1.8.0_121]
	at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169) ~[?:1.8.0_121]
	at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175) ~[?:1.8.0_121]
	at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948) ~[?:1.8.0_121]
	at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) ~[?:1.8.0_121]
	at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) ~[?:1.8.0_121]
	at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) ~[?:1.8.0_121]
	at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[?:1.8.0_121]
	at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) ~[?:1.8.0_121]
	at org.apereo.cas.authentication.AuthenticationTransaction.sanitizeCredentials(AuthenticationTransaction.java:99) ~[cas-server-core-api-authentication-5.1.2.jar!/:5.1.2]
	at org.apereo.cas.authentication.AuthenticationTransaction.wrap(AuthenticationTransaction.java:50) ~[cas-server-core-api-authentication-5.1.2.jar!/:5.1.2]
	at org.apereo.cas.authentication.DefaultAuthenticationSystemSupport.handleAuthenticationTransaction(DefaultAuthenticationSystemSupport.java:54) ~[cas-server-core-authentication-5.1.2.jar!/:5.1.2]


好吧,我们回到 org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [java.util.regex.Pattern] 这个错上上,一看错误就知道属性值转换器问题,查看源代码并测试如下:

在这个包下 package org.apereo.cas.util.spring 有个自定义转换器Converters.java,我们添加一个Pattern转换器试试:

   /**
     * string to Pattern converter
     * turns a {@link Pattern} into a formatted string.
     */
    public static class StringToPatternConverter implements Converter<String, Pattern> {
        @Override
        public Pattern convert(final String pgtUrlPattern) {
            return Pattern.compile(pgtUrlPattern, Pattern.CASE_INSENSITIVE);
        }
    }

然后编译后,运行debug,查看转换器加载完毕了,但是错误依然存在,转换器没起作用。好吧,我忍了。 这个cas我采用的是mongodb作为存储,那么这个注册服务的驱动用的是spring-data-mongodb组件,查看了官方文档,发现可以自定义转换器;好吧,弄它试试! 在包 package org.apereo.cas.services.convert 下面建立一个StringToPatternConverter.java,然后放入org.apereo.cas.config.MongoDbServiceRegistryConfiguration中的这个方法中(其实官方的注释上标明了是排除转换器):

   @Override
    public CustomConversions customConversions() {
        return new CustomConversions(Arrays.asList(
                new BaseConverters.LoggerConverter(),
                new BaseConverters.ClassConverter(),
                new BaseConverters.CommonsLogConverter(),
                new BaseConverters.PersonAttributesConverter(),
                new BaseConverters.CacheLoaderConverter(),
                new BaseConverters.RunnableConverter(),
                new BaseConverters.ReferenceQueueConverter(),
                new BaseConverters.ThreadLocalConverter(),
                new BaseConverters.CertPathConverter(),
                new BaseConverters.CacheConverter()
        ));
    }

编译后启动测试,发现问题依然存在,弯路走了不少。

原来错误的是这个样子的:

    "proxyPolicy" : {
        "_class" : "org.apereo.cas.services.RegexMatchingRegisteredServiceProxyPolicy",
        "pattern" : "^(http|https)?://.*/eccl/proxyUrl.?.*"
    },

最终正确的是这个样子的:

    "proxyPolicy" : {
        "_class" : "org.apereo.cas.services.RegexMatchingRegisteredServiceProxyPolicy",
        "pattern" : /^(http|https)?://.*/eccl/proxyUrl.?.*/i
    },

为了取得真经,中间饶了十八弯,其中包含配置控制面板dashboard,包含配置CAS server that management app,中间的各种配置参数的组合,头大的很,主要是官方没有明确的每个参数的说明和例子,你自己猜着看。

转载于:https://my.oschina.net/u/1465974/blog/1510344

最后

以上就是安静网络为你收集整理的cas 5.1.x 关于配置集群代理的那些坑-2的全部内容,希望文章能够帮你解决cas 5.1.x 关于配置集群代理的那些坑-2所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(41)

评论列表共有 0 条评论

立即
投稿
返回
顶部