本章开始对cas的搭建
我们这次用到的cas版本为5.1.3
注意:
- 环境要求
Jdk1.8
Maven3.x - 基础知识要求
spring
spring boot
spring cloud
spring security
pac4j
1. 手脚架代码下载
官方提供了手脚架工具,可以自定去定义自己的项目,但里面用到了一个query的cdn需要翻墙
1.1. 代码下载步骤
填写依赖
输入项目信息:
但发现输入中文会乱码,可以在生成后再调整
点击create project即可下载到代码
2. 代码调整
2.1. 默认信息
- https
- 有部分java代码
- 配置文件不足
2.2. 调整计划
我接下来要调整部分代码涉及到的为:
- 调整maven为国内代理
- 删除java文件
- 修改日志目录
- 屏蔽https
2.3. maven调整
由于我们的平台涉及到多个项目所以我们会有一个父工程: sso
1. 新建父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>com.thtf</groupId>
<artifactId>sso</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>sso-server</module>
</modules>
<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.3.9</cas.version>
<springboot.version>1.5.3.RELEASE</springboot.version>
</properties>
<!--为了加快速度,修改成国内的代理-->
<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>
关注点为cas.version,调整到5.1.3,并且把maven的仓库进行调整
2. 导入sso-server工程
把多余的删除掉,包括maven仓库,properties信息(已挪到父工程),profile暂时不需要,删除
重点:
需要把cas-server-webapp-tomcat
依赖的type
调整为war
<?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">
<parent>
<artifactId>sso</artifactId>
<groupId>com.thtf</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cas-server</artifactId>
<name>cas-server</name>
<description>CAS认证服务,用来实现认证和授权</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.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>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>
3. 日志文件调整:
把etc/cas/config/log4j2.xml
放到项目的src/main/resources
下,并且把内容由<Property name="cas.log.dir" >.</Property>
调整成 <Property name="cas.log.dir" >logs</Property>
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
application.properties
##
# CAS Server Context Configuration
#
server.context-path=/cas
server.port=8443
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=false
# 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=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
##
# 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
4.删除java文件
包括src/main/java,src/test/java,否则会影响运行
最后我们的目录结构应该是这样的:
2.3. 为什么不用ect下的文件?
因为我们大多数开发都在windows,为了配置文件统一,我们直接放到工程下面,后面我们上线了可以放到指定地方不用重新打包即可运行
3. 运行
由于这个项目是spring boot,所以可以用java -jar指令直接运行的,或者运行交给cas提供的脚本
所有执行命令需要在所在的工程下执行
- 启动命令:
mvn clean package
java -jar target/cas.war
build.cmd run
- debug命令:
开放了5000远程调整端口build.cmd debug
- 打包:
build.cmd package
启动成功:
3.1 访问测试
目前我们配置的:
- 非https
- 端口为8443
- context-path 为cas
那么我们试着访问 http://localhost:8443/cas
如图所示,再次恭喜你
3.2 尝试登录
默认用户名/密码为:casuser/Mellon
注意:
由于目前为静态配置用户,后面我们会接入数据库,采用默认用户登录即可
登录后结果: