Apereo CAS 5.0.X 启用OAuth2服务时的一个坑

来源:互联网 发布:开源财务系统 php 编辑:程序博客网 时间:2024/06/07 15:35

问题描述

引入cas-server-support-oauth-webflow依赖,启用oauth2服务,保存service先后使用了JSON和JPA的方式(如果不了解,可以参考http://blog.csdn.net/xichenguan/article/details/60770491
补充一下科学知识)。在使用JSON的时候,Tomcat启动,CAS部署过程中,在配置的目录下面会生成一个名为[OAuthCallbackurl-随机数字]格式的文件。内容如下:

{  @class: org.apereo.cas.support.oauth.services.OAuthCallbackAuthorizeService  serviceId: https://cas.example.org:8443/cas/oauth2.0/callbackAuthorize.*  name: OAuth Callback url  id: 32577569223569  description: OAuth Wrapper Callback Url  evaluationOrder: -2147483648  logoutType: BACK_CHANNEL  attributeReleasePolicy:  {    @class: org.apereo.cas.services.ReturnAllAttributeReleasePolicy    principalAttributesRepository:    {      @class: org.apereo.cas.authentication.principal.DefaultPrincipalAttributesRepository      expiration: 2      timeUnit: HOURS    }    authorizedToReleaseCredentialPassword: false    authorizedToReleaseProxyGrantingTicket: false  }  multifactorPolicy:  {    @class: org.apereo.cas.services.DefaultRegisteredServiceMultifactorPolicy    failureMode: CLOSED  }  accessStrategy:  {    @class: org.apereo.cas.services.DefaultRegisteredServiceAccessStrategy    enabled: true    ssoEnabled: true    requireAllAttributes: true    caseInsensitive: false  }}

用JPA方式验证,同样是在数据库中生成一条同样含义的记录。

问题处理

第一步

每次生成的时候,大部分内容都是固定不变的,想到这些中肯定有部分或者全部都是静态变量,于是挨个找,在CasOAuthConfiguration中找到下面一段代码:

@PostConstruct    public void initializeServletApplicationContext() {        final String oAuthCallbackUrl = casProperties.getServer().getPrefix() + BASE_OAUTH20_URL + '/'                + OAuthConstants.CALLBACK_AUTHORIZE_URL_DEFINITION;        final Service callbackService = this.webApplicationServiceFactory.createService(oAuthCallbackUrl);        final RegisteredService svc = servicesManager.findServiceBy(callbackService);        if (svc == null || !svc.getServiceId().equals(oAuthCallbackUrl)) {            final OAuthCallbackAuthorizeService service = new OAuthCallbackAuthorizeService();            service.setName("OAuth Callback url");            service.setDescription("OAuth Wrapper Callback Url");            service.setServiceId(oAuthCallbackUrl);            service.setEvaluationOrder(Integer.MIN_VALUE);            service.setAttributeReleasePolicy(new ReturnAllAttributeReleasePolicy());            servicesManager.save(service);            servicesManager.load();        }        this.validationServiceSelectionStrategies.add(0, oauth20ValidationServiceSelectionStrategy());    }

问题就出在casProperties.getServer().getPrefix()上了,这是一个嵌套的属性配置文件,里面的字段是这么定义的:

public class ServerProperties {    private int connectionTimeout = 20000;    private String name = "https://cas.example.org:8443";    private String prefix = name.concat("/cas");    private Ajp ajp = new Ajp();    private Http http = new Http();    private ExtendedAccessLog extAccessLog = new ExtendedAccessLog();    }

就是这个prefix字段了,他使用了name变量,也就是配置一下这个name变量就行了。

第二步

于是在application.properties文件中按照命名空间添加下面几个配置:

cas.server.name=https://asdf.com:8443cas.server.prefix=https://asdf.com:8443/cas

asdf.com是我测试CAS过程中,部署CAS服务的操作系统的域名,此域名添加到了需要访问CAS服务器的操作系统的hosts文件里。

第三步

远程debug了好久,发现还是不行,想到可能是其他地方也有配置,被覆盖了,就在项目底下,各种找,完了一会linux命令之后,已经绝望了。

第四步

折腾了两个多小时,挨个文件看,突然发现bootstrap.properies文件中有这么个配置:

spring.cloud.config.server.native.searchLocations=file:///etc/cas/config

疯了一样找这个目录,果然里面有个cas.properties文件:

cas.server.name: https://cas.example.org:8443cas.server.prefix: https://cas.example.org:8443/cascas.adminPagesSecurity.ip=127\.0\.0\.1logging.config: file:/etc/cas/config/log4j2.xml# cas.serviceRegistry.config.location: classpath:/services

呵呵,猜对了一半,是被覆盖了。

8 0
原创粉丝点击