我已经使用gorilla/muxand设置了我的 Go 后端rs/cors。当我尝试发送包含自定义标头 ( Bearer)的请求时,它失败了。
我的服务器设置如下所示:
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/users", GetUsers).Methods("GET")
router.HandleFunc("/", GetUsers).Methods("GET")
router.HandleFunc("/tweets", GetTweets).Methods("GET")
router.HandleFunc("/login", Login).Methods("POST")
router.HandleFunc("/profile/tweets", ProfileTweets).Methods("GET")
c := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PATCH"},
AllowedHeaders: []string{"Bearer", "Content_Type"},})
handler := c.Handler(router)
log.Fatal(http.ListenAndServe(":8080", handler))
Run Code Online (Sandbox Code Playgroud)
我尝试了各种其他解决方案(例如OPTIONS在Methods调用中添加。我尝试为其传递Bearer令牌的/profile/tweets端点是端点。
我不确定如何继续gorilla/mux以及如何rs/cors添加预检请求。
我得到的实际错误:
Fetch API 无法加载http://localhost:8080/profile/tweets。对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问Origin ' http://localhost:4200 '。如果不透明响应满足您的需求,请将请求的模式设置为“no-cors”以在禁用 CORS 的情况下获取资源。
谢谢!
我在 Chrome 中验证 CORS 请求时遇到问题。
我在 localhost 上运行单页应用程序,在 Azure 中运行 Web 服务。我使用 OpenIdConnect 登录。
当我像这样在 EDGE 中向后端发出 CORS 请求时,身份验证有效:
$.ajax({
type: 'get',
url: buildBackendUrl("api/Account"),
xhrFields: { withCredentials: true }
});
Run Code Online (Sandbox Code Playgroud)
但同样的方法在 Chrome 中不起作用。当我手动在浏览器中输入 Web 服务 url 时,请求已通过身份验证。
我检查了 CORS 请求的请求标头,差异在于 cookie:
ARRAfinity=...; AspNetCore.Cookies=...ARRAfinity=...为什么 Chrome 不包含所有 cookie?
编辑:这里是提琴手捕获的请求:
myapp.azurewebsites.net/api/Account/login?returnUrl=http://localhost:46563/myapp.azurewebsites.net/signin-oidclocalhost:46563/myapp-dev.azurewebsites.net/api/Account 在请求 nr3 或 4 中,我都没有看到 cookie。
无论如何,请求 nr2 ( myapp.azurewebsites.net/signin-oidc) 的响应尝试设置 cookie:
HTTP/1.1 302 Found
Cache-Control: no-cache …Run Code Online (Sandbox Code Playgroud) javascript authentication cookies google-chrome cross-domain
我正在寻找获取提交的作者/贡献者的方法。我对 github-api 真的很陌生,这给我带来的麻烦比我想象的要多。
问题描述
如果我有这样的文件的 URL ,是否有 github API 可以向我显示已提交该文件的贡献者列表?
或者,我是否需要使用多个 API 调用的结果(例如)
I'm thinking of cross-referencing the outputs of those two^ if everything else fails.
这应该返回 Pratik855
我找到了这个答案,但这并不是我想要的。虽然满足了所有要求,但我不确定如何https://api.github.com/repos/csitauthority/csitauthority.github.io/commits?=README转换为https://api.github.com/repos/csitauthority/csitauthority.github.io/commits?=HUGO/content/page/vlan-101.md基于https://github.com/csitauthority/CSITauthority.github.io/blob/master/HUGO/content/post/vlan-101.md,因为 HUGO 只能生成第三种规范 URL。
我在 spring boot rest 应用程序中使用 spring security。获取请求工作正常,但 POST/PUT/DELETE 请求给出“403 Forbidden”。下面是我的代码片段。UI 是 Angular 6
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserService userService;
@Override
protected void configure(HttpSecurity http) throws Exception {
CustomAuthorizationFilter customAuthorizationFilter = new CustomAuthorizationFilter(authenticationManager());
customAuthorizationFilter.setUserService(userService);
http.cors().and().authorizeRequests().anyRequest().authenticated().and().addFilter(customAuthorizationFilter);
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/v2/api-docs", "/configuration/ui", "/swagger-resources", "/configuration/security",
"/swagger-ui.html", "/webjars/**");
}
@Bean
public CorsConfigurationSource corsConfigurationSource() {
final CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Arrays.asList("*"));
configuration.setAllowedMethods(Arrays.asList("HEAD", "GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS")); …Run Code Online (Sandbox Code Playgroud) 这是我的 api 调用,我试图从响应头中读取 Set-Cookie
return $http(config).then(function (response) {
console.log(response.headers('Set-Cookie')); // undefined
console.log($cookies.ovrcMfa); // undefined
return response.data;
});
Run Code Online (Sandbox Code Playgroud)
和响应头是:
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Content-Type, Authorization, Set-Cookie, X-Requested-With, X-Session-Id, X-App-version, X-Target-Client, X-Origin-Domain, X-Dev-Mode
Access-Control-Allow-Methods: GET,PUT,POST,DELETE,OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: Set-Cookie
Set-Cookie : ovrc-mfa=IHfpt06ogxzLQFpwySE7vaJYg7whQ0sF; Max-Age=31536000; Path=/; Expires=Thu, 16 Jan 2020 09:30:47 GMT
Run Code Online (Sandbox Code Playgroud)
这里我想读取 Set-Cookie 值,即 ovrc-mfa
目标/背景:我需要将一个变量的值从我的 Angular 应用程序发送到页面上 iFrame 内的 ChatServer(aspx 和 javascript)。它在不同的服务器上。
我尝试过的:我正在遵循以下解决方法:https : //stackoverflow.com/a/25098153/11187561
但是,我收到错误消息:HTMLElement 类型上不存在属性“contentWindow”
我接下来还尝试了什么:通过 SO,我发现/sf/answers/2692052051/
我将它放在 ngAfterViewInit 中,但仍然出现错误。
代码:
ngAfterViewInit() {
var frame = document.getElementById('your-frame-id');
frame.contentWindow.postMessage(/*any variable or object here*/, '*');
}
Run Code Online (Sandbox Code Playgroud) 我正在使用 Vue 设置一个应用程序,并希望创建一个 API 服务,其中生产应指向 api.domain.com.br,而开发应指向 127.0.0.1:1337。
我的 API 使用 axios 发出如下请求:
axios.post('/api/v2/plan', plan)
Run Code Online (Sandbox Code Playgroud)
我的 vue.config.js 有一个这样的代理解决方案:
devServer: {
proxy: {
'/api': {
target: 'http://127.0.0.1:1337',
ws: true,
changeOrigin: true
},
}
},
Run Code Online (Sandbox Code Playgroud)
我可以从 axios 创建一个服务并在 api 上导入它:
import axios from 'axios';
const client = axios.create({
baseURL: 'https://api.domain.com.br'
});
export default client;
Run Code Online (Sandbox Code Playgroud)
但是它不适用于在 vue.config.js 上设置的开发代理
合成:
生产应用程序位于 app.domain.com.br
生产中的 Api 位于 api.domain.com.br
开发应用程序位于 127.0.0.1:8080
dev 上的 Api 位于 127.0.0.1:1337
当我构建这个应用程序时,它应该在 axios 调用上使用 api.domain.com.br。
我正在使用react-pdf在我的 Django/Wagtail 网站上内联渲染 PDF 文件。
为此,我在 HTML 模板中创建一个 ID 为 React 的 div,然后运行一个名为的文件,index.js这是一个非常简单的 React 文件,它创建一个 DocumentViewer 元素并使用 ReactDom 将其渲染到 ID 为“react”的 div。
从我的主包加载工作文件时,在生产环境中运行我的网站时出现错误,特别是有关如何无法从源“example.com”访问脚本worker.js 的错误
确切的代码并不真正相关(尽管我可以在必要时发布它,但给我带来问题的是加载react-pdf工作程序。
我按照文档的建议使用以下导入语句:
import {Document, Outline, Page} from 'react-pdf/dist/entry.webpack';
Run Code Online (Sandbox Code Playgroud)
然后,我使用 webpack 捆绑并缩小该文件,内容如下webpack.config.js:
var path = require("path");
var webpack = require('webpack');
var BundleTracker = require('webpack-bundle-tracker');
module.exports = {
context: __dirname,
entry: './project/app_name/static/js/index.js',
output: {
path: path.resolve('./project/app_name/static/bundles/'),
publicPath: '/static/bundles/',
filename: "[name]-[hash].js",
},
plugins: [
new BundleTracker({filename: './webpack-stats.json'}),
],
module: {
rules: [ …Run Code Online (Sandbox Code Playgroud) 我有两个应用程序,ASP.NET Core Web APP(应用程序1)和.NET CORE API(应用程序2)。我在(应用程序 1)中发出 AntiForgeryToken 并将其以隐藏字段的形式返回(实际上 .netcore 自动生成)。现在,当用户提交表单时,数据将转到(应用程序 2)并使用 CORS,我能够将(应用程序 1)发出的 AntiForgeryToken 的 cookie 发送到(应用程序 2),以便我可以验证它并在(应用程序 2)上进行验证添加了 validateAntiFOrgeryToken 属性,以便我可以验证它。下面是我在 chrome 开发工具响应选项卡上收到的错误。
\n\n我尝试了一切并搜索了所有内容,但这个特定错误无法找到答案。
\n\nSystem.InvalidOperationException: No service for type \'Microsoft.AspNetCore.Mvc.ViewFeatures.Filters.ValidateAntiforgeryTokenAuthorizationFilter\' has been registered.\n at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)\n at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)\n at Microsoft.AspNetCore.Mvc.ValidateAntiForgeryTokenAttribute.CreateInstance(IServiceProvider serviceProvider)\n at Microsoft.AspNetCore.Mvc.Filters.DefaultFilterProvider.ProvideFilter(FilterProviderContext context, FilterItem filterItem)\n at Microsoft.AspNetCore.Mvc.Filters.DefaultFilterProvider.OnProvidersExecuting(FilterProviderContext context)\n at Microsoft.AspNetCore.Mvc.Filters.FilterFactory.CreateUncachedFiltersCore(IFilterProvider[] filterProviders, ActionContext actionContext, List`1 filterItems)\n at Microsoft.AspNetCore.Mvc.Filters.FilterFactory.GetAllFilters(IFilterProvider[] filterProviders, ActionContext actionContext)\n at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvokerCache.GetCachedResult(ControllerContext controllerContext)\n at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvokerProvider.OnProvidersExecuting(ActionInvokerProviderContext context)\n at Microsoft.AspNetCore.Mvc.Infrastructure.ActionInvokerFactory.CreateInvoker(ActionContext actionContext)\n at Microsoft.AspNetCore.Mvc.Routing.ActionEndpointFactory.<>c__DisplayClass7_0.<CreateRequestDelegate>b__0(HttpContext context)\n at …Run Code Online (Sandbox Code Playgroud) cross-domain asp.net-core-mvc .net-core asp.net-core asp.net-core-webapi
我使用 Google Cloud Functions 创建了一个 API 端点,并尝试从 JS 提取函数中调用它。
我遇到了我很确定与 CORS 或输出格式有关的错误,但我不确定发生了什么。其他一些 SO 问题是类似的,并帮助我意识到我需要删除mode: "no-cors". 大多数提到在 BE 上启用 CORS,所以我添加了response.headers.set('Access-Control-Allow-Origin', '*')- 我在本文中了解到- 以确保启用 CORS...但我仍然收到“无法获取”错误。
完整错误(可在下面链接的实时演示中重现)是:
未捕获的错误:无法添加节点 1,因为具有该 ID 的节点已在 Store 中。(这个应该是无关的吧?)
从源“https://o2gxx.csb.app”访问“https://us-central1-stargazr-ncc-2893.cloudfunctions.net/nearest_csc?lat=37.75&lon=-122.5”已被阻止CORS 策略:预检响应中的 Access-Control-Allow-Headers 不允许请求标头字段 access-control-allow-origin。
获取 https://us-central1-stargazr-ncc-2893.cloudfunctions.net/nearest_csc?lat=37.75&lon=-122.5 net::ERR_FAILED
Uncaught (in promise) TypeError: Failed to fetch
请参阅下面的代码片段,请注意我用来<---- *** Message ***表示最近更改的代码部分的位置,这给了我这两个错误之一。
前端代码:
function getCSC() {
let lat = 37.75;
let lng = -122.5;
fetch(
`https://us-central1-stargazr-ncc-2893.cloudfunctions.net/nearest_csc?lat=${lat}&lon=${lng}`,
{
method: "GET",
// mode: "no-cors", <---- …Run Code Online (Sandbox Code Playgroud) javascript ×5
cors ×3
cross-domain ×3
cookies ×2
.net-core ×1
angularjs ×1
asp.net-core ×1
axios ×1
django ×1
github-api ×1
go ×1
http ×1
iframe ×1
mux ×1
python ×1
reactjs ×1
spring ×1
spring-boot ×1
spring-mvc ×1
typescript ×1
vue.js ×1
webpack ×1