Golang中解决CORS策略阻塞问题:预检请求未通过访问控制检查
Golang中解决CORS策略阻塞问题:预检请求未通过访问控制检查
已被CORS策略阻止:预检请求的响应未通过访问控制检查:它没有HTTP正常状态。
我创建了一个旅行服务器,它运行正常,我们能够通过Insomnia发出POST请求,但当我们通过前端的axios发出POST请求时,会发送错误
我们在axios中的请求:
let config = {
headers: {
"Content-Type": "application/json",
'Access-Control-Allow-Origin': '*',
}
}
let data = {
"id": 4
}
axios.post('http://196.121.147.69:9777/twirp/route.FRoute/GetLists', data, config)
.then((res) => {
console.log(res)
})
.catch((err) => {
console.log(err)
});
}
我的go文件:
func setupResponse(w *http.ResponseWriter, req *http.Request) {
(*w).Header().Set("Access-Control-Allow-Origin", "*")
(*w).Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
(*w).Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
}
func WithUserAgent(base http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ua := r.Header.Get("Jwt")
ctx = context.WithValue(ctx, "jwt", ua)
r = r.WithContext(ctx)
setupResponse(&w, r)
base.ServeHTTP(w, r)
})
}
const (
host = "localhost"
port = 5432
user = "postgres"
password = "postgres"
dbname = "postgres"
)
func main() {
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
"password=%s dbname=%s sslmode=disable",
host, port, user, password, dbname)
server := &s.Server{psqlInfo}
twirpHandler := p.NewFinanceServiceServer(server, nil)
wrap := WithUserAgent(twirpHandler)
log.Fatalln(http.ListenAndServe(":9707", wrap))
}
更多关于Golang中解决CORS策略阻塞问题:预检请求未通过访问控制检查的实战教程也可以访问 https://www.itying.com/category-94-b0.html
3 回复
错误信息
已被 CORS 策略阻止:预检请求的响应未通过访问控制检查:未返回 HTTP 正常状态。
我通过将代码包装到 Gorilla 的处理器中解决了这个问题。
在Golang中处理CORS问题时,预检请求(OPTIONS方法)需要被正确处理。你的中间件没有显式处理OPTIONS请求,这会导致预检失败。以下是修复方案:
func setupResponse(w *http.ResponseWriter, req *http.Request) {
(*w).Header().Set("Access-Control-Allow-Origin", "*")
(*w).Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
(*w).Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
}
func WithUserAgent(base http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
setupResponse(&w, r)
// 显式处理OPTIONS预检请求
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
ctx := r.Context()
ua := r.Header.Get("Jwt")
ctx = context.WithValue(ctx, "jwt", ua)
r = r.WithContext(ctx)
base.ServeHTTP(w, r)
})
}
更完整的CORS处理可以使用专门的中间件:
func CORSMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, Jwt")
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
next.ServeHTTP(w, r)
})
}
func main() {
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
host, port, user, password, dbname)
server := &s.Server{psqlInfo}
twirpHandler := p.NewFinanceServiceServer(server, nil)
// 使用CORS中间件包装处理器
handler := CORSMiddleware(twirpHandler)
log.Fatalln(http.ListenAndServe(":9707", handler))
}
关键修改点:
- 在中间件中显式处理OPTIONS方法,返回200状态码
- 确保预检请求不会继续传递到业务逻辑层
- 在Access-Control-Allow-Headers中包含实际使用的自定义头(如Jwt)
这样配置后,前端的axios请求应该能正常通过CORS检查。


