CAS單點登入-基礎搭建

Null指標發表於2018-12-04

前言

上一篇:CAS單點登入-簡介

下一篇:CAS單點登入-配置中心

手腳架程式碼下載

程式碼下載

官方提供了下載工具(需要FQ)

  1. 選擇依賴,目前只要選擇Tomcat即可

CAS單點登入-基礎搭建

  1. 填寫專案資訊

CAS單點登入-基礎搭建

  1. 點選Generate Project, 生成專案

專案調整

建立專案

  1. 建立資料夾(sso)
  2. 在sso資料夾裡建立maven專案(sso-base)
  3. 將下載下來的sso-server拷貝到sso

maven調整

parent pom.xml

sso-base作為主專案,這邊關注點為cas.version,調整到5.1.3,並且把maven的倉庫進行調整

<?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>com.cxy.auth</groupId>
    <artifactId>sso-base</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <cas.version>5.1.3</cas.version>
        <springboot.version>1.5.3.RELEASE</springboot.version>
    </properties>

    <modules>
        <module>../sso-server</module>
    </modules>

    <!--為了加快下載jar包速度,修改成國內的代理-->
    <repositories>
        <repository>
            <releases>
                <enabled>true</enabled>
            </releases>
            <id>maven2-release</id>
            <url>http://uk.maven.org/maven2/</url>
        </repository>

        <repository>
            <snapshots>
                <enabled>true</enabled>
                <!--快照版本庫兩個小時檢查更新一遍-->
                <updatePolicy>interval:120</updatePolicy>
            </snapshots>
            <id>oss-snapshots</id>
            <url>http://repository.jboss.org/nexus/content/groups/public/</url>
        </repository>
    </repositories>
</project>
複製程式碼

sso-server pom.xml

刪掉多餘的配置,該配置在父工程中都已配置,包括maven倉庫,properties資訊,profile。

<?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>

    <parent>
        <groupId>com.cxy.auth</groupId>
        <artifactId>sso-base</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../sso-base/pom.xml</relativePath>
    </parent>

    <artifactId>sso-server</artifactId>
    <packaging>war</packaging>

    <name>sso-server</name>
    <description>CAS認證服務,SSO(單點登入)</description>

    <dependencies>
        <dependency>
            <groupId>org.apereo.cas</groupId>
            <artifactId>cas-server-webapp-tomcat</artifactId>
            <version>${cas.version}</version>
            <type>war</type>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.apereo.cas</groupId>
                <artifactId>cas-server-support-bom</artifactId>
                <version>${cas.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>com.rimerosolutions.maven.plugins</groupId>
                <artifactId>wrapper-maven-plugin</artifactId>
                <version>0.0.5</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>3.1.0</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-tomcat/META-INF/MANIFEST.MF</manifestFile>
                    </archive>
                    <overlays>
                        <overlay>
                            <groupId>org.apereo.cas</groupId>
                            <artifactId>cas-server-webapp-tomcat</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>
</project>
複製程式碼

log4j2.xml

etc/cas/config/log4j2.xml放到專案的src/main/resources下,並且把內容<Property name="cas.log.dir" >.</Property> 調整成 <Property name="cas.log.dir" >logs</Property>

CAS單點登入-基礎搭建

application.properties

我們把依賴包下載的cas-server-webapp-tomcat/5.1.3/cas-server-webapp-tomcat-5.1.3.war!/WEB-INF/classes/application.properties拷貝到src/main/resources下並且進行調整

關注點:

server.ssl.enabled=false

如果嫌麻煩,可以直接複製貼上下面的程式碼

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

server.ssl.enabled=false

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

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=true
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
#
cas.authn.accept.users=casuser::Mellon
複製程式碼

.java

刪除java檔案,包括src/main/javasrc/test/java,否則會影響執行

執行

啟動專案

由於這個專案是spring boot,所以可以用java -jar指令直接執行的,那麼我們執行交給cas提供的指令碼吧

所有執行命令需要在所在的工程下執行

CAS單點登入-基礎搭建

初始化命令:

build.cmd init

打包命令:

build.cmd package

清理target目錄檔案命令:

build.cmd clean

啟動命令:

build.cmd run

如下圖,顯示啟動成功

CAS單點登入-基礎搭建

測試

訪問 http://localhost:8443/cas/login

CAS單點登入-基礎搭建

登陸

預設使用者名稱/密碼為:casuser/Mellon

**注意: ** 由於目前為靜態配置使用者,採用預設使用者登入即可 登入後結果:

CAS單點登入-基礎搭建

總結

  1. 利用手腳架搭建了cas,並且進行了配置檔案的調整
  2. 簡單介紹了cas

本文參考他人部落格學習整理,需要看原部落格的或者檢視後續文章的可以點選

相關文章