Giter Site home page Giter Site logo

Comments (5)

rickwang7712 avatar rickwang7712 commented on August 22, 2024 3

I have tried this way, and it seems to work fine.
However, I'm just a beginner. Maybe there is some problem that I haven't discovered...

Use CSRF() as normal gin middleware, you can pass to group or router like
"xxx.Use(middlewares.CSRF())".

package middlewares

import (
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/gorilla/csrf"
	adapter "github.com/gwatts/gin-adapter"
)

var csrfMd func(http.Handler) http.Handler

func init() {
	csrfMd = csrf.Protect([]byte("Taiwan NO.1."),
		csrf.MaxAge(0),
		csrf.Secure(true),
		csrf.ErrorHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			w.WriteHeader(http.StatusForbidden)
			w.Write([]byte(`{"message": "Forbidden - CSRF token invalid"}`))
		})),
	)
}

func CSRF() gin.HandlerFunc {
	return adapter.Wrap(csrfMd)
}

from csrf.

fmpwizard avatar fmpwizard commented on August 22, 2024 1

this shows the options you have
gin-gonic/contrib#63

from csrf.

elithrar avatar elithrar commented on August 22, 2024 1

@rickwang7712 0- I don't see any issue with your code there - adapter.Wrap is what Gin provides to leverage http.Handler implementations instead of Gin-specific types.

from csrf.

hongjinlin avatar hongjinlin commented on August 22, 2024

how?

from csrf.

elithrar avatar elithrar commented on August 22, 2024

(Updated the title to be more searchable against Gin or gin-gonic)

from csrf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.