CAS单点登录:基础框架搭建(一)


1.引言

在多服务统一帐号的应用集中,单点登录是必不可少的。CAS就是成熟的单点登录框架之一。

Github地址:https://github.com/apereo/cas。

现在我们就通过一系列快速简单的构建方式实现一个简单的单点登录系统集。

首先下载cas-overlay-template:https://github.com/apereo/cas-overlay-template ,这里我们使用5.3.x版本

# 拉去代码
git clone https://github.com/apereo/cas-overlay-template.git

# 进入文件夹
cd cas-overlay-template

# 切换分支
git checkout 5.3

2.准备工作

2.1.配置域名映射

打开host文件,配置cas域名映射。

windows:C:\Windows\System32\drivers\etc,linux:/etc/host

2.2.配置Keystore

配置keystore的目的是让tomcat支持https。

生成Keystore

keytool -genkey -alias tomcat -keyalg RSA -validity 3650 -keystore D:/keystore/tomcat.keystore

-alias tomcat :表示秘钥库的别名是tomcat,实际操作都用别名识别,所以这个参数很重要。你也可以去其他的别名。

-validity 3650:表示证书有效期10年。

-keystore D:/keystore/tomcat.keystore:指定keystore的存储路径为D:/keystore,名称为tomcat.keystore

秘钥库口令: changeit,这里建议输入changeit,因为证书库cacerts的缺省口令为changeit,这里方便统一。

名字与姓氏输入服务器域名。

其它回车,最后如果显示正确 输入 ‘y’ 就行了。

tomcat秘钥口令采用与秘钥库相同,因此也回车。

查看密匙库文件内容

keytool -list -keystore D:/keystore/tomcat.keystore

根据keystore生成crt文件

keytool -export -alias tomcat -file D:/keystore/tomcat.cer -keystore D:/keystore/tomcat.keystore -validity 3650

信任授权文件到jdk

keytool -import -keystore D:/java/jdk1.8/jre/lib/security/cacerts -file D:/keystore/tomcat.cer -alias tomcat -storepass changeit

证书库cacerts的缺省口令为changeit ,这也是为什么我上面的密码都是用的它,防止混淆,直接都设成一样的。

删除授权文件

keytool -delete -alias tomcat -keystore D:/java/jdk1.8/jre/lib/security/cacerts

查看cacerts中证书

keytool -list -v -keystore D:/java/jdk1.8/jre/lib/security/cacerts

2.3.修改tomcat的配置文件server.xml

打开tomcat安装目录的/conf/server.xml,添加以下内容

<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
           maxThreads="200" SSLEnabled="true" scheme="https"
           secure="true" clientAuth="false" sslProtocol="TLS"
           keystoreFile="D:\keystore\tomcat.keystore"
           keystorePass="changeit"/>

2.4.让浏览器信任证书

 

3.使用Overlay自定义服务端

overlay可以把多个项目war合并成为一个项目,并且如果项目存在同名文件,那么主项目中的文件将覆盖掉其他项目的同名文件。使用maven 的Overlay配置实现无侵入的改造cas。

3.1.打包Overlay

mvn clean package

执行完成后,在target下会生成cas.war

将war包进行解压

3.2.新建项目cas-server

pom.xml

