一、部署服务端
我这边部署的是cas-server5.2
cas-server: https://github.com/apereo/cas-overlay-template
cas document https://apereo.github.io/cas/5.2.x/index.html
源代码:https://codechina.youkuaiyun.com/wwwzhouzy/zhouzy-cas
1、构建
下载后解压如图:
构建,我的是windows,用build.cmd
也可以直接从https://mvnrepository.com/artifact/org.apereo.cas/cas-server-webapp-tomcat maven库中下载服务端war包
2、war包部署
下载后重命名cas-server.war放入tomcat8中,只有tomcat8以上版本支持,7.0是不支持的
解压后需要修改一些配置文件:
application.properties加入一些配置
cas.tgc.secure=false
cas.serviceRegistry.initFromJson=true
修改services\HTTPSandIMAPS-10000001.json
找到serviceId让它支持http如图,默认是https,这个需要keytools工具先生成证书:
重新打成war包,运行
启动成功
访问:
http://localhost:8080/cas-server/login
application.properties文件中找到用户名密码:
登录 成功
二、客户端测试
1、客户端引pom文件,记住要jdk1.8版本
<dependency>
<groupId>org.jasig.cas.client</groupId>
<artifactId>cas-client-core</artifactId>
<version>3.4.1</version>
</dependency>
application.properties
cas.server-url-prefix=http://localhost:8080/cas-server
cas.server-login-url=http://localhost:8080/cas-server/login
cas.client-host-url=http://localhost:9002
cas.use-session=true
cas.validation-type=cas
server.port=9002
casClientLogoutUrl=http://localhost:8080/cas-server/logout?service=http://localhost:9002/logout/success
启动:
package com.oumuv.cas;
import net.unicon.cas.client.configuration.EnableCasClient;
import org.jasig.cas.client.authentication.AuthenticationFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import java.util.HashMap;
import java.util.Map;
@SpringBootApplication
@EnableCasClient//启用cas client
public class CasApplication {
public static void main(String[] args) {
SpringApplication.run(CasApplication.class, args);
}
}
访问:localhost:9002/hello 因为没有登录跳转到cas服务端去登录:
登录后访问:
2、另起一个工程,application.properties配置
server.port=9003
server.servlet.context-path=/
server-url-prefix=http://localhost:8080/cas-server
server-login-url=http://localhost:8080/cas-server/login
client-host-url=http://localhost:9003/index
server-logout-url =http://localhost:8080/cas-server/logout
client-logout-url =http://localhost:8080/cas-server/logout?service=http://localhost:9003/logout/success
validation-type=cas
server-name=http://localhost:9003
启动:
package com.oumuv.cas;
import net.unicon.cas.client.configuration.EnableCasClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableCasClient//启用cas client
public class CasCApplication {
public static void main(String[] args) {
SpringApplication.run(CasCApplication.class, args);
}
}
访问:
不需要登录就成功访问了
三、说说cas的原理
1、cas主要包含两部分server和client
CAS Server 负责完成对用户的认证工作 , 需要独立部署 , CAS Server 会处理用户名 / 密码等凭证(Credentials) 。
CAS Client负责处理对客户端受保护资源的访问请求,需要对请求方进行身份认证时,重定向到 CAS Server 进行认证。(原则上,客户端应用不再接受任何的用户名密码等 Credentials )。
CAS Client 与受保护的客户端应用部署在一起,以 Filter 方式保护受保护的资源。
2、原理图