亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

? ??? ?? Golang Go API?? JWT ?? ??

Go API?? JWT ?? ??

Dec 27, 2024 pm 08:56 PM

Implementing JWT Authentication in Go API

JWT(JSON ? ??)? ?? ?? ??? ?? API? ???? ?? ???? ????, ??? ???? API ?????? ???? ? ??? ???. ?? ?? ?? ?? ??? ?? JWT? ?? ?????? ?? ? ?? ???? ???? ???? ?? ???? ??? ??? ??????? ??????. ? ?????? ??? ??? ? ??? ???? ??? ??? ??? ???? ?????? ???? ????? ?????? ??? ? ???? ??? ???? ???? ??? Go API?? JWT ??? ???? ??? ?????.

?? ??

  • 1.21? ??

???? ??

go mod init app
go get github.com/gin-gonic/gin@v1.5.0
go get github.com/golang-jwt/jwt
go get github.com/joho/godotenv 

???? ??

├─ .env
├─ main.go
├─ middleware
│  └─ authenticate.go
└─ public
   ├─ index.html
   └─ login.html

???? ??

.env

jwt_secret = b0WciedNJvFCqFRbB2A1QhZoCDnutAOen5g1FEDO0HsLTwGINp04GXh2OXVpTqQL

? .env ???? ???????? JWT ??? ???? ???? ? ???? ?? ?? ???? ?? ?? ?? jwt_secret? ???? ????.

authenticate.go

package middleware

