diff --git a/dsSso/Const/Const.go b/dsSso/Const/Const.go new file mode 100644 index 00000000..ec5c539f --- /dev/null +++ b/dsSso/Const/Const.go @@ -0,0 +1,4 @@ +package Const + +//整数最大值 +const Int32Max = 2147483647 diff --git a/dsSso/Controller/ControllerOauth2/ControllerOauth2.go b/dsSso/Controller/ControllerOauth2/ControllerOauth2.go index 073a9fbb..1f36874d 100644 --- a/dsSso/Controller/ControllerOauth2/ControllerOauth2.go +++ b/dsSso/Controller/ControllerOauth2/ControllerOauth2.go @@ -1,9 +1,11 @@ package ControllerOauth2 import ( + "dsSso/Const" "dsSso/Const/DefaultConst" "dsSso/Controller/ControllerRecaptcha" "dsSso/Model" + "dsSso/Service/ServiceJoinApp" "dsSso/Service/ServiceLoginPerson" "dsSso/Utils/AesUtil" "dsSso/Utils/CommonUtil" @@ -15,8 +17,10 @@ import ( "dsSso/Utils/SsoUtil" "encoding/base64" "encoding/json" + "fmt" "github.com/dchest/captcha" "github.com/gin-gonic/gin" + "io/ioutil" "net/http" "strconv" "strings" @@ -36,6 +40,9 @@ func Routers(r *gin.RouterGroup) { r.GET("/getCaptcha", getCaptcha) //获取验证码图片 r.GET("/getCaptchaPng", getCaptchaPng) + //退出接口 + r.GET("/logout", logout) + r.POST("/logout", logout) return } @@ -392,3 +399,39 @@ func authorizeGet(context *gin.Context) { url := "/sso/static/index.html?redirect_uri=" + redirectUri + "&response_type=code&client_id=" + paraClientId + "&oauth_callback=" + oauthCallback context.Redirect(302, url) } + +// @Summary 退出统一认证 +// @Description 退出统一认证 +// @Tags 登录验证类 +// @Accept application/x-www-form-urlencoded +// @Produce json +// @Success 200 {string} string +// @Router /oauth2/logout [get] +func logout(context *gin.Context) { + //取出现在的cookie中的accessToken + accessToken := SsoUtil.ReadSsoCookie(context) + //获取所有接入系统的 + list, _ := ServiceJoinApp.GetJoinAppList(1, Const.Int32Max, "") + for i := range list { + //每个系统的退出地址 + logoutUri := list[i]["logout_uri"].(string) + //如果注册了退出地址的话 + if len(logoutUri) > 0 { + //开启协程进行调用 + go func() { + resp, err := http.Get(logoutUri + "?access_token=" + accessToken) + if err != nil { + fmt.Println(err) + return + } + defer resp.Body.Close() + body, _ := ioutil.ReadAll(resp.Body) + fmt.Println(string(body)) + }() + } + } + //清除cookie + SsoUtil.DeleteSsoCookie(context) + msg := "统一认证退出成功!" + context.JSON(http.StatusOK, map[string]interface{}{"success": false, "msg": msg}) +} diff --git a/dsSso/Dao/DaoSysLoginPerson/DaoSysLoginPerson.go b/dsSso/Dao/DaoSysLoginPerson/DaoSysLoginPerson.go index e7797563..cf3c79be 100644 --- a/dsSso/Dao/DaoSysLoginPerson/DaoSysLoginPerson.go +++ b/dsSso/Dao/DaoSysLoginPerson/DaoSysLoginPerson.go @@ -10,6 +10,7 @@ import ( "dsSso/Utils/LogUtil" "dsSso/models" "fmt" + "time" ) var loginModel Model.Selector @@ -97,6 +98,7 @@ func WriteLoginLog(identityId string, personId string, ip string, loginState int tLog.IpAddress = ip tLog.LoginName = loginName tLog.LoginState = int32(loginState) + tLog.LoginTime = time.Now() _, err := db.Insert(tLog) if err != nil { fmt.Println(err.Error()) diff --git a/dsSso/Utils/SsoUtil/SsoUtil.go b/dsSso/Utils/SsoUtil/SsoUtil.go index f8baea1e..4ee68e93 100644 --- a/dsSso/Utils/SsoUtil/SsoUtil.go +++ b/dsSso/Utils/SsoUtil/SsoUtil.go @@ -61,6 +61,6 @@ func ReadSsoCookie(c *gin.Context) string { 作者:黄海 时间:2020-02-22 */ -func deleteSsoCookie(c *gin.Context) { +func DeleteSsoCookie(c *gin.Context) { c.SetCookie(ConfigUtil.AccessToken, "", -1, "/", "", false, true) } diff --git a/dsSso/docs/docs.go b/dsSso/docs/docs.go index 42e7c632..220884c4 100644 --- a/dsSso/docs/docs.go +++ b/dsSso/docs/docs.go @@ -260,6 +260,29 @@ var doc = `{ ] } }, + "/oauth2/logout": { + "get": { + "description": "退出统一认证", + "consumes": [ + "application/x-www-form-urlencoded" + ], + "produces": [ + "application/json" + ], + "tags": [ + "登录验证类" + ], + "summary": "退出统一认证", + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "string" + } + } + } + } + }, "/sso/addJoinApp": { "post": { "description": "增加一条接入系统的记录", diff --git a/dsSso/docs/swagger.json b/dsSso/docs/swagger.json index 65c7e177..c65b14e6 100644 --- a/dsSso/docs/swagger.json +++ b/dsSso/docs/swagger.json @@ -244,6 +244,29 @@ ] } }, + "/oauth2/logout": { + "get": { + "description": "退出统一认证", + "consumes": [ + "application/x-www-form-urlencoded" + ], + "produces": [ + "application/json" + ], + "tags": [ + "登录验证类" + ], + "summary": "退出统一认证", + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "string" + } + } + } + } + }, "/sso/addJoinApp": { "post": { "description": "增加一条接入系统的记录", diff --git a/dsSso/docs/swagger.yaml b/dsSso/docs/swagger.yaml index d2c425aa..778bfc05 100644 --- a/dsSso/docs/swagger.yaml +++ b/dsSso/docs/swagger.yaml @@ -198,6 +198,21 @@ paths: - 登录验证类 x-lengthlimit: - captchaId: 20,20 + /oauth2/logout: + get: + consumes: + - application/x-www-form-urlencoded + description: 退出统一认证 + produces: + - application/json + responses: + "200": + description: OK + schema: + type: string + summary: 退出统一认证 + tags: + - 登录验证类 /sso/addJoinApp: post: consumes: