Vue系列第五篇:Vue2(Element UI) + Go(gin框架) + nginx开发登录页面及其校验登录功能

2023-11-14

   本篇使用Vue2开发前端,Go语言开发服务端,使用nginx代理部署实现登录页面及其校验功能。

目录

1.部署结构

2.Vue2前端

2.1代码结构

2.1源码

3.Go后台服务

3.2代码结构

3.2 源码

3.3单测效果

4.nginx

5.运行效果

6.问题总结


1.部署结构

 

2.Vue2前端

2.1代码结构

2.1源码

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>demo</title>
  </head>
  <body>
    <div id="myapp"></div>
    <!-- built files will be auto injected -->
  </body>
</html>

src/App.vue

<template>
<div>
<router-view></router-view>
</div>
</template>

src/assets/css/reset.css

/* http://meyerweb.com/eric/tools/css/reset/ 
   v2.0 | 20110126
   License: none (public domain)
*/

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
    display: block;
}
body {
    line-height: 1;
}
ol, ul {
    list-style: none;
}
blockquote, q {
    quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
    content: '';
    content: none;
}
table {
    border-collapse: collapse;
    border-spacing: 0;
}

src/components/Home.vue

<template>
<div id="YYYYYYYYYYY" class="hello">
<button @click="goLogin">登录</button>
<el-button type="primary">你好</el-button>
<el-button type="info">你好</el-button>
<el-button type="danger">你好</el-button>
<el-button type="success">你好</el-button>
<el-tag> fsfsd dsd dsfv</el-tag>
<i class="fa fa-user"></i>
<i class="fa fa-users"></i>
</div>
</template>

<style lang="scss">
    .hello {
        background: yello;
        .el-button {
            color: red;
        }
    }
@import url('../assets/css/reset.css')
</style>


<script>
export default {
  methods: {
    goLogin () {
      this.$router.push('/login')
    }
  }
}
</script>

src/components/Login.vue

<template>
  <div class="login">
    <el-card class="box-card">
        <div slot="header" class="clearfix">
            <span>业务后台管理系统</span>
        </div>

        <el-form label-width="100px" :model="form" ref="form" :rules='rules'>
            <el-form-item label="用户名" prop='username'>
                <el-input v-model="form.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop='password'>
                <el-input type='password' v-model="form.password"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type='primary' @click="login('form')">登录</el-button>
            </el-form-item>
        </el-form>
    </el-card>
  </div>
</template>

/*
原生AJAX和Axios在使用上存在一定的区别。Axios可以支持多种方式,包括浏览器环境、node环境,而AJAX则只能在浏览器环境中使用。
Axios还支持多种请求方式,包括GET、POST、PUT、DELETE等;而AJAX只能支持GET和POST方式发送请求。此外,Axios还可以拦截请求和响应。
*/

<script>

//登录验证的封装
import {nameRule, passRule} from '../utils/validate.js'

import {setToken} from '@/utils/dealtoken.js'

export default {
  data () {
    return {
        form: {
            username: "",
            password: ""
        },
        rules: {
            username: [{validator: nameRule, required: true, trigger: "blur"}],
            password: [{validator: passRule, required: true, trigger: "blur"}]
        }
    }
  },
  methods: {
    login(form) {
        this.$refs[form].validate((valid) => {
            if (valid) {
                console.log(this.form)
                //this.$router.push('/home')
                //解决axios post请求 404  OPTIONS问题
                const posthead = {
                    headers: {
                    'Content-Type': 'text/plain;charset=utf-8',
                    },
                    //withCredentials: 'same-origin'
                }
                this.axios.post('http://localhost:8282/login', JSON.stringify(this.form), posthead).then(res => {
                    console.log(res)
                    if (res.status === 200) {
                        //localStorage.setItem('username', res.data.username)
                        setToken('username', res.data.username)
                        this.$message({message: res.data, type: 'success'})
                        this.$router.push('/home')
                        console.log("post mid")
                    }
                })
            } else {
                console.error(this.form)
            }
        })
    }
  }
}
</script>

<style lang='scss'>
    .login {
        width: 100%;
        height: 100%;
        position: absolute;
        background: #409EFF;
        .box-card {
            width: 450px;
            margin: 200px auto;
            .el-card_header {
                font-size: 34px;
            }
            .el-button {
                width: 100%;
            }
        }
    }
</style>

src/main.js

import Vue from 'vue'
import App from './App'
import 'font-awesome/css/font-awesome.min.css'
import axios from 'axios'
import router from './router'

