CAS

一、发送GET请求获取RSA公钥和JSESSIONID

请求地址:/cas/login,请求类型:GET

curl -I http://cas.gfstack.geo:8080/cas/login

返回如下:

1HTTP/1.1 200 2Set-Cookie: JSESSIONID=2AC7AF14F7798FA770E927F8EFB356FE; Path=/cas; HttpOnly 3Set-Cookie: publicKey=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDOrwpYIEi0CTd7TSVqQZ/EhHqysaKT0XEn7pjkO4LQ4g6IfJ/4tcbgn4XpS0cR/9P0WpV93Fsnc40s8VINoMqBH64cNHqE4boEqftRSi/L3FQvMSLxctLavu/kF5bzURPRMPyKlW2FQqMkhVautguT939cu+EitEQKMj29hikAyQIDAQAB; Path=/cas 4Cache-Control: no-store 5Content-Type: text/html;charset=UTF-8 6Content-Length: 9856 7Date: Tue, 14 May 2019 04:07:12 GMT

获取到 JSESSIONID 和 publicKey

二、发送POST请求获取请求头中的Location

拿 publicKey 做为RSA公钥对用户密码明文做加密得到 password ,提供java实现,其他语言请自行百度

1package com.geostar.gfstack.cas.support.util; 2 3import org.apache.commons.codec.binary.Base64; 4 5import javax.crypto.Cipher; 6import java.security.KeyFactory; 7import java.security.KeyPair; 8import java.security.KeyPairGenerator; 9import java.security.NoSuchAlgorithmException; 10import java.security.PrivateKey; 11import java.security.PublicKey; 12import java.security.interfaces.RSAPrivateKey; 13import java.security.interfaces.RSAPublicKey; 14import java.security.spec.PKCS8EncodedKeySpec; 15import java.security.spec.X509EncodedKeySpec; 16 17/** 18 * RSA加解密工具类,实现公钥加密私钥解密和私钥解密公钥解密 19 * link:https://www.cnblogs.com/nihaorz/p/10690643.html 20 */ 21public class RSAUtils { 22 23 private static final String src = "abcdefghijklmnopqrstuvwxyz"; 24 25 public static void main(String[] args) throws Exception { 26 System.out.println("\n"); 27 RSAKeyPair keyPair = generateKeyPair(); 28 System.out.println("公钥:" + keyPair.getPublicKey()); 29 System.out.println("私钥:" + keyPair.getPrivateKey()); 30 System.out.println("\n"); 31 test1(keyPair, src); 32 System.out.println("\n"); 33 test2(keyPair, src); 34 System.out.println("\n"); 35 } 36 37 /** 38 * 公钥加密私钥解密 39 */ 40 private static void test1(RSAKeyPair keyPair, String source) throws Exception { 41 System.out.println("***************** 公钥加密私钥解密开始 *****************"); 42 String text1 = encryptByPublicKey(keyPair.getPublicKey(), source); 43 String text2 = decryptByPrivateKey(keyPair.getPrivateKey(), text1); 44 System.out.println("加密前:" + source); 45 System.out.println("加密后:" + text1); 46 System.out.println("解密后:" + text2); 47 if (source.equals(text2)) { 48 System.out.println("解密字符串和原始字符串一致,解密成功"); 49 } else { 50 System.out.println("解密字符串和原始字符串不一致,解密失败"); 51 } 52 System.out.println("***************** 公钥加密私钥解密结束 *****************"); 53 } 54 55 /** 56 * 私钥加密公钥解密 57 * 58 * @throws Exception 59 */ 60 private static void test2(RSAKeyPair keyPair, String source) throws Exception { 61 System.out.println("***************** 私钥加密公钥解密开始 *****************"); 62 String text1 = encryptByPrivateKey(keyPair.getPrivateKey(), source); 63 String text2 = decryptByPublicKey(keyPair.getPublicKey(), text1); 64 System.out.println("加密前:" + source); 65 System.out.println("加密后:" + text1); 66 System.out.println("解密后:" + text2); 67 if (source.equals(text2)) { 68 System.out.println("解密字符串和原始字符串一致,解密成功"); 69 } else { 70 System.out.println("解密字符串和原始字符串不一致,解密失败"); 71 } 72 System.out.println("***************** 私钥加密公钥解密结束 *****************"); 73 } 74 75 /** 76 * 公钥解密 77 * 78 * @param publicKeyText 79 * @param text 80 * @return 81 * @throws Exception 82 */ 83 public static String decryptByPublicKey(String publicKeyText, String text) throws Exception { 84 X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyText)); 85 KeyFactory keyFactory = KeyFactory.getInstance("RSA"); 86 PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec); 87 Cipher cipher = Cipher.getInstance("RSA"); 88 cipher.init(Cipher.DECRYPT_MODE, publicKey); 89 byte[] result = cipher.doFinal(Base64.decodeBase64(text)); 90 return new String(result); 91 } 92 93 /** 94 * 私钥加密 95 * 96 * @param privateKeyText 97 * @param text 98 * @return 99 * @throws Exception 100 */ 101 public static String encryptByPrivateKey(String privateKeyText, String text) throws Exception { 102 PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyText)); 103 KeyFactory keyFactory = KeyFactory.getInstance("RSA"); 104 PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec); 105 Cipher cipher = Cipher.getInstance("RSA"); 106 cipher.init(Cipher.ENCRYPT_MODE, privateKey); 107 byte[] result = cipher.doFinal(text.getBytes()); 108 return Base64.encodeBase64String(result); 109 } 110 111 /** 112 * 私钥解密 113 * 114 * @param privateKeyText 115 * @param text 116 * @return 117 * @throws Exception 118 */ 119 public static String decryptByPrivateKey(String privateKeyText, String text) throws Exception { 120 PKCS8EncodedKeySpec pkcs8EncodedKeySpec5 = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyText)); 121 KeyFactory keyFactory = KeyFactory.getInstance("RSA"); 122 PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec5); 123 Cipher cipher = Cipher.getInstance("RSA"); 124 cipher.init(Cipher.DECRYPT_MODE, privateKey); 125 byte[] result = cipher.doFinal(Base64.decodeBase64(text)); 126 return new String(result); 127 } 128 129 /** 130 * 公钥加密 131 * 132 * @param publicKeyText 133 * @param text 134 * @return 135 */ 136 public static String encryptByPublicKey(String publicKeyText, String text) throws Exception { 137 X509EncodedKeySpec x509EncodedKeySpec2 = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyText)); 138 KeyFactory keyFactory = KeyFactory.getInstance("RSA"); 139 PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec2); 140 Cipher cipher = Cipher.getInstance("RSA"); 141 cipher.init(Cipher.ENCRYPT_MODE, publicKey); 142 byte[] result = cipher.doFinal(text.getBytes()); 143 return Base64.encodeBase64String(result); 144 } 145 146 /** 147 * 构建RSA密钥对 148 * 149 * @return 150 * @throws NoSuchAlgorithmException 151 */ 152 public static RSAKeyPair generateKeyPair() throws NoSuchAlgorithmException { 153 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); 154 keyPairGenerator.initialize(1024); 155 KeyPair keyPair = keyPairGenerator.generateKeyPair(); 156 RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic(); 157 RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate(); 158 String publicKeyString = Base64.encodeBase64String(rsaPublicKey.getEncoded()); 159 String privateKeyString = Base64.encodeBase64String(rsaPrivateKey.getEncoded()); 160 RSAKeyPair rsaKeyPair = new RSAKeyPair(publicKeyString, privateKeyString); 161 return rsaKeyPair; 162 } 163 164 165 /** 166 * RSA密钥对对象 167 */ 168 public static class RSAKeyPair { 169 170 private String publicKey; 171 private String privateKey; 172 173 public RSAKeyPair(String publicKey, String privateKey) { 174 this.publicKey = publicKey; 175 this.privateKey = privateKey; 176 } 177 178 public String getPublicKey() { 179 return publicKey; 180 } 181 182 public String getPrivateKey() { 183 return privateKey; 184 } 185 186 } 187 188}