在解压的war包中,拷贝pom.xml,路径:/cas/META-INF/maven/org.apereo.cas/cas-overlay

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.fdzanggroupId>
    <artifactId>cas-serverartifactId>
    <version>1.0-SNAPSHOTversion>

    <build>
        <plugins>
            <plugin>
                <groupId>com.rimerosolutions.maven.pluginsgroupId>
                <artifactId>wrapper-maven-pluginartifactId>
                <version>0.0.5version>
                <configuration>
                    <verifyDownload>trueverifyDownload>
                    <checksumAlgorithm>MD5checksumAlgorithm>
                configuration>
            plugin>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <version>${springboot.version}version>
                <configuration>
                    <mainClass>${mainClassName}mainClass>
                    <addResources>trueaddResources>
                    <executable>${isExecutable}executable>
                    <layout>WARlayout>
                configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackagegoal>
                        goals>
                    execution>
                executions>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-war-pluginartifactId>
                <version>2.6version>
                <configuration>
                    <warName>caswarName>
                    <failOnMissingWebXml>falsefailOnMissingWebXml>
                    <recompressZippedFiles>falserecompressZippedFiles>
                    <archive>
                        <compress>falsecompress>
                        <manifestFile>${manifestFileToUse}manifestFile>
                    archive>
                    <overlays>
                        <overlay>
                            <groupId>org.apereo.casgroupId>
                            <artifactId>cas-server-webapp${app.server}artifactId>
                        overlay>
                    overlays>
                configuration>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>3.3version>
            plugin>
        plugins>
        <finalName>casfinalName>
    build>

    <properties>
        <cas.version>5.3.14cas.version>
        <springboot.version>1.5.18.RELEASEspringboot.version>
        
        <app.server>-tomcatapp.server>

        <mainClassName>org.springframework.boot.loader.WarLaunchermainClassName>
        <isExecutable>falseisExecutable>
        <manifestFileToUse>
            ${project.build.directory}/war/work/org.apereo.cas/cas-server-webapp${app.server}/META-INF/MANIFEST.MF
        manifestFileToUse>

        <maven.compiler.source>1.8maven.compiler.source>
        <maven.compiler.target>1.8maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>

    <repositories>
        <repository>
            <id>sonatype-releasesid>
            <url>http://oss.sonatype.org/content/repositories/releases/url>
            <snapshots>
                <enabled>falseenabled>
            snapshots>
            <releases>
                <enabled>trueenabled>
            releases>
        repository>
        <repository>
            <id>sonatype-snapshotsid>
            <url>https://oss.sonatype.org/content/repositories/snapshots/url>
            <snapshots>
                <enabled>trueenabled>
            snapshots>
            <releases>
                <enabled>falseenabled>
            releases>
        repository>
        <repository>
            <id>shibboleth-releasesid>
            <url>https://build.shibboleth.net/nexus/content/repositories/releasesurl>
        repository>
    repositories>

    <profiles>
        <profile>
            <activation>
                <activeByDefault>trueactiveByDefault>
            activation>
            <id>defaultid>
            <dependencies>
                <dependency>
                    <groupId>org.apereo.casgroupId>
                    <artifactId>cas-server-webapp${app.server}artifactId>
                    <version>${cas.version}version>
                    <type>wartype>
                    <scope>runtimescope>
                dependency>
                
            dependencies>
        profile>

        <profile>
            <activation>
                <activeByDefault>falseactiveByDefault>
            activation>
            <id>execid>
            <properties>
                <mainClassName>org.apereo.cas.web.CasWebApplicationmainClassName>
                <isExecutable>trueisExecutable>
                <manifestFileToUse>manifestFileToUse>
            properties>
            <build>
                <plugins>
                    <plugin>
                        <groupId>com.soebes.maven.pluginsgroupId>
                        <artifactId>echo-maven-pluginartifactId>
                        <version>0.3.0version>
                        <executions>
                            <execution>
                                <phase>prepare-packagephase>
                                <goals>
                                    <goal>echogoal>
                                goals>
                            execution>
                        executions>
                        <configuration>
                            <echos>
                                <echo>Executable profile to make the generated CAS web application executable.echo>
                            echos>
                        configuration>
                    plugin>
                plugins>
            build>
        profile>

        <profile>
            <activation>
                <activeByDefault>falseactiveByDefault>
            activation>
            <id>bootifulid>
            <properties>
                <app.server>-tomcatapp.server>
                <isExecutable>falseisExecutable>
            properties>
            <dependencies>
                <dependency>
                    <groupId>org.apereo.casgroupId>
                    <artifactId>cas-server-webapp${app.server}artifactId>
                    <version>${cas.version}version>
                    <type>wartype>
                    <scope>runtimescope>
                dependency>
            dependencies>
        profile>

        <profile>
            <activation>
                <activeByDefault>falseactiveByDefault>
            activation>
            <id>pgpid>
            <build>
                <plugins>
                    <plugin>
                        <groupId>com.github.s4u.pluginsgroupId>
                        <artifactId>pgpverify-maven-pluginartifactId>
                        <version>1.1.0version>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>checkgoal>
                                goals>
                            execution>
                        executions>
                        <configuration>
                            <pgpKeyServer>hkp://pool.sks-keyservers.netpgpKeyServer>
                            <pgpKeysCachePath>${settings.localRepository}/pgpkeys-cachepgpKeysCachePath>
                            <scope>testscope>
                            <verifyPomFiles>trueverifyPomFiles>
                            <failNoSignature>falsefailNoSignature>
                        configuration>
                    plugin>
                plugins>
            build>
        profile>
    profiles>
project>

其他文件

META-INF/spring.factories、application.properties、log4j2.xml,路径:cas\WEB-INF\classes

最终项目目录:

 修改application.properties

server.ssl.enabled=true
server.ssl.key-store=file:D:/keystore/tomcat.keystore
server.ssl.key-store-password=changeit
server.ssl.key-password=changeit
server.ssl.keyAlias=tomcat

4.在IDEA配置Tomcat

点击Run-Edit Configurations…,添加tomcat,配置如下:

点击运行,第一次会出现如下情况,点击accept即可:

运行效果如下:

参考:https://blog.csdn.net/qq_34021712/article/details/80871015

CAS