OAuth2 - 检索TOKEN时OPTIONS请求的状态401(OAuth2 - Status 401 on OPTIONS request while retrieving TOKEN)
我们的堆栈使用Backbone作为客户端应用程序,Spring Boot作为RESTful API。
我们正在尝试使用OAuth2进行基本身份验证,用户提供用户名和密码。
我们使用Spring Security进行身份验证,使用jQuery $ .ajax方法进行请求。 然而,我们得到的响应是在预检OPTIONS请求上的401(未授权)状态,然后我们甚至可以使用我们的秘密进行POST授权。 但是我们可以毫无问题地发布或获取任何其他资源。 OPTIONS请求的服务器响应是200(ok),然后它跟进POST请求。
那么为什么来自/ oauth / token响应的OPTIONS请求具有401状态,即使它不应该? 它不会让我们自己授权,因为它会停留在我们无法添加授权标头的OPTIONS请求中。
这就是我们处理前端请求的方式:
$.ajax({ url: "http://localhost:8080/oauth/token", type: "POST", beforeSend: function(xhr) { xhr.setRequestHeader("Authorization", "Basic Y2xpZW50YXBwOjEyMzQ1Ng=="); }, data: { password: "qwerty", username: "jkowalski", grant_type: "password" } });
这是我们的OAuth2配置:
@Configuration public class OAuth2ServerConfiguration { private static final String RESOURCE_ID = "restservice"; @Configuration @EnableResourceServer protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter { [...] @Override public void configure(HttpSecurity http) throws Exception { http.csrf().disable(); http .authorizeRequests() .anyRequest().permitAll(); } } [...] @Configuration @EnableAuthorizationServer protected static class OAuth2AuthorizationConfig extends AuthorizationServerConfigurerAdapter { @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { // @formatter:off clients .inMemory() .withClient("clientapp") .authorizedGrantTypes("password", "refresh_token") .authorities("USER","ADMIN") .scopes("read", "write") .resourceIds(RESOURCE_ID) .secret("123456"); // @formatter:on } [...] }
Our stack uses Backbone as our client-side app and Spring Boot as a RESTful API.
We're trying to make basic authentication using OAuth2 with user providing username and password.
We use Spring Security for authentication and jQuery $.ajax method for making requests. However the response we get is 401(unauthorized) status on preflight OPTIONS request before we can even POST header with our secret to authorize. However we can POST or GET any other resource without any problems. Server response for OPTIONS request is 200(ok) and then it follows up with POST request.
So why is that an OPTIONS request from /oauth/token responses with 401 status even when it shouldn't? It won't let us authorize ourselves because it get's stuck at OPTIONS request in which we can't add authorization header.
This is how we handle requests on front-end:
$.ajax({ url: "http://localhost:8080/oauth/token", type: "POST", beforeSend: function(xhr) { xhr.setRequestHeader("Authorization", "Basic Y2xpZW50YXBwOjEyMzQ1Ng=="); }, data: { password: "qwerty", username: "jkowalski", grant_type: "password" } });
This is our OAuth2 config:
@Configuration public class OAuth2ServerConfiguration { private static final String RESOURCE_ID = "restservice"; @Configuration @EnableResourceServer protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter { [...] @Override public void configure(HttpSecurity http) throws Exception { http.csrf().disable(); http .authorizeRequests() .anyRequest().permitAll(); } } [...] @Configuration @EnableAuthorizationServer protected static class OAuth2AuthorizationConfig extends AuthorizationServerConfigurerAdapter { @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { // @formatter:off clients .inMemory() .withClient("clientapp") .authorizedGrantTypes("password", "refresh_token") .authorities("USER","ADMIN") .scopes("read", "write") .resourceIds(RESOURCE_ID) .secret("123456"); // @formatter:on } [...] }
原文:https://stackoverflow.com/questions/30622624
最满意答案
您可以检查您的搜索控制器是否处于活动状态(我假设您在视图控制器中有对您的搜索控制器的引用)。
将其添加到
prepareForDelete
的开头:guard !searchController.active else { return }
该代码检查搜索控制器是否未激活,但如果是,则不执行任何代码。
You can check if your search controller is active (I'm assuming you have a reference to your search controller in the view controller).
Add this to the beginning of
prepareForDelete
:guard !searchController.active else { return }
That code checks if the search controller is not active, but if it is, it doesn't execute any code.
相关问答
更多-
如何在UITableView中实现UISearchController - Swift(How to implement UISearchController in UITableView - Swift)[2022-05-20]
是的,搜索的方式已经根本改变了我认为更好的系统。 新系统对于大多数简单实施来说更好,更直接。 使用它非常简单。 首先,让你的课程符合UISearchResultsUpdating协议。 class MyTableViewController: UITableViewController, UISearchResultsUpdating {} 添加搜索控制器属性: class MyTableViewController: UTableViewController, UISearchResultsUpdatin ... -
问题在于你的tableView数据源方法: numberOfSectionsInTableview: , tableView:numberOfRowsInSection:和tableView:cellForRowAtIndexPath: 如果searchPredicate不是零,你需要每个方法返回不同的结果 - 非常像你的tableView:commitEditingStyle:方法。 我将使filteredObjects成为一个实例属性(在类的开始处定义),以便所有这些方法都可以访问它: var filt ...
-
您可以检查您的搜索控制器是否处于活动状态(我假设您在视图控制器中有对您的搜索控制器的引用)。 将其添加到prepareForDelete的开头: guard !searchController.active else { return } 该代码检查搜索控制器是否未激活,但如果是,则不执行任何代码。 You can check if your search controller is active (I'm assuming you have a reference to your search cont ...
-
事实证明,在应用程序启动时创建的storedata文件没有被覆盖,因此每次我更改并在storedata中进行属性时它都会因为属性不同而中断。 要解决此问题,我每次运行应用程序时都必须删除存储的数据。 该文件可在/ Users / {user} / Library / Application \ Support / {app name}中找到 通常,应用程序名称为com.yourcompany.appname It turns out that the storedata file that is creat ...
-
我找到了问题的解决方案 import UIKit import Foundation import CoreData class SongTableViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate, NSFetchedResultsControllerDelegate, UISearchControllerDelegate, UISearchBarDelegate, UISearchResul ...
-
您的updatesearch函数缺少其他部分来返回默认加载的数据。 做这个: func setupSearchBar() { searchController = self.addSearchBar() searchController.searchResultsUpdater = self searchController.delegate = self searchController.searchBar.delegate = self ...
-
所以在经过多次调查后,我发现对于这种情况更好的方法是简单地使用.filter()和.rangeOfString()来解决这个问题。 theArray.filter() { $0.json?.rangeOfString(searchQuery, options: .CaseInsensitiveSearch) != nil} 虽然没有使用NSPredicate,但这对我当前的设置来说似乎更容易实现。 So after much looking around, I found that a better ap ...
-
如果你不坚持NSPredicate (因为你没有使用NSFetchRequest ,看不出你应该这么做的原因,...),这里是代码: struct Exercises { let category: String let name : String let x_seed: [Double] let y_seed: [Double] let hasMult: Bool } let exercises = [ Exercises(category:"Sports", name:"Bow ...
-
在Swift中搜索核心数据(Search Core Data in Swift)[2022-08-13]
你还没有做任何事要求fetchedResultsController获取新数据。 你在getter中做的第一件事是: if _fetchedResultsController != nil { return _fetchedResultsController! } 当调用reloadData() ,框架调用tableView:cellForRowAtIndexPath:它会询问fetchedResultsController ,并且只返回你之前获得的相同的获取结果。 当您更改搜索栏文本时,您将不得 ... -
这是我如何做到的,没有集合(似乎没有使用集合运算符的快捷方式); 基本上它只是一个嵌套的提取请求: func dailyitems() -> [(date: String, items: [Item])]? { let request = NSFetchRequest(entityName: ItemEntity); request.returnsDistinctResults = true; request.resultType = NSFetchRequestResultType ...