将 JSESSION 带到cookie中发送POST请求(参数包含用户名和RSA公钥加密后的密码),请求地址:/cas/v1/tickets,请求类型POST,Content-Type: application/x-www-form-urlencoded

1curl -i -X POST \ 2 http://cas.gfstack.geo:8080/cas/v1/tickets \ 3 -H 'Content-Type: application/x-www-form-urlencoded' \ 4 --cookie 'JSESSIONID=2AC7AF14F7798FA770E927F8EFB356FE' \ 5 -d 'username=admin&password=ifP607Amuqxy19VUvZZ79Aq9pTzCYbShrLtSzChhm+GAmtCWUQyryCPjnh7/RhWow2qOSkCy/JynFtkznvwRgofDKN7aCOPDiIEe04IyDMOCEpCws/su+Gp4Jr2kUZHKR6iJ9Ht/adNQqFZ+r2RPiaJSIvNMYSTnY2RBZvwyxNY='

返回如下:

1HTTP/1.1 201 2Cache-Control: no-store 3Location: http://cas.gfstack.geo:8080/cas/v1/tickets/TGT-6-NK2bhU6TddIyeqmY94BKm2dzFc7BuPQHaMe1pUhX2RrVBmFKQp-cas01.example.org 4Content-Type: text/html;charset=UTF-8 5Content-Length: 382 6Date: Tue, 14 May 2019 04:08:13 GMT 7 8<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\"><html><head><title>201 Created</title></head><body><h1>TGT Created</h1><form action="http://cas.gfstack.geo:8080/cas/v1/tickets/TGT-6-NK2bhU6TddIyeqmY94BKm2dzFc7BuPQHaMe1pUhX2RrVBmFKQp-cas01.example.org" method="POST">Service:<input type="text" name="service" value=""><br><input type="submit" value="Submit"></form></body></html>

获取请求头中的 Location

三、发送POST请求获取ST

请求地址:上一步返回的 Location ,请求类型:POST,Content-Type: application/x-www-form-urlencoded

1curl -X POST \ 2 http://cas.gfstack.geo:8080/cas/v1/tickets/TGT-6-NK2bhU6TddIyeqmY94BKm2dzFc7BuPQHaMe1pUhX2RrVBmFKQp-cas01.example.org \ 3 -H 'Content-Type: application/x-www-form-urlencoded' \ 4 -d 'service=http%3A%2F%2F192.168.36.158%3A8080%2Fdemo%2F'

其中 service 是CAS客户端地址(综合运维、服务中心、数据中心等),需要经过encodeURI编码

返回如下:

ST-9-kzS0oIyS9DZmveLy4vZs-cas01.example.org

返回内容即是ST,成功拿到ST即成功使用REST登录

四、关于ST校验

请参照:https://www.cnblogs.com/nihaorz/p/10445514.html 部署cas-sample-java-webapp应用完成校验

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )