增加是否开启登录功能的开关

This commit is contained in:
gotoeasy 2022-07-10 20:44:06 +08:00
parent 01379e3df0
commit fd42a7a2f7
7 changed files with 56 additions and 28 deletions

View File

@ -27,6 +27,7 @@ var amqpAddr string
var amqpQueueName string
var amqpJsonFormat bool
var saveDays int
var enableLogin bool
var username string
var password string
@ -51,10 +52,16 @@ func UpdateConfigByEnv() {
amqpQueueName = Getenv("GLC_AMQP_QUEUE_NAME", "glc-log-queue") // rabbitMq队列名
amqpJsonFormat = GetenvBool("GLC_AMQP_JSON_FORMAT", true) // rabbitMq消息文本是否为json格式默认true
saveDays = GetenvInt("GLC_SAVE_DAYS", 180) // 日志分仓时的保留天数(0~180)0表示不自动删除默认180天
enableLogin = GetenvBool("GLC_ENABLE_LOGIN", false) // 是否开启用户密码登录默认“false”
username = Getenv("GLC_USERNAME", "glc") // 登录用户名默认“glc”
password = Getenv("GLC_PASSWORD", "glogcenter") // 登录密码默认“glogcenter”
}
// 取配置: 是否开启用户密码登录可通过环境变量“GLC_ENABLE_LOGIN”设定默认“false”
func IsEnableLogin() bool {
return enableLogin
}
// 取配置: 登录用户名可通过环境变量“GLC_USERNAME”设定默认“glc”
func GetUsername() string {
return username

View File

@ -34,15 +34,13 @@ func Run() {
gweb.RegisterController(method.GET, "/**/*.png", html.StaticFileController)
// 控制器
gweb.RegisterController(method.POST, contextPath+"/search", controller.LogSearchController) // 查询日志 Deprecated
gweb.RegisterController(method.POST, contextPath+"/add", controller.LogAddController) // 添加日志 Deprecated
gweb.RegisterController(method.POST, contextPath+"/v1/log/add", controller.JsonLogAddController) // 添加日志
gweb.RegisterController(method.POST, contextPath+"/v1/log/search", controller.LogSearchController) // 查询日志
gweb.RegisterController(method.POST, contextPath+"/v1/store/names", controller.StorageNamesController) // 查询日志仓名称列表
gweb.RegisterController(method.POST, contextPath+"/v1/store/list", controller.StorageListController) // 查询日志仓信息列表
gweb.RegisterController(method.POST, contextPath+"/v1/store/delete", controller.StorageDeleteController) // 删除日志仓
gweb.RegisterController(method.POST, contextPath+"/v1/user/login", controller.LoginController) // Login
gweb.RegisterController(method.POST, contextPath+"/v1/log/add", controller.JsonLogAddController) // 添加日志
gweb.RegisterController(method.POST, contextPath+"/v1/log/search", controller.LogSearchController) // 查询日志
gweb.RegisterController(method.POST, contextPath+"/v1/store/names", controller.StorageNamesController) // 查询日志仓名称列表
gweb.RegisterController(method.POST, contextPath+"/v1/store/list", controller.StorageListController) // 查询日志仓信息列表
gweb.RegisterController(method.POST, contextPath+"/v1/store/delete", controller.StorageDeleteController) // 删除日志仓
gweb.RegisterController(method.POST, contextPath+"/v1/user/enableLogin", controller.IsEnableLoginController) // 查询是否开启用户密码登录功能
gweb.RegisterController(method.POST, contextPath+"/v1/user/login", controller.LoginController) // Login
// 默认引擎空转一下,触发未建索引继续建
go ldb.NewDefaultEngine().AddTextLog("", "", "")

View File

@ -2,6 +2,7 @@ package controller
import (
"glc/cmn"
"glc/conf"
"glc/gweb"
"glc/ldb"
)
@ -9,7 +10,7 @@ import (
// 日志检索(表单提交方式)
func LogSearchController(req *gweb.HttpRequest) *gweb.HttpResult {
if req.GetFormParameter("token") != GetSessionid() {
if conf.IsEnableLogin() && req.GetFormParameter("token") != GetSessionid() {
return gweb.Error403() // 登录检查
}

View File

@ -12,14 +12,16 @@ import (
var sessionid string
func init() {
sessionid = createSessionid()
go func() {
ticker := time.NewTicker(time.Hour) // 一小时更新一次
for {
<-ticker.C
sessionid = createSessionid()
}
}()
if conf.IsEnableLogin() {
sessionid = createSessionid()
go func() {
ticker := time.NewTicker(time.Hour) // 一小时更新一次
for {
<-ticker.C
sessionid = createSessionid()
}
}()
}
}
func LoginController(req *gweb.HttpRequest) *gweb.HttpResult {
@ -32,6 +34,10 @@ func LoginController(req *gweb.HttpRequest) *gweb.HttpResult {
return gweb.Result(sessionid)
}
func IsEnableLoginController(req *gweb.HttpRequest) *gweb.HttpResult {
return gweb.Result(conf.IsEnableLogin())
}
func createSessionid() string {
ymd := cmn.GetYyyymmdd(0)
by1 := md5.Sum(cmn.StringToBytes(conf.GetUsername() + ymd))

View File

@ -12,7 +12,7 @@ import (
// 查询日志仓名称列表
func StorageNamesController(req *gweb.HttpRequest) *gweb.HttpResult {
if req.GetFormParameter("token") != GetSessionid() {
if conf.IsEnableLogin() && req.GetFormParameter("token") != GetSessionid() {
return gweb.Error403() // 登录检查
}
@ -22,7 +22,7 @@ func StorageNamesController(req *gweb.HttpRequest) *gweb.HttpResult {
// 查询日志仓信息列表
func StorageListController(req *gweb.HttpRequest) *gweb.HttpResult {
if req.GetFormParameter("token") != GetSessionid() {
if conf.IsEnableLogin() && req.GetFormParameter("token") != GetSessionid() {
return gweb.Error403() // 登录检查
}
@ -32,7 +32,7 @@ func StorageListController(req *gweb.HttpRequest) *gweb.HttpResult {
// 删除指定日志仓
func StorageDeleteController(req *gweb.HttpRequest) *gweb.HttpResult {
if req.GetFormParameter("token") != GetSessionid() {
if conf.IsEnableLogin() && req.GetFormParameter("token") != GetSessionid() {
return gweb.Error403() // 登录检查
}

View File

@ -15,22 +15,21 @@
</div>
<el-link v-if="isLogin" @click="logout">
<el-link v-if="enableLogin && isLogin" @click="logout">
<el-icon :size="26"><expand/></el-icon>
</el-link>
</el-header>
<el-aside v-if="isLogin" class="menubar x-menubar" style="width:48px">
<el-aside v-if="!enableLogin || isLogin" class="menubar x-menubar" style="width:48px">
<Menu :isCollapsed="true"></Menu>
</el-aside>
<el-main v-if="isLogin" class="main x-main" >
<el-main v-if="!enableLogin || isLogin" class="main x-main" >
<router-view></router-view>
</el-main>
<Login v-if="!isLogin"></Login>
<Login v-if="enableLogin && !isLogin"></Login>
</el-container>
</el-container>
@ -40,6 +39,7 @@
import Menu from './components/Menu.vue'
import { Expand, Fold } from '@element-plus/icons-vue'
import Login from './views/login.vue'
import api from './api'
export default {
components: {
@ -47,12 +47,24 @@ export default {
Expand,
Menu,
Login
},
},
data() {
return {
isLogin: !!sessionStorage.getItem('glctoken')
enableLogin: true,
isLogin: false,
}
},
created() {
api.enableLogin().then(rs => {
let res = rs.data
if (res.success) {
this.enableLogin = res.result;
if (res.result) {
this.isLogin = !!sessionStorage.getItem('glctoken');
}
}
});
},
methods: {
clickLogo() {
location.href = "https://github.com/gotoeasy/glogcenter"

View File

@ -50,4 +50,8 @@ export default {
formData.append("password", pass);
return post(`${BASE_URL}/glc/v1/user/login`, formData)
},
enableLogin() {
let formData = new FormData();
return post(`${BASE_URL}/glc/v1/user/enableLogin`, formData)
},
}