GO模拟cas登录,并读取数据
通过go后台模拟人工登录cas系统,并调用接口获取数据。分四步进行。
1、通过GET调用登录接口,获取登录需要的参数。
2、通过POS调用登录接口,进行登录。
3、通过GET调用接口,获取cookie。
4、调用数据接口,读取数据。
如下是代码:
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
)
func main() {
loginCAS()
}
func loginCAS(){
client := &http.Client{}
//post要提交的数据
dataUrlVal := url.Values{}
//第一步,通过GET登录界面,获取登录需要的参数
dyurl := "http://http://ip:port/cas/login?service=http://http://ip:port/cas"
req,err := http.NewRequest("GET",dyurl,nil)
//伪装头部
req.Header.Set("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
req.Header.Set("Accept-Encoding","gzip, deflate, br")
req.Header.Set("Accept-Language","zh-CN,zh;q=0.9")
req.Header.Set("Cache-Control","max-age=0")
req.Header.Set("Connection","keep-alive")
req.Header.Set("Host","ip:port")
req.Header.Set("Upgrade-Insecure-Requests","1")
req.Header.Set("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36")
//提交请求
resp,err := client.Do(req)
//获取cookie
cookie := strings.Split(resp.Header["Set-Cookie"][0],";")[0]
defer resp.Body.Close()
//读取返回值
result,err := ioutil.ReadAll(resp.Body)
//读取lt和execution
var lt,execution string
html := string(result)
index := strings.Index(html,"jsessionid=")
html = html[index+11:]
index = strings.Index(html,"\"")
index = strings.Index(html,"name=\"lt\" value=\"")
html = html[index+17:]
index = strings.Index(html,"\"")
lt = html[0:index]
index = strings.Index(html,"name=\"execution\" value=\"")
html = html[index+24:]
index = strings.Index(html,"\"")
execution = html[0:index]
index = strings.Index(html,"name=\"_eventId\" value=\"")
html = html[index+23:]
index = strings.Index(html,"\"")
//第二次请求,进行登录
//组织登录需要的参数
data := make(map[string]string)
data["username"] = "username"
data["password"] = "password"
data["execution"] = execution
data["submit"] = "登录"
data["_eventId"] = "submit"
data["lt"] = lt
for key,val := range data{
dataUrlVal.Add(key,val)
}
dlurl := "http://http://ip:port/cas/login"
radar := strings.NewReader(dataUrlVal.Encode())
req,err = http.NewRequest("POST",dlurl,radar)
length := len(dataUrlVal.Encode())
req.Header.Set("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
req.Header.Set("Accept-Encoding","gzip, deflate")
req.Header.Set("Accept-Language","zh-CN,zh;q=0.9")
req.Header.Set("Cache-Control","max-age=0")
req.Header.Set("Connection","keep-alive")
req.Header.Set("Content-Length",strconv.Itoa(length))
req.Header.Set("Content-Type","application/x-www-form-urlencoded")
req.Header.Set("Cookie",cookie)
req.Header.Set("Host","ip:port")
req.Header.Set("Origin","http://ip:port")
req.Header.Set("Referer","http://ip:port/cas/login?service=http://ip:port2/cas")
req.Header.Set("Upgrade-Insecure-Requests","1")
req.Header.Set("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36")
//提交登录请求,这里更换了提交方法,是因为服务端登录成功后返回302,进行了跳转,这里是为了避免跳转导致参数无法获取
resp, err = http.DefaultTransport.RoundTrip(req)
if resp != nil{
fmt.Println(err)
}
ticket := ""
//读取登录成功返回的ticket
for _,val := range resp.Header["Location"]{
if strings.Index(val,"ticket")>-1{
ticket = strings.Split(val,"?")[1]
}
}
//第二次请求,读取sid
readsidurl := "http://http://ip:port/cas?"+ticket
req,err = http.NewRequest("GET",readsidurl,nil)
//伪装头部
req.Header.Set("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
req.Header.Set("Accept-Encoding","gzip, deflate, br")
req.Header.Set("Accept-Language","zh-CN,zh;q=0.9")
req.Header.Set("Cache-Control","max-age=0")
req.Header.Set("Connection","keep-alive")
req.Header.Set("Host","ip:port")
req.Header.Set("Upgrade-Insecure-Requests","1")
req.Header.Set("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36")
//提交请求,这里同样是因为服务端返回302,避免跳转导致参数无法获取
resp,err = http.DefaultTransport.RoundTrip(req)
cookie = strings.Split(resp.Header["Set-Cookie"][0],";")[0]
//第四次请求,调用正式接口,读取数据
var paramData map[string]string //post要传输的数据,必须key value必须都是string
paramData = make(map[string]string)
DataUrlVal2 := url.Values{}
paramData["beginTime"]="2021-08-25 00:00:00"
paramData["endTime"]="2021-08-26 00:00:00"
for key,val := range paramData{
DataUrlVal2.Add(key,val)
}
readdataurl := "http://ip:port/api/v1/list"
radar = strings.NewReader(DataUrlVal2.Encode())
req,err = http.NewRequest("POST",readdataurl,radar)
length = len(DataUrlVal2.Encode())
req.Header.Set("Accept","application/json, text/javascript, */*; q=0.01")
req.Header.Set("Accept-Encoding","gzip, deflate, br")
req.Header.Set("Accept-Language","zh-CN,zh;q=0.9")
req.Header.Set("Connection","keep-alive")
req.Header.Set("Content-Length",strconv.Itoa(length))
req.Header.Set("Content-Type","application/x-www-form-urlencoded; charset=UTF-8")
req.Header.Set("Cookie",cookie)
req.Header.Set("Host","ip:port")
req.Header.Set("Origin","http://ip:port")
req.Header.Set("Referer","http://ip:port/assets/index.html")
req.Header.Set("X-Requested-With","XMLHttpRequest")
req.Header.Set("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36")
resp,err = client.Do(req)
if err != nil{
fmt.Println(err)
}
body,err := ioutil.ReadAll(resp.Body)
if err != nil{
fmt.Println(err)
}
fmt.Println("请求得到的数据")
fmt.Println(string(body))
}