增加Login功能

This commit is contained in:
gotoeasy 2022-07-10 18:47:22 +08:00
parent 1efde24146
commit b7659fcd23
11 changed files with 192 additions and 32 deletions

View File

@ -27,6 +27,8 @@ var amqpAddr string
var amqpQueueName string
var amqpJsonFormat bool
var saveDays int
var username string
var password string
func init() {
UpdateConfigByEnv()
@ -49,6 +51,18 @@ 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天
username = Getenv("GLC_USERNAME", "glc") // 登录用户名默认“glc”
password = Getenv("GLC_PASSWORD", "glogcenter") // 登录密码默认“glogcenter”
}
// 取配置: 登录用户名可通过环境变量“GLC_USERNAME”设定默认“glc”
func GetUsername() string {
return username
}
// 取配置: 登录用户名可通过环境变量“GLC_PASSWORD”设定默认“glogcenter”
func GetPassword() string {
return password
}
// 取配置: 日志分仓时的保留天数(0~180)0表示不自动删除可通过环境变量“GLC_SAVE_DAYS”设定默认180天

View File

@ -28,6 +28,14 @@ func Error500(msg string) *HttpResult {
}
}
func Error403() *HttpResult {
return &HttpResult{
Code: 403,
Message: "forbidden",
Success: false,
}
}
func Error404() *HttpResult {
return &HttpResult{
Code: 404,

View File

@ -34,14 +34,15 @@ 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+"/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/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
// 默认引擎空转一下,触发未建索引继续建
go ldb.NewDefaultEngine().AddTextLog("", "", "")

View File

@ -8,6 +8,11 @@ import (
// 日志检索(表单提交方式)
func LogSearchController(req *gweb.HttpRequest) *gweb.HttpResult {
if req.GetFormParameter("token") != GetSessionid() {
return gweb.Error403() // 登录检查
}
storeName := req.GetFormParameter("storeName")
//searchKey := tokenizer.GetSearchKey(req.GetFormParameter("searchKey"))
searchKey := req.GetFormParameter("searchKey")

View File

@ -0,0 +1,48 @@
package controller
import (
"crypto/md5"
"encoding/hex"
"glc/cmn"
"glc/conf"
"glc/gweb"
"time"
)
var sessionid string
func init() {
sessionid = createSessionid()
go func() {
ticker := time.NewTicker(time.Hour) // 一小时更新一次
for {
<-ticker.C
sessionid = createSessionid()
}
}()
}
func LoginController(req *gweb.HttpRequest) *gweb.HttpResult {
username := req.GetFormParameter("username")
password := req.GetFormParameter("password")
if username != conf.GetUsername() || password != conf.GetPassword() {
return gweb.Error500("用户名或密码错误")
}
return gweb.Result(sessionid)
}
func createSessionid() string {
ymd := cmn.GetYyyymmdd(0)
by1 := md5.Sum(cmn.StringToBytes(conf.GetUsername() + ymd))
by2 := md5.Sum(cmn.StringToBytes(ymd + conf.GetPassword()))
by3 := md5.Sum(cmn.StringToBytes(ymd + "添油" + conf.GetUsername() + "加醋" + conf.GetPassword()))
str1 := hex.EncodeToString(by1[:])
str2 := hex.EncodeToString(by2[:])
str3 := hex.EncodeToString(by3[:])
return cmn.RightRune(str1, 15) + cmn.LeftRune(str2, 15) + cmn.LeftRune(str3, 30)
}
func GetSessionid() string {
return sessionid
}

View File

@ -12,18 +12,30 @@ import (
// 查询日志仓名称列表
func StorageNamesController(req *gweb.HttpRequest) *gweb.HttpResult {
if req.GetFormParameter("token") != GetSessionid() {
return gweb.Error403() // 登录检查
}
rs := cmn.GetStorageNames(conf.GetStorageRoot(), ".sysmnt")
return gweb.Result(rs)
}
// 查询日志仓信息列表
func StorageListController(req *gweb.HttpRequest) *gweb.HttpResult {
if req.GetFormParameter("token") != GetSessionid() {
return gweb.Error403() // 登录检查
}
rs := sysmnt.GetStorageList()
return gweb.Result(rs)
}
// 删除指定日志仓
func StorageDeleteController(req *gweb.HttpRequest) *gweb.HttpResult {
if req.GetFormParameter("token") != GetSessionid() {
return gweb.Error403() // 登录检查
}
name := req.GetFormParameter("storeName")
if conf.IsStoreNameAutoAddDate() && conf.GetSaveDays() > 0 {

View File

@ -14,16 +14,24 @@
</div>
</div>
<el-link v-if="isLogin" @click="logout">
<el-icon :size="26"><expand/></el-icon>
</el-link>
</el-header>
<el-aside class="menubar x-menubar" style="width:48px">
<Menu :isCollapsed="true"></Menu>
</el-aside>
<el-aside v-if="isLogin" class="menubar x-menubar" style="width:48px">
<Menu :isCollapsed="true"></Menu>
</el-aside>
<el-main class="main x-main" >
<el-main v-if="isLogin" class="main x-main" >
<router-view></router-view>
</el-main>
<Login v-if="!isLogin"></Login>
</el-container>
</el-container>
</template>
@ -31,19 +39,28 @@
<script>
import Menu from './components/Menu.vue'
import { Expand, Fold } from '@element-plus/icons-vue'
import Login from './views/login.vue'
export default {
components: {
Fold,
Expand,
Menu,
},
Login
},
data() {
return {
isLogin: !!sessionStorage.getItem('glctoken')
}
},
methods: {
clickLogo() {
location.href = "https://github.com/gotoeasy/glogcenter"
},
logout() {
sessionStorage.clear();
location.reload();
},
}
}
</script>

View File

@ -12,11 +12,11 @@ function request(url, method = 'get', data) {
}
function post(url, formData) {
formData.append('token', sessionStorage.getItem("glctoken"));
return axios.post(url,formData,{
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
// 'X-GLC-AUTH': 'glogcenter',
}
}
})
}
@ -44,22 +44,10 @@ export default {
formData.append("storeName", name);
return post(`${BASE_URL}/glc/v1/store/delete`, formData)
},
// remove(db, id) {
// return request(`/remove?database=${db}`, 'post', { id })
// },
// gc() {
// return request('/gc')
// },
// getStatus() {
// return request('/status')
// },
// addIndex(db, index) {
// return request(`/index?database=${db}`, 'post', index )
// },
// drop(db){
// return request(`/db/drop?database=${db}`)
// },
// create(db){
// return request(`/db/create?database=${db}`)
// }
login(user,pass) {
let formData = new FormData();
formData.append("username", user);
formData.append("password", pass);
return post(`${BASE_URL}/glc/v1/user/login`, formData)
},
}

View File

@ -103,6 +103,9 @@ export default {
for (let i = 0; i < names.length; i++) {
this.storageOptions.push({value: names[i], label: '日志仓:' + names[i]})
}
}else if (res.code == 403){
sessionStorage.clear();
location.reload();
}
});
},
@ -153,6 +156,9 @@ export default {
}else{
this.info = `日志总量 ${res.result.total} 条,当前条件最多匹配 ${this.data.length} 条,正展示前 ${this.data.length}`
}
}else if (res.code == 403){
sessionStorage.clear();
location.reload();
}
})
},
@ -173,6 +179,9 @@ export default {
}else{
this.info = `日志总量 ${res.result.total} 条,当前条件最多匹配 ${res.result.count} 条,正展示前 ${this.data.length}`
}
}else if (res.code == 403){
sessionStorage.clear();
location.reload();
}
}).finally(() => {

View File

@ -0,0 +1,52 @@
<template>
<el-dialog title="登录" class="x-login" v-model="dialogVisible" width="420px"
:close-on-click-modal="false" :close-on-press-escape="false" :show-close="false"
:before-close="handleClose">
<el-input placeholder="请输入用户名" v-model="username" maxlength="100"></el-input><p/>
<el-input placeholder="请输入密码" type="password" v-model="password" maxlength="100"></el-input>
<template #footer>
<span class="dialog-footer">
<el-button type="primary" @click="login" style="width:100%;"> </el-button>
</span>
</template>
</el-dialog>
</template>
<script>
import api from '../api'
export default {
data() {
return {
dialogVisible: true,
username: "",
password: "",
};
},
methods: {
login(){
this.loading = true
api.login(this.username,this.password).then(rs => {
let res = rs.data
console.info('-------------------------------', res)
if (res.success) {
sessionStorage.setItem("glctoken", res.result)
location.reload()
}else{
this.$message({type: 'error', message: res.message});
}
}).finally(() => {
this.loading = false
})
}
}
};
</script>
<style>
</style>

View File

@ -92,6 +92,9 @@ export default {
if (res.success) {
this.$message({type: 'info', message: "已删除日志仓 " + row.name});
this.search();
}else if (res.code == 403){
sessionStorage.clear();
location.reload();
}else{
this.$message({type: 'error', message: res.message});
}
@ -112,6 +115,9 @@ export default {
this.data = res.result.data || [];
this.info = res.result.info;
// document.querySelector('.el-scrollbar__wrap').scrollTop = 0; //
}else if (res.code == 403){
sessionStorage.clear();
location.reload();
}
}).finally(() => {
this.loading = false