import (
    "net/http"
    "os"
    "strings"

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

type Claims struct {
    Id int `json:"id"`
    Name string `json:"name"`
    jwt.StandardClaims
}

func Authenticate() gin.HandlerFunc {
    return func(c *gin.Context) {
        if c.Request.URL.Path == "/" || c.Request.URL.Path == "/login" {
            c.Next()
            return
        }
        authHeader := c.GetHeader("Authorization")
        if authHeader == "" {
            c.Status(http.StatusUnauthorized)
            c.Abort()
            return
        }
        tokenString := strings.TrimPrefix(authHeader, "Bearer ")
        token, err := jwt.ParseWithClaims(tokenString, &Claims{}, func(token *jwt.Token) (interface{}, error) {
            return []byte(os.Getenv("jwt_secret")), nil
        })
        if err != nil || !token.Valid {
            c.Status(http.StatusUnauthorized)
            c.Abort()
            return
        }
        if claims, ok := token.Claims.(*Claims); ok {
            c.Set("user", claims)
        } else {
            c.Status(http.StatusUnauthorized)
            c.Abort()
            return
        }
        c.Next()
    }
}

authenticate.go ????? Gin ?????? ???? Go API?? JWT ??? ?? ??? ?????. ??? / ?? /login ??? ?? ??? ?????. ? ?? ??? ???? ????. ?? ??? ?? Bearer ??? ???? Authorization ??? ?????. ??? jwt ???? ?? ??? ?? ?? ???? ?? ???? ?????. ??? ???? ??? ??? ?? ??? 401 Unauthorized ??? ?????. ??? ?? ??? ???(?: ID ? ??)? ???? Gin ????? ???? ??? ??? ?? ???? ?????.

main.go

package main

import (
    "app/middleware"
    "net/http"
    "os"
    "time"

    "github.com/gin-gonic/gin"
    "github.com/golang-jwt/jwt"
    "github.com/joho/godotenv"
)

func main() {
    godotenv.Load()
    router := gin.Default()
    router.Use(middleware.Authenticate())
    router.LoadHTMLFiles("public/index.html", "public/login.html")

    router.GET("/", func(c *gin.Context) {
        c.HTML(http.StatusOK, "index.html", nil)
    })

    router.GET("/login", func(c *gin.Context) {
        c.HTML(http.StatusOK, "login.html", nil)
    })

    router.GET("/user", func(c *gin.Context) {
        user, _ := c.Get("user")
        claims := user.(*middleware.Claims)
        c.JSON(http.StatusOK, gin.H{"name": claims.Name})
    })

    router.POST("/login", func(c *gin.Context) {
        var login map[string]string
        c.BindJSON(&login)
        if login["name"] == "admin" && login["password"] == "1234" {
            token := jwt.NewWithClaims(jwt.SigningMethodHS256, &middleware.Claims{
                Id: 1,
                Name: login["name"],
                StandardClaims: jwt.StandardClaims{
                    IssuedAt: time.Now().Unix(),
                    ExpiresAt: time.Now().Add(24 * time.Hour).Unix(),
                },
            })
            tokenString, _ := token.SignedString([]byte(os.Getenv("jwt_secret")))
            c.JSON(http.StatusOK, gin.H{"token": tokenString})
        } else {
            c.Status(http.StatusBadRequest)
        }
    })
    router.Run()
}

main.go ??? JWT ?? ???? ??? ???? ?? Gin ?????? ???? Go ? ??? ?????. ???? ??? JWT ??? ???? ??? ????? ?????. ??? / ? /login ??? ?? ???? ? ?? index.html ? login.html??? ? ?? HTML ???? ?????.

/user ??? ?? ??? JWT ????? ??? ??? ??? ???? ???? ?????. /login POST ??? ?? ??? ??? ?? ??(?? ? ????)? ???? ???? ??? ?? JWT ??? ???? ?? ?? ??? ? ?????? ?? ????. ??? ??? ???? ?? ???? ????? ?????.

index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.3/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css" rel="stylesheet">
</head>
<body>
    <div>



<p>The index.html is a simple web page that provides a user interface for displaying the login status of a user. It uses Bootstrap for styling and Font Awesome for icons. On page load, it checks the user's authentication status by sending a request to the server with a JWT token stored in localStorage. If the user is logged in, it shows a success message with the user's name and a logout button. If not logged in, it shows a message indicating the user is not logged in and redirects them to the login page after a few seconds.</p>

<h3>
  
  
  login.html
</h3>



<pre class="brush:php;toolbar:false"><!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.3/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css" rel="stylesheet">
</head>
<body>
    <div>



<p>The login.html page provides a simple login form where users can input their username and password. It uses Bootstrap for styling and Font Awesome for icons. When the user submits the form, a JavaScript function login() sends a POST request to the /login endpoint with the entered credentials. If the login is successful, the server returns a JWT token, which is stored in localStorage. The page then redirects the user to the home page (/). If the login fails, an error message is displayed.</p>

<h2>
  
  
  Run project
</h2>



<pre class="brush:php;toolbar:false">go run main.go

? ????? ?? http://localhost:8080

?? ?????. ? ??? ???? ?? ? ????.

Implementing JWT Authentication in Go API

???

? ? ?? ??? ???? ???????.

Implementing JWT Authentication in Go API

??? ??? ??? ????? ?????, ???? ???? ??? ?????.

Implementing JWT Authentication in Go API

????? ?? ??? ??? ????? ?? ?? ??? ? ????. ?? ?? ???? ??? ??? JWT ??? ???? ?? ??? ???? ???????.

Implementing JWT Authentication in Go API

??

????? Go API?? JWT ??? ???? ??? ??? ???? ???? ?? ??? ?? ??? ?????. golang-jwt/jwt ???? ?? Gin ?????? ???? ?? ?? ??? ??????? ?? ??? ? ????. JWT ??? ??? ?? ???? ??? ?? ??? ???? ???? ??? ??? ?? ??? ??? ?????. ????? ??? ???? ???? ??? ???? ??? ??? ???? ? ??? ?????. ? ??? ?? ????? ??? ??? ???? ????? ?? API ????? ???? ?????.

?? ??: https://github.com/stackpuz/Example-JWT-Go

? ? ?? CRUD ? ? ???: https://stackpuz.com

? ??? Go API?? JWT ?? ??? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? ????? ??
? ?? ??? ????? ???? ??? ??????, ???? ?????? ????. ? ???? ?? ???? ?? ??? ?? ????. ???? ??? ???? ???? ??? ?? admin@php.cn?? ?????.

? AI ??

Undresser.AI Undress

Undresser.AI Undress

???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover

AI Clothes Remover

???? ?? ???? ??? AI ?????.

Video Face Swap

Video Face Swap

??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

???

??? ??

???++7.3.1

???++7.3.1

???? ?? ?? ?? ???

SublimeText3 ??? ??

SublimeText3 ??? ??

??? ??, ???? ?? ????.

???? 13.0.1 ???

???? 13.0.1 ???

??? PHP ?? ?? ??

???? CS6

???? CS6

??? ? ?? ??

SublimeText3 Mac ??

SublimeText3 Mac ??

? ??? ?? ?? ?????(SublimeText3)

???

??? ??

??? ????
1597
29
PHP ????
1488
72
NYT ?? ??? ??
131
836
???
Golang Frontend ?? ?????? Golang Frontend ?? ?????? Jul 08, 2025 am 01:44 AM

Golang? ?? ??? ??? ????? ??? ?? ???? ??? ? ??? ? ?? ????. ?? ??? ???, ?? ?? ? ??? ?? ?????? ????? API ??, ???? ???, ?? ???, ?????? ?? ? CLI ??? ?? ??? ?? ????? ???? ? ?????. Golang? ? ??? ??? ?? ??? ???? Gopherjs? ?? JavaScript? ?????? Tinygo? ?? WebAssembly?? ????? ??? ?? ??? ???? ?? ??? ???? HTML ???? ?? ? ? ????. ??? ???? ??? ?? ??? ??? ??JavaScript/TypeScript ? ???? ???????. ??? Golang? ??? ???? ???? ?? ?? ??? ? ?????.

Golang?? GraphQL API? ???? ?? Golang?? GraphQL API? ???? ?? Jul 08, 2025 am 01:03 AM

GO?? GraphQlapi? ????? GQLGEN ?????? ???? ?? ???? ????? ?? ????. 1. ?? ???? ???? ?? ?? ??? ???? GQLGEN? ?? ??? ?????? ??????. 2. ?? ?? GraphQLSchema? ???? POST ?? ? ?? ??? ??? ?? API ?? ? ?? ??? ??????. 3. ?? ?? ????? ????? ?? ??? ???? Resolver?? ???? ??? ?????. 4. ????? ??? Qlhandler? httpserver? ???? ?? ???? ?? API? ???????. ?? ?? ?? ??, ?? ??, ?? ??? ? ?? ??? ???? ???? ?? ??? ?????.

GO? ???? ?? GO? ???? ?? Jul 09, 2025 am 02:37 AM

GO? ???? ?? ??? ??? ???? ?? ??? ???? ??? ???? ????. 1. ?? ???? ?? ???? ??????? ?? ? ???? ??????. Windows? .msi ??? ???? MacOS? .pkg ??? ???? Linux? .tar.gz ??? ???? /usr /local ????? ??? ????. 2. Linux/MacOS?? ?? ??, ?? ~/.bashrc ?? ~/.zshrc? ???? ??? Gopath? ???? Windows Set ??? ??? ???? ?????. 3. ?? ??? ???? ??? ???? ??? ???? Hello.Go? ???? ?? ? ??? ???? ??????. ???? ???? ?? ?? ? ??

go sync.waitgroup ?? go sync.waitgroup ?? Jul 09, 2025 am 01:48 AM

sync.waitgroup? ?? ? ??? ??? ?? ? ??? ???? ? ?????. ??? ??? ? ?? ??? ?? ?? ??? ???? : ??, ?? ? ??. 1. Aadd (n) ?? ? ?? ? ?? ?????. 2. DONE ()? ? ? ??? ??? ???? ???? 1 ? ?? ???. 3. Wait ()? ?? ??? ?? ? ??? ?? ? ??? ?????. ?? ??? ?? ?? ?? : ADD? ?? ? ???? ????????. ?? ??? ??? DON? ????? ??????. ??? ?? ???? ?? ????. ? ???? ?? ???, ?? ??? ?? ? ?? ?????? ????? ??? ????? ????? ?? ? ? ????.

?? ????? ?????? ?? ????? ?????? Jul 09, 2025 am 02:46 AM

Go? Embed ???? ???? ? ???? ??? HTML, CSS, ?? ? ?? ??? ???? ? ??? ?? ???? ????? ?? ???? ? ????. 1. ?? ? ???? ????? ??????. 2. ??/*? ?? ?? ????? ??? ? ??? embed.fs? ?? ?? ?? ??? ??? ? ????. 3. ?? ?? ?? ?? ??? ?? ??? ?? ??? ???? ???? ????? ?? ????. 4. ???? ???? ?? ???, ?? ?? ?? ? ?? ?? ?????????. Embed? ???? ??? ??? ????? ???? ??? ??? ? ? ????.

???/??? ??? ?????? ???/??? ??? ?????? Jul 20, 2025 am 04:14 AM

??? ? ??? ??? ??? ?? ???? ? ??? ??? ???? ? ????. 1. ?? ?????? ??, ???, ??, ??? ? ??? ???? ? ???? ??? ? ???? ????. 2. ??? ? ??? ??, ?? ??, ??? ???, ??? ?? ?? ?? ???? ??? ?? ??, ?? ???, ??? ?? ??, ?? ?? ?? ?? ?? ??? ? ????. 3. FFMPEG, OPENCV, WEBRTC, GSTREAMER ? ?? ??? ???? ??? ???? ?? ????. 4. ?? ?? ???? ???? ??, ???? ??? ??? ?? ??, ?? ??? ? ??? ?? ?????? ???????. ??? ?? ???? ????? ?? ???? ??? ??? ????? ? ??????.

?? ?? ? ??? ???? ?? ?? ?? ? ??? ???? ?? Jul 15, 2025 am 03:05 AM

?? ?? ??? ? ??? ???? ?? ??? ????. ??? Net/HTTP ???? ???? ?? ???? ???? ? ????. 1. net/http? ???? ?? ??? ??? ??????. ?? ?? ??? ???? ? ?? ??? ?? ??? ????. 2. ??? ?? : Servemux? ???? ?? ??? ? ??? ?? ?? ????? ??? ?????. 3. ???? ?? : ?? ?? ? ?? ??? ? ?? ?????? ???? ??? ??? ?????. 4. ?? ?? ??? : http.fileserver? ?? HTML, CSS ? JS ??? ?????. 5. ?? ? ?? : HTTPS ???, ?? ??? ??? ???? ?? ? ??? ????? ?? ?? ??? ?????. ??? ?? ???? ????? ??? ???? ?? ? ?? ????.

?? ???? ?????? ?? ???? ?????? Jul 14, 2025 am 02:54 AM

Select Plus Default? ??? ?? ??? ???? ??? ?? ????? ??? ? Select? ?? ??? ????? ???? ????. 1. ???? ?? ???? ???? ?? ? ? ??? ?? ??? ?? ??? ?? ?????. 2. ??? ??. ?? ?? ?? ????? ???? ?????. ??? ?? ?? ???? ?? ?? ????. 3. ?? ??? ????, ??? ?? ??? ???? ?? ?? ????? ???? ???????. ?? ??? ?? ?? ??? ?? ???? ?? ? ? ??? ?? ? ??? ?? ????? ??? ???? ????.

See all articles