前两篇介绍了Apereo CAS以及服务器端的安装,但还不够完整,服务端尚未Application真正用起来呢!这篇文章将介绍怎么用起来
客户端咱们想要与Apereo CAS作什么集成呢?回顾一下Apereo CAS是作什么的?Apereo CAS的一个功能就是单点登陆,统一的登陆登出接口与页面,让系统中的模块只须要关注在业务点,而把安全认证的功能交给统一认证来作。因此客户端的集成主要是单点登陆的集成,客户端指定须要作安全认证的页面,而后Apereo CAS的安全包检测校验用户登陆状况,并自动与CAS登陆页面进行跳转交互。java
Apereo CAS提供了Springboot的包,可让咱们的集成些微方便了那么一丢丢!首先咱们建立一个Springboot的application,里面带了Apereo CAS start的依赖spring
<dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-cas</artifactId> </dependency>
同时在application.properties文件里面指定启动的端口 server.port = 9000
json
有了Apereo CAS的包以后,咱们就能够进行代码的配置。客户端的配置按照SpringSecurity的安全检验流程进行的:安全
AuthenticationEntryPoint
被触发了,把用户重定向到配置好的CAS登陆页面https://localhost:6443/casCasAuthenticationFilter
一直在监听/login/cas
这个路径,当发现有请求后,它会触发CasTicketValidator,由CasTickerValidator检验ticket的有效性下面代码大体描述了这个过程:服务器
@Bean public ServiceProperties serviceProperties() { ServiceProperties serviceProperties = new ServiceProperties(); serviceProperties.setService("http://localhost:9000/login/cas"); serviceProperties.setSendRenew(false); return serviceProperties; } @Bean @Primary public AuthenticationEntryPoint authenticationEntryPoint( ServiceProperties sP) { CasAuthenticationEntryPoint entryPoint = new CasAuthenticationEntryPoint(); entryPoint.setLoginUrl("https://localhost:6443/cas/login"); entryPoint.setServiceProperties(sP); return entryPoint; } @Bean public TicketValidator ticketValidator() { return new Cas30ServiceTicketValidator( "https://localhost:6443/cas"); } @Bean public CasAuthenticationProvider casAuthenticationProvider() { CasAuthenticationProvider provider = new CasAuthenticationProvider(); provider.setServiceProperties(serviceProperties()); provider.setTicketValidator(ticketValidator()); provider.setUserDetailsService( s -> new User("casuser", "Mellon", true, true, true, true, AuthorityUtils.createAuthorityList("ROLE_ADMIN"))); provider.setKey("CAS_PROVIDER_LOCALHOST_9000"); return provider; }
@EnableWebSecurity @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { private AuthenticationProvider authenticationProvider; private AuthenticationEntryPoint authenticationEntryPoint; private SingleSignOutFilter singleSignOutFilter; private LogoutFilter logoutFilter; @Autowired public SecurityConfig(CasAuthenticationProvider casAuthenticationProvider, AuthenticationEntryPoint eP, LogoutFilter lF , SingleSignOutFilter ssF ) { this.authenticationProvider = casAuthenticationProvider; this.authenticationEntryPoint = eP; this.logoutFilter = lF; this.singleSignOutFilter = ssF; } // ... @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.authenticationProvider(authenticationProvider); } @Override protected AuthenticationManager authenticationManager() throws Exception { return new ProviderManager(Arrays.asList(authenticationProvider)); } @Bean public CasAuthenticationFilter casAuthenticationFilter(ServiceProperties sP) throws Exception { CasAuthenticationFilter filter = new CasAuthenticationFilter(); filter.setServiceProperties(sP); filter.setAuthenticationManager(authenticationManager()); return filter; } }
下面这个文件配置了application中全部/secured/*
,login
的URL都是受保护资源,都要通过CAS认证过才能够访问:app
@EnableWebSecurity @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .regexMatchers("/secured.*", "/login") .authenticated() .and() .authorizeRequests() .regexMatchers("/") .permitAll() .and() .httpBasic() .authenticationEntryPoint(authenticationEntryPoint); } // ... }
跟全部统一认证平台同样,全部application想要跟CAS作集成的,都须要在CAS配置相应的参数才可使用。Apereo CAS提供了不少配置的方式,有YML,JSON, MongoDB以及其余(可查官网)。但高度自由的CAS一如既往的,没有提供可视化操做的界面。好比咱们采用JSON的方式。首先咱们须要通知Apereo CAS咱们采用的是JSON的方式,并通知JSON文件的路径在哪里框架
cas.serviceRegistry.initFromJson=true cas.serviceRegistry.config.location=classpath:/services
而后咱们在这个目录里面,建立一个对应的JSON文件,保存咱们的客户端信息,为了方面管理,建议文件名为 application_id.json, 好比"secureApp_9991.json", 内容以下:ide
{ "@class" : "org.apereo.cas.services.RegexRegisteredService", "serviceId" : "^http://localhost:9000/login/cas", "name" : "CAS Spring Secured App", "description": "This is a Spring App that usses the CAS Server for it's authentication", "id" : 19991, "evaluationOrder" : 1 }
第一次配置从JSON加载客户端配置的话,须要重启Apereo CAS。以后再加新的客户端的话就不用再重启,Apereo CAS会自动监测这个文件夹的变更学习
至此咱们对于Apereo CAS就有了一个稍微完整一点点的了解,从服务端安装部署,到配置,以及客户端如何集成等。但从这个短期的学习来看,若是企业已经重度使用了Apereo CAS,那相信它能够很好地服务支撑企业的应用。但若是是新的项目,特别是项目周期比较紧张的项目,而且团队以前没有对统一认证有技术积累的话,不是很建议采用Apereo CAS,这些细微的配置以及无所不在的隐藏功能,会让你给项目经理催死的! 后面我会介绍另一个统一认证的框架,我的感受能弥补Apereo CAS的短板的ui