// 挂载到原型就可以全局使用
Vue.prototype.axios = axios

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI)


new Vue({
  router,
  render: h => h(App)
}).$mount('#myapp')

src/router/index.js

import Vue from 'vue'
import Home from '@/components/Home'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login', component: () => import('@/components/Login') },
  { path: '/login', name: 'Login', component: () => import('@/components/Login') },
  { path: '/home', component: Home },
  { path: '*', component: Home }
]


export default new VueRouter({
  mode: 'history',
  routes: routes
})


src/utils/validate.js

// Token的封装 Token存放在localStorage
export function setToken(tokenkey, token) {
    return localStorage.setItem(tokenkey, token)
}

export function getToken(tokenkey) {
    return localStorage.getItem(tokenkey)
}

export function removeToken(tokenkey) {
    return localStorage.removeItem(tokenkey)
}

src/utils/dealtoken.js

//用户名匹配
export function nameRule (rule, value, callback) {
    let reg = /(^[a-zA-Z0-9]{4,10}$)/;
    if (value === "") {
        callback(new Error("请输入用户名"));
    } else if (!reg.test(value)) {
        callback(new Error("请输入4-10用户名"));
    } else {
        callback();
    }
}

//密码匹配
export function passRule (rule, value, callback) {
    let pass = /^\S*(?=\S{6,12})(?=\S*\d)(?=\S*[A-Z])(?=\S*[a-z])(?=\S*[!@#$%^&*? ])\S*$/;
    if (value === "") {
        callback(new Error("请输入密码"));
    } else if (!pass.test(value)) {
        callback(new Error("请输入6-12位密码需要包含大小写和数字及特殊字符"));
    } else {
        callback();
    }
}

plugins/element.js

import Vue from 'vue'
import { Button, Tag } from 'element-ui'

Vue.use(Button)
Vue.use(Tag)

3.Go后台服务

3.2代码结构

3.2 源码

controller/login.go

package controller

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net/http"
    "path/filepath"

    "github.com/gin-gonic/gin"
)

// post  http://127.0.0.1:8181/login
// axios.post 和 post json处理
func LoginPost(ctx *gin.Context) {
    version := ctx.DefaultQuery("version", "V1.0.0.1")

    //前端使用axios直接传递form时,axios会默认使用json,必须使用下面方式获取json数据,解析后再使用
    data, _ := ioutil.ReadAll(ctx.Request.Body)
    type UserInfo struct {
        Username string
        Password string
    }
    var u UserInfo
    err := json.Unmarshal(data, &u)
    if err != nil {
        fmt.Println(err)
    }
    username := u.Username
    password := u.Password

    fmt.Println("login info:: ", version, username, password)

    /*
        ctx.Header("Access-Control-Allow-Origin", "*")

            ctx.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token")
            ctx.Header("Access-Control-Allow-Credentials", "true")
            ctx.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
            ctx.Header("content-type", "application/json;charset=UTF-8")

            ctx.Writer.Header().Set("Access-Control-Max-Age", "86400")
            ctx.Writer.Header().Set("Access-Control-Allow-Methods", "*")
            ctx.Writer.Header().Set("Access-Control-Allow-Headers", "*")
            ctx.Writer.Header().Set("Access-Control-Expose-Headers", "*")
            ctx.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
    */

    if username == "123456" && password == "1234abcdE@" {
        ctx.String(http.StatusOK, "登录成功")
    } else {
        ctx.String(http.StatusNotFound, "用户名或密码错误")
    }
}

// http://127.0.0.1:8181/formlogin
// form表单提交处理 application/x-www-form-urlencoded
func FormLoginPost(ctx *gin.Context) {

    //第一种
    username := ctx.PostForm("username")
    password := ctx.PostForm("password")

    //第二种
    /*
        username := ctx.DefaultPostForm("username", "somebody")
        password := ctx.DefaultPostForm("password", "***")
    */

    //第三种
    /*
        username, ok := ctx.GetPostForm("username")
        if !ok {
            username = "取不到的话"
        }
        password, ok := ctx.GetPostForm("password")
        if !ok {
            password = "***"
        }
    */

    fmt.Println("FormLoginPost :: ", username, password)
    /*
        ctx.HTML(http.StatusOK, "home.html", gin.H{
            "Name":     username,
            "Password": password,
        })
    */
    ctx.JSON(http.StatusOK, gin.H{
        "Name":     username,
        "Password": password,
    })

    if username == "123456" && password == "1234abcdE@" {
        ctx.String(http.StatusOK, "登录成功")
    } else {
        ctx.String(http.StatusNotFound, "用户名或密码错误")
    }
}

// form表单提交文件上传处理 multipart/form-data
func UploadFile(ctx *gin.Context) {
    file, _ := ctx.FormFile("uploadfile")
    fmt.Println(file.Filename)
    file_path := "upload/" + filepath.Base(file.Filename)
    fmt.Println(file_path)
    ctx.SaveUploadedFile(file, file_path)
    ctx.String(http.StatusOK, "上传成功")
}

server.go

package main

import (
    "main/controller"
    "net/http"

    "github.com/gin-contrib/cors"
    "github.com/gin-gonic/gin"
)

/*
// 错误: server.go:4:2: package main/controller is not in GOROOT (/home/tiger/go/go/src/main/controller)
go mod init main

//错误: server.go:7:2: no required module provides package github.com/gin-gonic/gin; to add it:
go get github.com/gin-gonic/gin

//处理跨域框架
go get github.com/gin-contrib/cors
*/

/*
当客户端(尤其是基于 Web 的客户端)想要访问 API 时,服务器会决定允许哪些客户端发送请求。这是通过使用称为 CORS 来完成的,它代表跨源资源共享。
跨域资源共享 (CORS) 是一种机制,允许从提供第一个资源的域之外的另一个域请求网页上的受限资源。
*/

func CrosHandler() gin.HandlerFunc {
    return func(context *gin.Context) {
        context.Writer.Header().Set("Access-Control-Allow-Origin", "*")
        context.Header("Access-Control-Allow-Origin", "*") // 设置允许访问所有域
        context.Header("Access-Control-Allow-Methods", "POST,GET,OPTIONS,PUT,DELETE,UPDATE")
        context.Header("Access-Control-Allow-Headers", "Authorization, Content-Length, X-CSRF-Token, Token,session,X_Requested_With,Accept, Origin, Host, Connection, Accept-Encoding, Accept-Language,DNT, X-CustomHeader, Keep-Alive, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Pragma,token,openid,opentoken")
        context.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers,Cache-Control,Content-Language,Content-Type,Expires,Last-Modified,Pragma,FooBar")
        context.Header("Access-Control-Max-Age", "172800")
        context.Header("Access-Control-Allow-Credentials", "true")
        context.Set("content-type", "application/json") //设置返回格式是json
        //处理请求
        context.Next()
    }
}

// http://127.0.0.1:8181/ping
// http://127.0.0.1:8181/index
func main() {
    r := gin.Default()

    // 设置全局跨域访问
    //r.Use(CrosHandler())

    //cors处理跨域
    r.Use(cors.Default())

    // 返回一个json数据
    r.GET("/ping", func(c *gin.Context) {
        c.JSON(200, gin.H{
            "message": "pong",
            "num":     888,
        })
    })

    // 返回一个html页面
    r.LoadHTMLGlob("templates/*")
    r.GET("/index", func(c *gin.Context) {
        c.HTML(http.StatusOK, "index.html", nil)
    })

    r.POST("/login", controller.LoginPost)
    r.POST("/formlogin", controller.FormLoginPost)
    r.POST("/upload", controller.UploadFile)

    //r.Run()  // <===> r.Run(":8080")  监听并在 0.0.0.0:8080 上启动服务
    r.Run(":8181")
}

templates/home.html

欢迎进入首页

templates/index.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8"/>
        <title>欢迎进入首页</title>
    </head>
    <body>
        <h3>登录测试</h3>
        <hr/>

        <form action="http://localhost:8282/formlogin" method="post">
            <table border=0 title="测试">
                <tr>
                    <td>用户名:</td>
                    <td><input type="text" name="username"></td>
                </tr>
                <tr>
                    <td>密码:</td>
                    <td><input type="password" name="password"></td>
                </tr>
                <tr>
                    <td colspan=2>
                        <input type="reset" />
                        <input type="submit" value="登录" />
                    </td>
                </tr>
            </table>
        </form>
        <br>
        <h3>文件上传测试</h3>
        <hr/>
        <form action="http://localhost:8282/upload" method="post" enctype="multipart/form-data">
            <input type="file" name="uploadfile"/>
            <input type="submit" value="upload">
        </form>
    </body>
</html>

3.3单测效果

http://127.0.0.1:8181/ping


http://127.0.0.1:8181/index

 

上传文件:

8282端口nginx代理测试:

4.nginx

#user nobody;

worker_processes 1;

#error_log logs/error.log;

#error_log logs/error.log notice;

#error_log logs/error.log info;

#pid logs/nginx.pid;


 

events {

worker_connections 1024;

}

#location [ = | ~ | ~* | ^~ ] uri { }

# ~区分大小写的正则匹配;

# ~*不区分大小写的正则匹配;

# ^~常规字符串匹配;

http {

include mime.types;

default_type application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '

# '$status $body_bytes_sent "$http_referer" '

# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;

#tcp_nopush on;

#keepalive_timeout 0;

keepalive_timeout 65;

fastcgi_intercept_errors on;

#gzip on;

limit_conn_zone $binary_remote_addr zone=addr:10m;

#代理静态页面

server {

listen 80;

server_name www.liudehua.com;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {

root /home/tiger/nginx-1.22.1/nginx/html/dist;

index index.html;

try_files $uri $uri/ /index.html;

}

#404配置

#error_page 404 /404.html;

#location /404.html {

# alias /home/tiger/nginx-1.22.1/nginx/html/dist;

# index index.html index.htm;

#}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html

#

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80

#

#location ~ \.php$ {

# proxy_pass http://127.0.0.1;

#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

#

#location ~ \.php$ {

# root html;

# fastcgi_pass 127.0.0.1:9000;

# fastcgi_index index.php;

# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;

# include fastcgi_params;

#}

# deny access to .htaccess files, if Apache's document root

# concurs with nginx's one

#

#location ~ /\.ht {

# deny all;

#}

}

#代理真正的后台服务

server {

listen 8282;

location / {

proxy_pass http://127.0.0.1:8181/;

}

}


 

# another virtual host using mix of IP-, name-, and port-based configuration

#

#server {

# listen 8000;

# listen somename:8080;

# server_name somename alias another.alias;

# location / {

# root html;

# index index.html index.htm;

# }

#}


 

# HTTPS server

#

#server {

# listen 443 ssl;

# server_name localhost;

# ssl_certificate cert.pem;

# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;

# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;

# ssl_prefer_server_ciphers on;

# location / {

# root html;

# index index.html index.htm;

# }

#}

}

部署前端程序:

 启动nginx:

5.运行效果

http://www.liudehua.com/

 可以看到nginx反向代理:

 

 postman测试:

不代理测试:8181端口

 代理测试:8282端口

6.问题总结

6.1 跨域访问错误:前后台一块解决
6.2 form表单请求,axios.post请求Go解析处理
6.2 vue中的正则表达式
vscode安装插件any-rule插件
ctrl + shift + p 输入密码,选择正则表达式

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Vue系列第五篇:Vue2(Element UI) + Go(gin框架) + nginx开发登录页面及其校验登录功能 的相关文章

随机推荐

  • [ Z-Stack协议分析(一)] ZMain.c函数

    Z Stack协议分析 一 main函数解析 1 Z stack的简单介绍 Z stack是一个协议栈 是由美国TI公司德州仪器公司设计的 Z Stack协议可在官网下载 我用的还是老版本 ZStack CC2530 2 3 0 1 4 0
  • 网易校园招聘c++题目--如何让new操作符不分配内存,只调用构造函数

    问题 c 中的new操作符 通常完成两个工作 分配内存及调用相应的构造函数 请问 1 如何让new操作符不分配内存 只调用构造函数 2 这样的用法有什么用 解答 要求new显式调用构造函数 但不分配内存 题目要求不能生成内存 还要调用构造函
  • random、range和len函数的使用

    random range和len函数的使用 一 random函数 1 random random 和random Random import random num random random 生成0 1的随机浮点数0 61612881836
  • douyin23.9 deviceid和iid设备注册分析

    使用23 9版本进行注册 版本多少 其实没有那么重要 老生常谈 老规矩注册接口device register不能少吧 然后要检测设备app alert check吧 之后要发app log日志包吧 当然除了只有这些接口肯定是不行啦 加密用到
  • this指针

    this定义 this 是 C 中的一个关键字 也是一个 const 指针 它指向当前对象 通过它可以访问当前对象的所有成员 例如 void Student setname char name this gt name name void
  • 什么是paxos算法

    从前有个村 老村长退休了 需要选一个新的村长 现有俩地痞 张三 和 李四 都想当村长 想当村长 至少需要获得一半以上长老的投票 如今这一届村委会有老李 老孙 老王 三位长老担任 第一天 张三依次拜访老李 老孙 老王三位长老 与他们说 选我一
  • UML类图中类与类之间的关系

    前言 在软件系统中 类不是孤立存在的 类与类之间存在相互关系 因此 需要通过 UML 来描述这些类之间的关系 类之间具有如下几种关系 关联关系 依赖关系 泛化关系 接口与实现关系 关联关系 含义 通常将一个类的对象作为另一个类的属性 表示
  • python matlibplot时如何不显示图片只保存图片

    1 查看网上的博客好像没得到需要的结果 于是查看官方文档 既然跟show有关 我就先查询 plt show 2 在see also中查看到了ioff可以禁用交互模式 而交互模式就是在创建图时就显示出图片 禁用了之后就可以只保存而不显示了 具
  • LVS + DR + Keepalived 高可用群集构建

    文章目录 一 Keepalived 概述 1 为什么需要 keepalived 2 keepalived 是什么 3 keepalived 服务重要功能 4 keepalived 高可用故障切换转移原理 5 keepalived 体系主要模
  • 关于执行上下文的学习总结

    学习总结自 https juejin cn post 6844904145372053511 heading 1 执行上下文 Execution Context 全局执行上下文 函数执行上下文 eval执行上下文 每个执行上下文会创建 词法
  • stem函数的简单应用-matlab

    stem x 去除火柴头 仅举例常用的几个函数 其他的可参考stem 还有 stem Y 将数据序列 Y 绘制为从沿 x 轴的基线延伸的针状图 各个数据值由终止每个针状图的圆指示 如果 Y 是向量 x 轴的刻度范围是从 1 至 length
  • AI浅谈

    前言 近年来 随着 Google 的 AlphaGo 打败韩国围棋棋手李世乭之后 机器学习尤其是深度学习的热潮席卷了整个IT界 所有的互联网公司 尤其是 Google 微软 百度 腾讯等巨头 无不在布局人工智能技术和市场 百度 腾讯 阿里巴
  • 1001. 害死⼈不偿命的(3n+1)猜想(15分)

    害死 不偿命的 3n 1 猜想 15分 卡拉兹 Callatz 猜想 对任何 个 然数n 如果它是偶数 那么把它砍掉 半 如果它是奇数 那么把 3n 1 砍掉 半 这 样 直反复砍下去 最后 定在某 步得到n 1 卡拉兹在1950年的世界数
  • 【数据结构】Map 映射

    数据结构源码 接口 public interface Map
  • shell 判断某个url是否能够访问

    urlstatus curl s m 5 IL serverurl grep 200 if urlstatus then echo urlstatus is OFF fi ping也可以判断 但是要判断返回值 比较麻烦 curl下载 然后判
  • LLVM 安装(Ubuntu)

    安装命令 sudo apt get install llvm sudo apt get install clang 验证安装 llvm as version clang version 安装成功 其他安装方法 LLVM Ubuntu安装 预
  • 结构体的基础知识

    1 结构体存储原则 原则一 结构体中元素是按照定义顺序一个一个放到内存中去的 但并不是紧密排列的 从结构体存储的首地址开始 每一个元素放置到内存中时 它都会认为内存是以它自己的大小来划分的 因此元素放置的位置一定会在自己宽度的整数倍上开始
  • 开放封闭原则的理解与具体实现 C#Unity

    目录 前言与文章介绍 一 概念阐释 1 什么是开放封闭 2 开放封闭原则怎么实现 2 1 基本思路 2 2 具体方法 二 运用原则的实例说明 实现解释 实例总结 三 相关链接 前言与文章介绍 文章将先阐释开放封闭原则 然后将理论运用于实践
  • 单片机I/O口输入输出阻抗

    目录 1 阻抗定义 2 输入阻抗 3 输出阻抗 1 阻抗定义 在具有电阻 电感 电容的电路里 对交流电所起的阻碍作用叫阻抗 用Z表示 阻抗由电阻 容抗 感抗三者组成 但不是简单三者相加 单位为欧 解决单片机端口问题时 可以将阻抗简单看成电阻
  • Vue系列第五篇:Vue2(Element UI) + Go(gin框架) + nginx开发登录页面及其校验登录功能

    本篇使用Vue2开发前端 Go语言开发服务端 使用nginx代理部署实现登录页面及其校验功能 目录 1 部署结构 2 Vue2前端 2 1代码结构 2 1源码 3 Go后台服务 3 2代码结构 3 2 源码 3 3单测效果 4 nginx