You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package main
import (
c "dsSso/Controller"
"dsSso/Handler"
"dsSso/Middleware"
"dsSso/Service/ServiceJoinApp"
"dsSso/Utils"
"dsSso/Utils/ConfigUtil"
"dsSso/Utils/DbUtil"
"dsSso/Utils/FileUtil"
"dsSso/Utils/RedisStorage"
"dsSso/Utils/RedisUtil"
"fmt"
"github.com/RangelReale/osin"
"github.com/gin-gonic/gin"
ginSwagger "github.com/swaggo/gin-swagger"
"github.com/swaggo/gin-swagger/swaggerFiles"
//http://www.freesion.com/article/3571182257/
_ "dsSso/docs"
)
/**
功能启动OAuth2服务器
作者:黄海
时间2020-03-25
*/
func startOAuth2Server() {
//声明OAuth2的Redis存储器
RedisStorage.OAuth2RedisStorage = &RedisStorage.RedisStorage{
Pool: RedisUtil.Pool,
KeyPrefix: ConfigUtil.OAuth2RedisKeyPrefix,
}
// 启动OAuth2的服务器
cfg := osin.NewServerConfig()
cfg.AllowGetAccessRequest = true
cfg.AllowClientSecretInParams = true
cfg.AllowedAccessTypes = append(cfg.AllowedAccessTypes, osin.REFRESH_TOKEN)
RedisStorage.OsinServer = osin.NewServer(cfg, RedisStorage.OAuth2RedisStorage)
//清理redis中关于接入系统的缓存
var db = DbUtil.Engine
sql := "select * from t_app_base"
list, _ := db.SQL(sql).Query().List()
for i := range list {
var appId = list[i]["app_id"].(string)
//删除redis查询缓存
RedisUtil.DEL("TAppBase:" + appId)
}
//这里需要循环从数据库中获取数据
list, _ = ServiceJoinApp.GetAppBaseList(1, 1000, "")
for i := 0; i < len(list); i++ {
appKey := list[i]["access_key"].(string)
secret := list[i]["secret_key"].(string)
redirectUri := list[i]["redirect_uri"].(string)
RedisStorage.OAuth2RedisStorage.CreateClient(
&osin.DefaultClient{
Id: appKey,
Secret: secret,
RedirectUri: redirectUri,
},
)
}
}
// @title 东师理想统一认证中心(OAuth2+Sso)
// @version 2.0
// @description 参考自xxl-sso
// @host 127.0.0.1:8000
func main() {
// 发布模式
//gin.SetMode(gin.ReleaseMode)
// 开发模式
gin.SetMode(gin.DebugMode)
// 开启gin服务器
r := gin.Default()
//设置静态资源
r.Static("/sso/static", "./static")
//启用日志中间件
r.Use(Middleware.LoggerToKafka())
// 允许跨域
r.Use(Utils.Cors())
// 显示Logo
var logo = FileUtil.ReadFileContent("./Config/logo.txt")
fmt.Print(logo)
//注册swagger
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
//注册启动OAuth2服务器
startOAuth2Server()
//注册登录验证拦截器
r.Use(Handler.AuthorizeHandler())
//主路由
c.GinRouter(r)
// 监听并在 0.0.0.0:8000 上启动服务
r.Run(":" + ConfigUtil.ServerPort)
}