免费注册,打造高效身份管理
博客/开发者/Authing Share|Spring Security 集成 CAS 项目编码 认证(三)
Authing Share|Spring Security 集成 CAS 项目编码 认证(三)
Authing 官方2021.12.02阅读 460
在上一篇文章(《CAS 在 Authing 控制台配置 认证(二)》)中,我们讲述了如何在 Authing 平台配置项目集成中需要的 CAS 的配置,以及在后期开发过程中如何获取配置。同时,也提前让大家预习和熟悉了一些项目搭建和编码过程中需要的知识点。
 
接下来这一小节,就让我们一起来顺利完成项目搭建和编码过程吧。

 

01

项目搭建

使用 Spring Initializr 快速构建项目

打开 IDEA,点击 New Project 创建一个新项目,选择 Spring Initializr 创建一个 Spring Boot 项目,输入项目的 Group 以及 Artifact 信息。
 
 
集成过程中需要在 pom.xml 中添加一些其他的依赖包,如下:
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>

        <dependency>
            <groupId>net.unicon.cas</groupId>
            <artifactId>cas-client-autoconfig-support</artifactId>
            <version>2.3.0-GA</version>
        </dependency>

        <!--远程调用接口使用-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.11</version>
        </dependency>
    </dependencies>
02 

项目编码

配置项目中的文件
自动回调接口编码
 
在项目下新建一个 package,然后新建一个 CallBackController,此接口作用是通过 CAS 的 ticket 获取用户信息。注意,下面的参数 service,ticket 都是 Authing CAS 的标准,不能乱改,这也是标准协议的规定,service 参数所对应的值也就是之前在 Authing 平台应用所配置的那些。

 

package com.authing.cas.authingcas.controller;
import cn.hutool.http.HttpUtil;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;

@Controller
public class CallbackController {

    @GetMapping(value = "/", produces = MediaType.APPLICATION_XML_VALUE)
    @ResponseBody
    public String getTicket(String ticket) {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("service", "http://localhost:9999/");
        paramMap.put("ticket", ticket);
        String result = HttpUtil.get("https://cjtjls-demo.authing.cn/cas-idp/61319680ea8b30c9ca9ca071/serviceValidate", paramMap);
        return result;
    }

}
运行项目
一切准备就绪了,现在启动项目并访问 http://localhost:9999,即可看到 Authing 登录窗口。

 

<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas">
  <cas:authenticationSuccess>
    <cas:user>用户名</cas:user>
    <cas:attributes>
      <cas:authenticationDate>2021-07-20T10:04:14.044Z</cas:authenticationDate>
      <cas:longTermAuthenticationRequestTokenUsed>false</cas:longTermAuthenticationRequestTokenUsed>
      <cas:updated_at/>
      <cas:address>
        <cas:locality/>
        <cas:street_address/>
      </cas:address>
      <cas:phone_number_verified>false</cas:phone_number_verified>
      <cas:gender>U</cas:gender>
      <cas:email_verified>false</cas:email_verified>
      <cas:picture>https://files.authing.co/authing-console/default-user-avatar.png</cas:picture>
      <cas:sub>60a5e57cfaa50e4850dd12b0</cas:sub>
    </cas:attributes>
  </cas:authenticationSuccess>
</cas:serviceResponse>

 

此外,Authing 还会保存用户的登录态,用户短时间内再次登录无需进行认证。另外,如果 ticket 验证失败,Authing 将返回格式如下的 xml 文档。
<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas">
  <cas:authenticationFailure code="INVALID_TICKET">Ticket 不存在</cas:authenticationFailure>
</cas:serviceResponse>
 

项目地址

https://github.com/Authing/example-spring-boot-cas

 

 

总结

恭喜 🎉,到此你已经学会了 Spring Security 5 集成 Authing CAS 认证授权的整个流程。后续还有更多的干货,持续关注吧!

文章作者

avatar

Authing 官方

0

文章总数

authing blog rqcode
关注 Authing 公众号
随时随地发现更多内容
authing blog rqcode
添加 Authing 小助手
加入 Authing 开发者大家庭