Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

localhost dev mode can't access backend api because of cors #1408

Closed
SealinGp opened this issue Dec 19, 2022 · 15 comments
Closed

localhost dev mode can't access backend api because of cors #1408

SealinGp opened this issue Dec 19, 2022 · 15 comments
Assignees
Labels
question Further information is requested

Comments

@SealinGp
Copy link

SealinGp commented Dec 19, 2022

i follow the https://casdoor.org/docs/basic/server-installation for local dev .then i got this cors error.
image
image
image

my app.conf file content

appname = casdoor
httpport = 8000
runmode = dev
copyrequestbody = true
driverName = mysql
dataSourceName = root:a123456@tcp(127.0.0.1:3306)/
dbName = casdoor
tableNamePrefix =
showSql = true
redisEndpoint =
defaultStorageProvider = 
isCloudIntranet = false
authState = "casdoor"
socks5Proxy = "127.0.0.1:10082"
verificationCodeTimeout = 10
initScore = 2000
logPostOnly = false
origin = "http://localhost:7001"
staticBaseUrl = "https://cdn.casbin.org"
isDemoMode = false
batchSize = 100
ldapServerPort = 389
languages = en,zh,es,fr,de,ja,ko,ru
quota = {"organization": -1, "user": -1, "application": -1, "provider": -1}
@casbin-bot
Copy link
Contributor

@casbin-bot casbin-bot added the enhancement New feature or request label Dec 19, 2022
@SealinGp
Copy link
Author

it will be fixed in my case if i change code like this

const (
	headerOrigin           = "Origin"
	headerAllowOrigin      = "Access-Control-Allow-Origin"
	headerAllowMethods     = "Access-Control-Allow-Methods"
	headerAllowHeaders     = "Access-Control-Allow-Headers"
	headerAllowCredentials = "Access-Control-Allow-Credentials"
)

func CorsFilter(ctx *context.Context) {
	origin := ctx.Input.Header(headerOrigin)
	originConf := conf.GetConfigString("origin")

	if origin == originConf {
		ctx.Output.Header(headerAllowOrigin, origin)
		ctx.Output.Header(headerAllowMethods, "POST, GET, OPTIONS")
		ctx.Output.Header(headerAllowHeaders, "Content-Type, Authorization")
		ctx.Output.Header(headerAllowCredentials, "true")

		if ctx.Input.Method() == "OPTIONS" {
			ctx.ResponseWriter.WriteHeader(http.StatusOK)
		}
		return
	}

	// if origin != "" && originConf != "" && origin != originConf {
	// 	if object.IsAllowOrigin(origin) {
	// 		ctx.Output.Header(headerAllowOrigin, origin)
	// 		ctx.Output.Header(headerAllowMethods, "POST, GET, OPTIONS")
	// 		ctx.Output.Header(headerAllowHeaders, "Content-Type, Authorization")
	// 	} else {
	// 		ctx.ResponseWriter.WriteHeader(http.StatusForbidden)
	// 		return
	// 	}

	// 	if ctx.Input.Method() == "OPTIONS" {
	// 		ctx.ResponseWriter.WriteHeader(http.StatusOK)
	// 		return
	// 	}
	// }
}

image

@hsluoyz
Copy link
Member

hsluoyz commented Dec 20, 2022

@ziranl16

@ziranl16
Copy link
Contributor

@SealinGp Hi, may I ask which release version did you clone?

@SealinGp
Copy link
Author

@SealinGp Hi, may I ask which release version did you clone?

image

commit: 26acece
branch: master
tag: v1.189.0

@ziranl16
Copy link
Contributor

ziranl16 commented Dec 21, 2022

Hi @SealinGp , I tried to replicate the issue locally(win10) but mine seems to be working fine with all the recent commits in the master branch. I have searched online for relevant solutions, maybe you could also look at this extension Allow CORS: Access-Control-Allow-Origin to resolve the issue instead of changing the code directly?

Moreover, would you mind seeing if you could successfully deploy earlier commits?

image

image

@ziranl16
Copy link
Contributor

@SealinGp I think this issue is also similar to #1355 , maybe you can also have a look?

@SealinGp
Copy link
Author

Hi @SealinGp , I tried to replicate the issue locally(win10) but mine seems to be working fine with all the recent commits in the master branch. I have searched online for relevant solutions, maybe you could also look at this extension Allow CORS: Access-Control-Allow-Origin to resolve the issue instead of changing the code directly?

