Background
单点登录SSO
(Single Sign ON),指在多个应用系统中,只需登录一次,即可在多个应用系统之间共享登录。统一身份认证CAS
(Central Authentication Service)是SSO的开源实现,利用CAS实现SSO可以很大程度的降低开发和维护的成本。- CAS由CAS Server和CAS Client两部分组成。
- CAS三个重要术语:TGT(Ticket Granting Ticket)、TGC(Ticket Granting Cookie)和ST(Service Ticket)。
1、所用软件版本
软件名称 | 版本 |
---|---|
jdk | 1.8.0_202 |
maven | 3.6.3 |
cas | 5.3.16 |
- maven下载地址:https://maven.apache.org/download.cgi
- cas-overlay github地址:https://github.com/apereo/cas-overlay-template/tree/5.3
- cas-overlay gitee地址:https://gitee.com/waisaa/cas-overlay-template
CAS项目托管在Github上,克隆或者下载源码,进行编译以后即可安装。但是对于一般用户来说,官方建议的安装方式是使用WAR Overlay方法来安装编译好的war包。WAR Overlay是一种对抗重复性的代码和资源的策略。它会下载一个由项目本身提供的预构建的普通CAS Web应用程序服务器,而不是下载CAS代码库并从源代码开始构建。WAR Overlay在构建时,构建安装工具首先会尝试下载官方提供的二进制安装包。然后,该工具将查找在同一项目目录中可用的配置文件和设置,并将它们合并到下载的工件中,以生成一个完整的Web存档(
cas.war
)。
2、cas-server安装
- 生成的
cas.war
包在target目录下- 访问地址:
https://localhost:8443/cas
(localhost换成cas所在的服务器IP)- 默认用户名密码:
casuser
=Mellon
git clone -b 5.3 https://github.com/apereo/cas-overlay-template.git
mvn clean compile package
java -jar target/cas.war
这样,中央身份认证服务就安装好了!
3、springboot集成cas-client
- maven依赖
<dependency><groupId>net.unicon.cas</groupId><artifactId>cas-client-autoconfig-support</artifactId><version>2.3.0-GA</version>
</dependency>
- yml配置
cas:#后端服务地址client-host-url: http://127.0.0.1:8888#cas认证中心地址server-url-prefix: https://monkey:8443/cas_server#cas认证中心登录地址server-login-url: https://monkey:8443/cas_server/loginvalidation-type: cas
- 启动类加上
@EnableCasClient
启用CAS
- 退出示例
package cas.client.sso.controller;import lombok.extern.slf4j.Slf4j;
import org.jasig.cas.client.authentication.AttributePrincipal;
import org.jasig.cas.client.util.AbstractCasFilter;
import org.jasig.cas.client.validation.Assertion;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;/*** @ClassName: TestController* @Description: TestController* @Author W* @Date 2020/12/23 17:32* @Version 1.0*/
@Slf4j
@Controller
public class TestController {@Value(value = "${cas.server-url-prefix}")private String serverUrlPrefix = "";@Value(value = "${cas.client-host-url}")private String clientHostUrl = "";// /sso-client/hello@GetMapping("/hello")@ResponseBodypublic String hello() {return "word";}@GetMapping("user")@ResponseBodypublic String user(HttpServletRequest request) {Assertion assertion = (Assertion) request.getSession().getAttribute(AbstractCasFilter.CONST_CAS_ASSERTION);String loginName = null;if (assertion != null) {AttributePrincipal principal = assertion.getPrincipal();loginName = principal.getName();log.info("访问者: {}", loginName);}return "访问者:" + loginName;}@RequestMapping("/logout")public String logout(HttpSession session) {session.invalidate();return "redirect:" + serverUrlPrefix + "/logout?service=" + clientHostUrl + "/sso-client/user";}
}