|
|
package SSOAccountOpenAPI
|
|
|
|
|
|
import (
|
|
|
"dsSupport/MyModel/MySwagger"
|
|
|
"dsSupport/MyModel/SSOAccount/SSOAccountService"
|
|
|
"fmt"
|
|
|
"github.com/gin-gonic/gin"
|
|
|
goCas "github.com/go-cas/cas"
|
|
|
//"log"
|
|
|
"net/http"
|
|
|
)
|
|
|
|
|
|
var casURL = "http://10.10.14.199:8080/dsssoserver/" //单点登录地址
|
|
|
|
|
|
//type casMiddleware struct {
|
|
|
// casClient *goCas.Client
|
|
|
// handler http.Handler
|
|
|
//}
|
|
|
//type Options = goCas.Options
|
|
|
|
|
|
func Login(c *gin.Context) {
|
|
|
res, msg := SSOAccountService.Login()
|
|
|
|
|
|
if res {
|
|
|
c.JSON(http.StatusOK, MySwagger.SSOResult{
|
|
|
Success: true,
|
|
|
Message: msg,
|
|
|
})
|
|
|
} else {
|
|
|
c.JSON(http.StatusOK, MySwagger.SSOResult{
|
|
|
Success: false,
|
|
|
Message: msg,
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func Logout(c *gin.Context) {
|
|
|
var w http.ResponseWriter = c.Writer
|
|
|
var r *http.Request = c.Request
|
|
|
goCas.RedirectToLogout(w, r)
|
|
|
|
|
|
fmt.Println("LOGOUT OK!")
|
|
|
|
|
|
c.JSON(http.StatusOK, MySwagger.SSOResult{
|
|
|
Success: true,
|
|
|
Message: "LOGOUT OK!!",
|
|
|
})
|
|
|
}
|
|
|
|
|
|
// 后台登陆 godoc
|
|
|
// @Summary 后台登陆
|
|
|
// @Tags account
|
|
|
// @ID loginAccount
|
|
|
// @Accept json
|
|
|
// @Produce json
|
|
|
// @Param input body MySwagger.AccountSwag true "账号密码"
|
|
|
// @Success 200 {object} MySwagger.Result
|
|
|
// @Failure 400 {object} MySwagger.Result
|
|
|
// @Router /support/ssoAccount/userInfo [post]
|
|
|
func UserInfo(c *gin.Context) {
|
|
|
//var w http.ResponseWriter = c.Writer
|
|
|
ip := GetRequestIP(c)
|
|
|
fmt.Println("ip:", ip)
|
|
|
var r *http.Request = c.Request
|
|
|
|
|
|
//goCas.RedirectToLogin(w, r)
|
|
|
|
|
|
IsAuthenticated := goCas.IsAuthenticated(r)
|
|
|
|
|
|
fmt.Println("IsAuthenticated:", IsAuthenticated)
|
|
|
|
|
|
|
|
|
fmt.Println("goCas.Username(c.Request):", goCas.Username(c.Request))
|
|
|
|
|
|
CASAttributes, ok := c.Get("CASAttributes") //取值 实现了跨中间件取值
|
|
|
if !ok{
|
|
|
CASAttributes = "default CASAttributes"
|
|
|
}
|
|
|
fmt.Println("CASAttributes:", CASAttributes)
|
|
|
|
|
|
|
|
|
res, msg := SSOAccountService.UserInfo()
|
|
|
|
|
|
if res {
|
|
|
c.JSON(http.StatusOK, MySwagger.SSOResult{
|
|
|
Success: true,
|
|
|
Message: msg,
|
|
|
Data: CASAttributes,
|
|
|
})
|
|
|
} else {
|
|
|
c.JSON(http.StatusOK, MySwagger.SSOResult{
|
|
|
Success: false,
|
|
|
Message: msg,
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
// 后台登陆 godoc
|
|
|
// @Summary 后台登陆1
|
|
|
// @Tags account
|
|
|
// @ID loginAccount
|
|
|
// @Accept json
|
|
|
// @Produce json
|
|
|
// @Param input body MySwagger.AccountSwag true "账号密码"
|
|
|
// @Success 200 {object} MySwagger.Result
|
|
|
// @Failure 400 {object} MySwagger.Result
|
|
|
// @Router /support/ssoAccount/userInfo1 [post]
|
|
|
func UserInfo1(c *gin.Context) {
|
|
|
//var casURL = "http://10.10.14.199:8080/dsssoserver/" //单点登录地址
|
|
|
//redirectURL, err := url.Parse(casURL)
|
|
|
//if err != nil {
|
|
|
// log.Fatal(err)
|
|
|
// return
|
|
|
//}
|
|
|
//
|
|
|
//casOptions := goCas.Options{
|
|
|
// URL: redirectURL,
|
|
|
// SendService: true,
|
|
|
//}
|
|
|
//
|
|
|
//MiddlewareFunc(&casOptions)
|
|
|
|
|
|
|
|
|
|
|
|
CASUsername, ok := c.Get("CASUsername") //取值 实现了跨中间件取值
|
|
|
if !ok{
|
|
|
CASUsername = "default user"
|
|
|
}
|
|
|
fmt.Println("CASUsername:", CASUsername)
|
|
|
|
|
|
|
|
|
res, msg := SSOAccountService.UserInfo()
|
|
|
|
|
|
if res {
|
|
|
c.JSON(http.StatusOK, MySwagger.SSOResult{
|
|
|
Success: true,
|
|
|
Message: msg,
|
|
|
Data: CASUsername,
|
|
|
})
|
|
|
} else {
|
|
|
c.JSON(http.StatusOK, MySwagger.SSOResult{
|
|
|
Success: false,
|
|
|
Message: msg,
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//func (casMiddleware casMiddleware) authed(c *gin.Context) {
|
|
|
// c.Set("CASUsername", goCas.Username(c.Request))
|
|
|
// fmt.Println("goCas.Username(c.Request):", goCas.Username(c.Request))
|
|
|
// c.Set("CASAttributes", goCas.Attributes(c.Request))
|
|
|
// c.Next()
|
|
|
// return
|
|
|
//}
|
|
|
//
|
|
|
//func (casMiddleware casMiddleware) middlewareFunc(c *gin.Context) {
|
|
|
// casMiddleware.handler.ServeHTTP(c.Writer, c.Request)
|
|
|
// if goCas.IsAuthenticated(c.Request) {
|
|
|
// casMiddleware.authed(c)
|
|
|
// return
|
|
|
// }
|
|
|
// c.Abort()
|
|
|
//}
|
|
|
//
|
|
|
//
|
|
|
//func MiddlewareFunc(options *Options) gin.HandlerFunc {
|
|
|
// casClient := goCas.NewClient((*goCas.Options)(options))
|
|
|
// rawHandler := func(res http.ResponseWriter, req *http.Request) {
|
|
|
// if goCas.IsAuthenticated(req) {
|
|
|
// return
|
|
|
// }
|
|
|
// if goCas.IsNewLogin(req) {
|
|
|
// return
|
|
|
// }
|
|
|
// casClient.RedirectToLogin(res, req)
|
|
|
// }
|
|
|
// return casMiddleware{
|
|
|
// casClient: casClient,
|
|
|
// handler: casClient.HandleFunc(rawHandler),
|
|
|
// }.middlewareFunc
|
|
|
//}
|
|
|
|
|
|
//获取ip
|
|
|
func GetRequestIP(c *gin.Context)string{
|
|
|
reqIP := c.ClientIP()
|
|
|
if reqIP == "::1" {
|
|
|
reqIP = "127.0.0.1"
|
|
|
}
|
|
|
return reqIP
|
|
|
}
|