CAS單點登陸-基礎搭建

前言

上一篇:CAS單點登陸-簡介java

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

手腳架代碼下載

代碼下載

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

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

  1. 填寫項目信息

  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的倉庫進行調整apache

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

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

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下而且進行調整bash

關注點:cookie

server.ssl.enabled=falsesession

若是嫌麻煩,能夠直接複製粘貼下面的代碼app

##
# 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提供的腳本吧

全部執行命令須要在所在的工程下執行

初始化命令:

build.cmd init

打包命令:

build.cmd package

清理target目錄文件命令:

build.cmd clean

啓動命令:

build.cmd run

以下圖,顯示啓動成功

測試

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

登錄

默認用戶名/密碼爲:casuser/Mellon

**注意: ** 因爲目前爲靜態配置用戶,採用默認用戶登陸便可 登陸後結果:

總結

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

本文參考他人博客學習整理,須要看原博客的或者查看後續文章的能夠點擊

相關文章
相關標籤/搜索