Beego: Issue when using cors in beego

Created on 6 Feb 2017  ·  8Comments  ·  Source: astaxie/beego

Please answer these questions before submitting your issue. Thanks!

  1. What version of Go and beego are you using (bee version)?

| ___ \
| |_/ / ___ ___
| ___ \ / _ \ / _ \
| |_/ /| __/| __/
____/ ___| ___| v1.6.0

├── Beego : 1.7.1
├── GoVersion : go1.6.4
├── GOOS : windows
├── GOARCH : amd64
├── NumCPU : 4
├── GOPATH : C:\Projects\Go
├── GOROOT : C:\Go\
├── Compiler : gc
└── Date : Monday, 6 Feb 2017
2.What version of windows you use for running this code(go env)

set GOARCH=amd64
set GOBIN=C:\Projects\Go\bin
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Projects\Go
set GORACE=
set GOROOT=C:\Go
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GO15VENDOREXPERIMENT=1
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
set CXX=g++
set CGO_ENABLED=1

  1. What did you do?
    If possible, provide a recipe for reproducing the error.
    A complete runnable program is good.
package main

import (

    "github.com/astaxie/beego"
    "github.com/astaxie/beego/plugins/cors"
        _ "leavepost/routers"
)

func main() {

    if  beego.BConfig.RunMode == "dev" {
        beego.BConfig.WebConfig.DirectoryIndex = true
        beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
    }
    beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
        AllowOrigins:     []string{"https://*.foo.com"},
        AllowMethods:     []string{"PUT", "PATCH"},
        AllowHeaders:     []string{"Origin"},
        ExposeHeaders:    []string{"Content-Length"},
        AllowCredentials: true,
    }))
    beego.Run()
}

My Folder structure is as follows

leavepost

  1. What did you expect to see?

Enabling of the cors plugin

  1. What did you see instead?
    cannot use cors.Allow(&cors.Options literal) (type "github.com/astaxie/beego".FilterFunc) as type "leavepost/vendor/github.com/astaxie/beego".FilterFunc in argument to "leavepost/vendor/github.com/astaxie/beego".InsertFilter

Appreciate any help.Thanks

Most helpful comment

Missed a param, now add the true. Working for me.

        // 如下支持跨域
    beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
        AllowAllOrigins: true,
        // AllowOrigins:     []string{"https://*.foo.com"},
        AllowMethods:     []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
        AllowHeaders:     []string{"Origin", "Authorization", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
        ExposeHeaders:    []string{"Content-Length", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
        AllowCredentials: true,
    }), true)
        beego.Run()

All 8 comments

Ran into the same issue, would have been nice to update why this was closed?

I agree. I'm having the same issue

i have the same issue

Missed a param, now add the true. Working for me.

        // 如下支持跨域
    beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
        AllowAllOrigins: true,
        // AllowOrigins:     []string{"https://*.foo.com"},
        AllowMethods:     []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
        AllowHeaders:     []string{"Origin", "Authorization", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
        ExposeHeaders:    []string{"Content-Length", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
        AllowCredentials: true,
    }), true)
        beego.Run()

@xiaoping378

Allowing all origins with CORS is the same as leaving your car door unlocked with the keys in ignition, with your laptop in the back seat, its password is written on a stickie note, and there are photos which could be used to extort you, or cause great shame in the near future.

It really should not be done...

ran into this problem as well. What can we do other than set AllowAllOrigins to be true?

ran into this problem as well. set AllowAllOrigins to true didn't work, any ideas about this problem?

ran into this problem as well. set AllowAllOrigins to true didn't work, any ideas about this problem?

emm, seems I found the way to solve this problem, must download the file cors.go first before import it

Was this page helpful?
0 / 5 - 0 ratings