Moreover, would you mind seeing if you could successfully deploy earlier commits?

image

image

your origin conf is empty ? can you show me the origin config value ?

@ziranl16
Copy link
Contributor

your origin conf is empty ? can you show me the origin config value ?

yes,

appname = casdoor
httpport = 8000
runmode = dev
copyrequestbody = true
driverName = postgres
dataSourceName = "user=postgres password=123456 host=localhost port=5432 sslmode=disable dbname=casdoor"
dbName =
tableNamePrefix =
showSql = false
redisEndpoint =
defaultStorageProvider = 
isCloudIntranet = false
authState = "casdoor"
socks5Proxy = "127.0.0.1:10808"
verificationCodeTimeout = 10
initScore = 2000
logPostOnly = true
origin =
staticBaseUrl = "https://cdn.casbin.org"
isDemoMode = false
batchSize = 100
ldapServerPort = 389
languages = en,zh,es,fr,de,ja,ko,ru
quota = {"organization": -1, "user": -1, "application": -1, "provider": -1}

@SealinGp
Copy link
Author

your origin conf is empty ? can you show me the origin config value ?

yes,

appname = casdoor
httpport = 8000
runmode = dev
copyrequestbody = true
driverName = postgres
dataSourceName = "user=postgres password=123456 host=localhost port=5432 sslmode=disable dbname=casdoor"
dbName =
tableNamePrefix =
showSql = false
redisEndpoint =
defaultStorageProvider = 
isCloudIntranet = false
authState = "casdoor"
socks5Proxy = "127.0.0.1:10808"
verificationCodeTimeout = 10
initScore = 2000
logPostOnly = true
origin =
staticBaseUrl = "https://cdn.casbin.org"
isDemoMode = false
batchSize = 100
ldapServerPort = 389
languages = en,zh,es,fr,de,ja,ko,ru
quota = {"organization": -1, "user": -1, "application": -1, "provider": -1}

maybe you should start web server to access the page, then you can get this error, i still got the same error using your config file.

the below operation previous required:

here are my steps to start web dev server(after installed nvm-windows).

  1. web/src/Setting.js set backend api url
export const ServerUrl = "http://localhost:8000";
  1. install dependencies & start web server
# assume you located in /xxx/casdoor dir
pwd=$PWD
cd $pwd/web && nvm install v16.10.0 && nvm use v16.10.0 && npm install --global yarn && yarn install && yarn start

then access http://localhost:7001/ and press F12 to see the console log

@ziranl16
Copy link
Contributor

ziranl16 commented Dec 21, 2022

Hi @SealinGp , yup I started the frontend & backend service using the commands as shown below. Doesn't seem to have this error. I installed Go 1.6+, Node.js LTS (16 or 14), and Yarn 1.x

image

image

@SealinGp
Copy link
Author

Hi @SealinGp , yup I started the frontend & backend service using the commands as shown below. Doesn't seem to have this error. I installed Go 1.6+, Node.js LTS (16 or 14), and Yarn 1.x

image

image

i'm confused. can i see your web/src/Setting.js file content ?

@ziranl16
Copy link
Contributor

@SealinGp okay I now can replicate the error and have found the key part. In web/src/Setting.js, if the ServerUrl is empty, everything works fine. Else it results in CORS error. For your convenience, you can first leave web/src/Setting.js without modifications when running locally. I will debug the issue later.

image

@SealinGp
Copy link
Author

@SealinGp okay I now can replicate the error and have found the key part. In web/src/Setting.js, if the ServerUrl is empty, everything works fine. Else it results in CORS error. For your convenience, you can first leave web/src/Setting.js without modifications when running locally. I will debug the issue later.

image

i figure it out, just shutdown the local nginx server will be normal. thanks for your help. ^_^

@ziranl16
Copy link
Contributor

@SealinGp okay I now can replicate the error and have found the key part. In web/src/Setting.js, if the ServerUrl is empty, everything works fine. Else it results in CORS error. For your convenience, you can first leave web/src/Setting.js without modifications when running locally. I will debug the issue later.
image

i figure it out, just shutdown the local nginx server will be normal. thanks for your help. ^_^

No problem! 👍

@hsluoyz hsluoyz added question Further information is requested and removed enhancement New feature or request labels Dec 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants