# integrated-cas3-restful

# 一、前置条件

Jdk 1.8+

platform-client-common 中科软提供的基础类库

# 二、集成方式及说明

# 1、application.yml

# 端口
server:
  port: 8111
  servlet:
    encoding:
      charset: UTF-8
  tomcat:
    uri-encoding: UTF-8
1
2
3
4
5
6
7
8

# 2、pom.xml

 <!-- fastjson -->
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>fastjson</artifactId>
  <version>1.2.80</version>
</dependency>

<!--中科软提供的基础类库 用于访问中科软提供的资源信息时使用-->
<dependency>
  <groupId>com.sinosoft</groupId>
  <artifactId>platform-client-common</artifactId>
  <version>1.0.1</version>
  <exclusions>
    <exclusion>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
    </exclusion>
  </exclusions>
</dependency>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# 3、IndexController.java

package com.sinosoft.clientrest.controller;

import com.alibaba.fastjson.JSONObject;
import com.sinosoft.platformclientcommon.api.CasRestApi;
import com.sinosoft.platformclientcommon.result.SinoHttpResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class IndexController {

    @RequestMapping(value = {"/"})
    public String index() {
        return "index....";
    }

    /**
     * 校验过程(测试用)
     * 1 根据用户名、密码获取TicketGrantingTicket(CasRestApi.getTicketGrantingTicket)
     * 2 用TicketGrantingTicket获取ServiceTicket(CasRestApi.getServiceTicket)
     * 3 删除TicketGrantingTicket(CasRestApi.deleteTicketGrantingTicket)
     *
     * ServiceTicket都只能用1次,用完1次就过期。
     * st校验1次以后,也不能再用了。因为只能用1次
     * @return
     */
    @RequestMapping(value = {"/test"})
    public Object test() {

        // cas-server 服务端地址
        final String server = "http://127.0.0.1:8000/cas";
        final String username = "张三"; //李四
        final String password = "1";
        //client-rest 客户端地址
        final String service = "http://127.0.0.1:8111/index";


        log.info("========== 获取TicketGrantingTicket ==========");
        String ticketGrantingTicket = CasRestApi.getTicketGrantingTicket(server, username, password);
        log.info("ticketGrantingTicket:    {}", ticketGrantingTicket);

        // log.info("========== 删除TicketGrantingTicket ==========");
        // boolean isChecked4 = CasRestApi.deleteTicketGrantingTicket(server, ticketGrantingTicket);
        // log.info("ticketGrantingTicket 是否有效:    {}", isChecked4);
        //
        // boolean isChecked3 = CasRestApi.validateTicketGrantingTicket(server, ticketGrantingTicket, service);
        // log.info("ticketGrantingTicket 是否有效:    {}", isChecked3);

        log.info("========== 用TicketGrantingTicket获取ServiceTicket ==========");
        String serviceTicket = CasRestApi.getServiceTicket(server, ticketGrantingTicket, service);
        log.info("serviceTicket:    {}", serviceTicket);

        JSONObject user1 = null;
        if (serviceTicket != null) {
            log.info("========== 验证serviceTicket 是否有效 ==========");
            SinoHttpResult sinoHttpResult = CasRestApi.validateServiceTicket(server, serviceTicket, service);
            if (sinoHttpResult.getCode() == 200) {
                user1 = sinoHttpResult.getResult();
            }
            log.info("serviceTicket 是否有效:    {}", user1);
        }
        return user1;
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

# 三、效果展示

http://127.0.0.1:8111/test

image-20220623110046058.png

# 四、demo程序

下载地址:client-rest.zip

依赖jar包:platform-client-common-1.0.1.jar