springboot 实现cas单点登录

一、部署服务端

我这边部署的是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.csdn.net/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、原理图

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wwwzhouzy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值