搜索
您的当前位置:首页正文

SSO之CAS

来源:二三娱乐

为什么要使用CAS

当多个系统之间存在联系时,此时问题来了,是需要单个单个认证呢?还是只需要认证其中一个即可访问其他的呢?当然会选择后者,这样才是人的本性,懒惰是技术进步的前提嘛。此时会有疑问信任信息如何产生?如何存储信任信息?对于产生信任信息不难,后台服务器如直接生成随机数,而存储信任信息解决方案:cookie、jsonp、页面重定向等。

CAS是什么

先说一下单点登录SSO(Single Sign On)即多个系统只需登录一次,无需重复登录即可访问对方,而CAS只是其解决方案,CAS大概流程原理如:

参考

需要注意是与单一登录(同一系统,同一用户在同一时间内只能有一个会话(即一个用户只有一个在使用的浏览器)区别。

如何使用CAS

经过上述一番不详解说,大概知道需要CAS服务器CAS客户端,而对于这一切冥冥之中已经被Apereo和Spring所安排好,我们只需要拿来使用即可,创造的事是上帝的事,我们只是“程序猿”。


搭建CAS服务器:(以CAS5.x为例)

2)导出证书 keytool -export -alias 别名 -keystore 文件名 -file 文件名.cer -storepass 密码

3)客户端配置:为客户端的JVM导入密钥(将服务器下发的证书导入到JVM中) keytool -import -trustcacerts -alias 别名 -keystore "$JAVA_HOME/jre/lib/security/cacerts" -file 文件名.cer -storepass changeit 注意:导入JVM 密码默认的是changeit,其他密码会报keytool 错误: java.io.IOException: Keystore was tampered with, or password was incorrect 若不配,则访问时报sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

Springboot:application.yml(最好yml,而不要properties)

server:

  context-path: /cas

  port: 9000

  ssl:

    key-store: "classpath:bugbiu.jks"

    key-store-password: bugbiu

    key-password: bugbiu

    #key-alias: cas #目前支持3中方式:1、CAS;2、CAS3;3、SAML

## # CAS Authentication Credentials #

cas:

  authn:

    accept:

      users: "admin::admin"     

(3)cas-overlay-template-5.x下 build run (最好不要使用build bootrun,具体build help看一下)

CAS客户端:(以Springboot为例)

(1)pom:

    <dependency> <groupId>net.unicon.cas</groupId> <artifactId>cas-client-autoconfig-support</artifactId> <version>2.2.0-GA</version> </dependency>

(2)application.yml:

server:

  port: 9100

  #配置ssl

  ssl:

  #enabled: true

  key-store: "classpath:bugbiu.jks"

  key-store-password: bugbiu

  key-password: bugbiu

  #key-alias: cas     #server.ssl.keyStoreType=PKCS12

cas:

  validation-type: cas

package com.bugbiu.config; import org.apache.catalina.Context; import org.apache.catalina.connector.Connector; import org.apache.tomcat.util.descriptor.web.SecurityCollection; import org.apache.tomcat.util.descriptor.web.SecurityConstraint; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;

/**

* @author bugbiu

* 2019/5/10 11:26

* @version 1.0

*/

@Configuration

public class Http2HttpsConfig {

    @Value("${server.port}")

    private int port;

    @Bean

    public TomcatServletWebServerFactory servletContainer(Connector connector) {         //Springboot2以前TomcatEmbeddedServletContainerFactory、EmbeddedServletContainerCustomizer         TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {

            @Override

            protected void postProcessContext(Context context) {

                SecurityConstraint constraint = new SecurityConstraint();                 constraint.setUserConstraint("CONFIDENTIAL");

                SecurityCollection collection = new SecurityCollection();

                collection.addPattern("/*");

                constraint.addCollection(collection);

                context.addConstraint(constraint);

}                                                                                                                                              };                                                                            tomcat.addAdditionalTomcatConnectors(connector);                                                      return tomcat;                                                                                                                          }                                                                                                                                      @Bean

        connector.setRedirectPort(port);

        return connector;

    }

对于上述只是CAS使用,如果大家想深入研究CAS框架实现,可以一起探讨探讨,个人使用研究心得,若有错误欢迎指出,谢谢^_^

Top