CAS 5.1.x 的搭建和使用(一)—— 通過Overlay搭建服務端

先放上官網文檔地址:https://apereo.github.io/cas/5.1.x/index.html

另外有一篇特別好的文檔在豆丁上:

Windows環境CAS 5.1.X單點登錄系統配置試驗

然後說一下用到的東西:

jdk1.8、tomcat8.5、maven3.3、windows操作系統

 

1、下載Overlay

通過閱讀官網文檔(https://apereo.github.io/cas/5.1.x/planning/Getting-Started.html)瞭解到官方建議我們:

It is recommended to build and deploy CAS locally using the WAR Overlay method. 

通過使用一個名叫Overlay的項目來生成一個可以直接用的war包,來部署服務端,於是我們先下載這個項目,官網給出了兩個構築格式的:

我這裏使用Maven的,下載地址:https://github.com/apereo/cas-overlay-template,或者使用我上傳的網盤地址下載:鏈接:http://pan.baidu.com/s/1jIrFsWm 密碼:mxrq

 

2、構築Overlay

下載下來的Overlay默認配置就可以直接構築能用的war包,直接使用它下邊的build腳本執行 

build package

第一次構築比較慢,可以在pom的repositories里加一個ali源,構築會快一些。

構築完後在target下找到一個war包,放到你的tomcat8.5(官方建議8.0以上版本,我建議使用8.5,如果你用8.0跑不起來,記得換成8.5以上版本)下跑起來試試吧:

http://localhost:8080/cas/login  默認賬號:casuser  默認密碼:Mellon  目前的配置僅有這一個用戶

第一次會有兩個紅色警告,一個就是說你沒用HTTPS登錄,另一個就是你現在只有一個寫死的用戶,目前這個服務端只能看看,沒什麼實際用途。

別急,我們下邊開始解決這兩個問題。

這裏先不要急着刪掉你Tomcat下war包剛剛解壓出來的內容

 

3、生成真正有用的服務端

這一節裏面我們有兩件事情要做:

第一個就是做一個keystore,Tomcat配置HTTPS訪問的時候會用到;

第二個就是把我們的用戶改成從數據庫的表中讀取的。

 

先說keystore:

使用jdk自帶的keytool即可生成keystore,命令如下:

keytool -genkey -alias cas -keyalg RSA -keysize 2048 -keypass 123456 -storepass 123456 -keystore D:/liuyx.keystore -dname "CN=cas.example.org,OU=liuyx.com,O=liuyx,L=JiNan,ST=JiNan,C=CN"

別名密碼什麼的都可以改,需要指出的是:

CN=cas.example.org這段內容,後邊配置客戶端的時候需要用到,一定要確保能通過這個域名訪問到你的CAS服務端。

並且不能使用IP作爲域(上文的CN),使用IP雖然可以生成keystore,但是在客戶端使用的時候,如果服務端地址配置成IP會報錯。

這裏我的CAS服務端是部署在本地的,所以需要做一個本地映射——

用管理員身份修改C:\Windows\System32\drivers\etc\hosts文件,在其最後加上以下內容:

127.0.0.1       cas.example.org

這樣,我們在本地訪問這個域名,其實訪問的就是我們本機了。

 

再說https:

Tomcat8.5配置https的方式相較之前的版本有所調整,不過也差不太多,修改server.xml文件,如下:

複製代碼
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
               maxThreads="150" SSLEnabled="true">
        <SSLHostConfig>
            <Certificate certificateKeystoreFile="D:/liuyx.keystore"
                         type="RSA" certificateKeystoreType="JKS" certificateKeystorePassword="123456"/>
        </SSLHostConfig>
    </Connector>
複製代碼

注意,別忘了把8080端口的Connector註釋掉:

<!-- <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />-->

 

最後是從數據庫中讀取用戶:

這裏我們要做這麼幾件事:

  1、在pom中引入數據庫相關的jar包,註釋掉用不到的jar包

pom如下:

複製代碼
<?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.0</modelVersion>
    <groupId>org.apereo.cas</groupId>
    <artifactId>cas-overlay</artifactId>
    <packaging>war</packaging>
    <version>1.0</version>

    <build>
        <plugins>
            <!--STEP1 註釋掉無用組件
            <plugin>
                <groupId>com.rimerosolutions.maven.plugins</groupId>
                <artifactId>wrapper-maven-plugin</artifactId>
                <version>0.0.4</version>
                <configuration>
                    <verifyDownload>true</verifyDownload>
                    <checksumAlgorithm>MD5</checksumAlgorithm>
                </configuration>
            </plugin>-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${springboot.version}</version>
                <configuration>
                    <mainClass>org.springframework.boot.loader.WarLauncher</mainClass>
                    <addResources>true</addResources>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <warName>cas</warName>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <recompressZippedFiles>false</recompressZippedFiles>
                    <archive>
                        <compress>false</compress>
                        <manifestFile>${project.build.directory}/war/work/org.apereo.cas/cas-server-webapp${app.server}/META-INF/MANIFEST.MF
                        </manifestFile>
                    </archive>
                    <overlays>
                        <overlay>
                            <groupId>org.apereo.cas</groupId>
                            <artifactId>cas-server-webapp${app.server}</artifactId>
                        </overlay>
                    </overlays>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
            </plugin>
        </plugins>
        <finalName>cas</finalName>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.apereo.cas</groupId>
            <artifactId>cas-server-webapp${app.server}</artifactId>
            <version>${cas.version}</version>
            <type>war</type>
            <scope>runtime</scope>
        </dependency>
        <!--STEP2 引入數據庫認證相關 start-->
        <dependency>
            <groupId>org.apereo.cas</groupId>
            <artifactId>cas-server-support-jdbc</artifactId>
            <version>${cas.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apereo.cas</groupId>
            <artifactId>cas-server-support-jdbc-drivers</artifactId>
            <version>${cas.version}</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.36</version>
        </dependency>
        <!--數據庫認證相關 end-->

        <!--<dependency>
            <groupId>org.jasig.cas</groupId>
            <artifactId>cas-server-core-authentication</artifactId>
            <version>4.2.7</version>
        </dependency>-->
        <!--<dependency>
            <groupId>org.apereo.cas</groupId>
            <artifactId>cas-server-core-util</artifactId>
            <version>${cas.version}</version>
        </dependency>-->
    </dependencies>

    <properties>
        <cas.version>5.1.1</cas.version><!--STEP3 修改版本,高版本目前暫時沒有相應的JDBC支持-->
        <springboot.version>1.5.3.RELEASE</springboot.version>
        <!-- app.server could be -jetty, -undertow, -tomcat, or blank if you plan to provide appserver -->
        <app.server>-tomcat</app.server> 
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <repositories>
        <repository>
            <id>sonatype-releases</id>
            <url>http://oss.sonatype.org/content/repositories/releases/</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <releases>
                <enabled>true</enabled>
            </releases>
        </repository>
        <repository>
            <id>sonatype-snapshots</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
        <repository>
            <id>shibboleth-releases</id>
            <url>https://build.shibboleth.net/nexus/content/repositories/releases</url>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>
    <!--STEP4 註釋掉無用組件
        <profiles>
            <profile>
                <activation>
                    <activeByDefault>false</activeByDefault>
                </activation>
                <id>pgp</id>
                <build>
                    <plugins>

                        <plugin>
                            <groupId>com.github.s4u.plugins</groupId>
                            <artifactId>pgpverify-maven-plugin</artifactId>
                            <version>1.1.0</version>
                            <executions>
                                <execution>
                                    <goals>
                                        <goal>check</goal>
                                    </goals>
                                </execution>
                            </executions>
                            <configuration>
                                <pgpKeyServer>hkp://pool.sks-keyservers.net</pgpKeyServer>
                                <pgpKeysCachePath>${settings.localRepository}/pgpkeys-cache</pgpKeysCachePath>
                                <scope>test</scope>
                                <verifyPomFiles>true</verifyPomFiles>
                                <failNoSignature>false</failNoSignature>
                            </configuration>
                        </plugin>
                </plugins>
            </build>
        </profile>

    </profiles>-->
</project>
複製代碼

注意這裏我用的cas.version是5.1.1,是因爲現在的maven倉庫裏沒有更高版本的cas-server-support-jdbc和cas-server-support-jdbc-drivers,爲了保持統一選用的5.1.1,你如果能處理好這些jar包,也可以用高版本。

 

  2、修改配置文件

觀察剛纔war包解壓出的文件,可以在WEB-INF下發現很多配置文件,

通過閱讀:https://apereo.github.io/cas/5.1.x/installation/Configuration-Server-Management.html 得知,我們可以通過修改其中一個application.properties配置文件來使服務端支持從數據庫的某張表來驗證用戶。

具體的配置信息也可以從https://apereo.github.io/cas/5.1.x/installation/Configuration-Server-Management.html 來了解。

爲了防止再次打war包的時候,修改的配置被覆蓋,我在cas-overlay這個maven項目下新建了src,resources等目錄,然後把配置文件複製到相應的目錄進行修改,結構如下:

當然你也可以直接在war包解壓出來的內容上改。

修改後的application.properties內容如下(在原基礎上修改,註釋掉一部分用不到的東西):

複製代碼
#STEP 3 在TOMCAT8.5中跑一個模板然後將其war包中解壓出來的的application.properties複製出來,放到手動創建的src下的resources裏面

##
# CAS Server Context Configuration
#
server.context-path=/cas
server.port=8443

#STEP 5添加認證服務
cas.serviceRegistry.initFromJson=true

#STEP 4簽發證書,如果是用spring boot之類嵌入式的容器,則需要改這裏的配置,如果是直接部在tomcat中,則需要把tomcat改成https的
#server.ssl.key-store=file:/etc/cas/thekeystore
#server.ssl.key-store-password=changeit
#server.ssl.key-password=changeit
# server.ssl.ciphers=
# server.ssl.client-auth=
# server.ssl.enabled=
# server.ssl.key-alias=
# server.ssl.key-store-provider=
# server.ssl.key-store-type=
# server.ssl.protocol=
# server.ssl.trust-store=
# server.ssl.trust-store-password=
# server.ssl.trust-store-provider=
# server.ssl.trust-store-type=

#server.max-http-header-size=2097152
#server.use-forward-headers=true
#server.connection-timeout=20000
#server.error.include-stacktrace=NEVER

#server.tomcat.max-http-post-size=2097152
#server.tomcat.basedir=build/tomcat
#server.tomcat.accesslog.enabled=true
#server.tomcat.accesslog.pattern=%t %a "%r" %s (%D ms)
#server.tomcat.accesslog.suffix=.log
#server.tomcat.max-threads=10
#server.tomcat.port-header=X-Forwarded-Port
#server.tomcat.protocol-header=X-Forwarded-Proto
#server.tomcat.protocol-header-https-value=https
#server.tomcat.remote-ip-header=X-FORWARDED-FOR
#server.tomcat.uri-encoding=UTF-8
   
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true

##
# CAS Cloud Bus Configuration
#
spring.cloud.bus.enabled=false
# spring.cloud.bus.refresh.enabled=true
# spring.cloud.bus.env.enabled=true
# spring.cloud.bus.destination=CasCloudBus
# spring.cloud.bus.ack.enabled=true

endpoints.enabled=false
endpoints.sensitive=true

endpoints.restart.enabled=false
endpoints.shutdown.enabled=false

management.security.enabled=true
management.security.roles=ACTUATOR,ADMIN
management.security.sessions=if_required
management.context-path=/status
management.add-application-context-header=false

security.basic.authorize-mode=role
security.basic.enabled=false
security.basic.path=/cas/status/**

##
# CAS Web Application Session Configuration
#
server.session.timeout=300
server.session.cookie.http-only=true
server.session.tracking-modes=COOKIE

##
# CAS Thymeleaf View Configuration
#
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.cache=false
spring.thymeleaf.mode=HTML
##
# CAS Log4j Configuration
#
# logging.config=file:/etc/cas/log4j2.xml
server.context-parameters.isLog4jAutoInitializationDisabled=true

##
# CAS AspectJ Configuration
#
spring.aop.auto=true
spring.aop.proxy-target-class=true

##
# CAS Authentication Credentials
#
#STEP4 註釋掉寫死的用戶 改用jdbc的用戶 START
#cas.authn.accept.users=casuser::Mellon

cas.authn.jdbc.query[0].sql=select * from cms_auth_user where user_name=?
cas.authn.jdbc.query[0].healthQuery=
cas.authn.jdbc.query[0].isolateInternalQueries=false
cas.authn.jdbc.query[0].url=jdbc:mysql://127.0.0.1:3306/CASTEST?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false
cas.authn.jdbc.query[0].failFast=true
cas.authn.jdbc.query[0].isolationLevelName=ISOLATION_READ_COMMITTED
cas.authn.jdbc.query[0].dialect=org.hibernate.dialect.MySQLDialect
cas.authn.jdbc.query[0].leakThreshold=10
cas.authn.jdbc.query[0].propagationBehaviorName=PROPAGATION_REQUIRED
cas.authn.jdbc.query[0].batchSize=1
cas.authn.jdbc.query[0].user=root
#cas.authn.jdbc.query[0].ddlAuto=create-drop
cas.authn.jdbc.query[0].maxAgeDays=180
cas.authn.jdbc.query[0].password=123456
cas.authn.jdbc.query[0].autocommit=false
cas.authn.jdbc.query[0].driverClass=com.mysql.jdbc.Driver
cas.authn.jdbc.query[0].idleTimeout=5000
# cas.authn.jdbc.query[0].credentialCriteria=
# cas.authn.jdbc.query[0].name=
# cas.authn.jdbc.query[0].order=0
# cas.authn.jdbc.query[0].dataSourceName=
# cas.authn.jdbc.query[0].dataSourceProxy=false
cas.authn.jdbc.query[0].fieldPassword=password

# cas.authn.jdbc.query[0].fieldExpired=
# cas.authn.jdbc.query[0].fieldDisabled=
# cas.authn.jdbc.query[0].principalAttributeList=sn,cn:commonName,givenName

#cas.authn.jdbc.query[0].passwordEncoder.type=DEFAULT
#cas.authn.jdbc.query[0].passwordEncoder.type=com.example.CustomPasswordEncoder
#cas.authn.jdbc.query[0].passwordEncoder.characterEncoding=UTF-8
#cas.authn.jdbc.query[0].passwordEncoder.encodingAlgorithm=MD5

#cas.authn.jdbc.query[0].passwordEncoder.secret=
#cas.authn.jdbc.query[0].passwordEncoder.strength=16

# cas.authn.jdbc.query[0].principalTransformation.suffix=
# cas.authn.jdbc.query[0].principalTransformation.caseConversion=NONE|UPPERCASE|LOWERCASE
# cas.authn.jdbc.query[0].principalTransformation.prefix=
# STEP4 END


##
# CAS Delegated Authentication
#
#cas.authn.pac4j.bitbucket.clientName=Bitbucket
#cas.authn.pac4j.dropbox.clientName=Dropbox
#cas.authn.pac4j.facebook.clientName=Facebook
#cas.authn.pac4j.foursquare.clientName=Foursquare
#cas.authn.pac4j.github.clientName=Github
#cas.authn.pac4j.google.clientName=Google
#cas.authn.pac4j.linkedIn.clientName=LinkedIn
#cas.authn.pac4j.paypal.clientName=PayPal
#cas.authn.pac4j.twitter.clientName=Twitter
#cas.authn.pac4j.yahoo.clientName=Yahoo
#cas.authn.pac4j.windowsLive.clientName=Windows Live
#cas.authn.pac4j.wordpress.clientName=WordPress

#多屬性
cas.authn.attributeRepository.jdbc[0].singleRow=true
cas.authn.attributeRepository.jdbc[0].order=0
cas.authn.attributeRepository.jdbc[0].url=jdbc:mysql://127.0.0.1:3306/CASTEST?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false
cas.authn.attributeRepository.jdbc[0].username=user_name
cas.authn.attributeRepository.jdbc[0].user=root
cas.authn.attributeRepository.jdbc[0].password=123456
cas.authn.attributeRepository.jdbc[0].sql=select * from cms_auth_user where {0}
cas.authn.attributeRepository.jdbc[0].dialect=org.hibernate.dialect.MySQLDialect
cas.authn.attributeRepository.jdbc[0].ddlAuto=none
cas.authn.attributeRepository.jdbc[0].driverClass=com.mysql.jdbc.Driver
cas.authn.attributeRepository.jdbc[0].leakThreshold=10
cas.authn.attributeRepository.jdbc[0].propagationBehaviorName=PROPAGATION_REQUIRED
cas.authn.attributeRepository.jdbc[0].batchSize=1
cas.authn.attributeRepository.jdbc[0].healthQuery=SELECT 1
cas.authn.attributeRepository.jdbc[0].failFast=true
複製代碼

其中加粗標藍的都是我修改的內容,簡單說一下:

cas.authn.accept.users=casuser::Mellon這個配置記得刪掉,這就是那個寫死的用戶

cas.authn.jdbc.query[0]這些配置就是數據庫驗證相關的內容

在cas.authn.jdbc.query[0].sql中,程序會把你登錄時輸入的用戶名作爲參數傳進去

cas.authn.jdbc.query[0].fieldPassword則是指明那一列對應的是你輸入的密碼,目前沒有做MD5,怎麼做MD5下一篇一起說。

 

至此這節該說的說完了,有些朋友可能會納悶配置文件中的其它標藍加粗內容是幹什麼的,不要急我們下節再講,你可以先把這些我沒提到的標藍內容刪掉,跑起來試試現在服務端是否真的通過數據庫驗證了。

發佈了159 篇原創文章 · 獲贊 429 · 訪問量 99萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章