我有一個與在Grails上開發的REST API進行通信的ios應用程序。爲了保護REST API,我決定使用oAuth 2.0'資源所有者密碼'流程。對於Grails的應用爲OAuth 2.0提供者的角色,我使用以下http://grails.org/plugin/spring-security-oauth2-provider 爲ID爲「客戶」的客戶,祕密爲「1234」和用戶與用戶名爲「用戶和密碼,「密碼」,該請求令牌如下Grails oAuth2 Provider插件沒有針對grant_type'密碼'進行身份驗證
POST /oauth2-test/oauth/token HTTP/1.1
Host: 192.168.1.113:8080
Authorization: Basic Y2xpZW50OjEyMzQ=
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded
grant_type=password&scope=read&username=user&password=password
接收的響應是
{
"error": "unauthorized",
"error_description": "Full authentication is required to access this resource"
}
我Config.groovy中的編輯爲春季安全和OAuth 2.0提供的插件看起來像這樣
// Added by the Spring Security Core plugin:
grails.plugin.springsecurity.userLookup.userDomainClassName = 'test.User'
grails.plugin.springsecurity.userLookup.authorityJoinClassName = 'test.UserRole'
grails.plugin.springsecurity.authority.className = 'test.Role'
grails.plugin.springsecurity.controllerAnnotations.staticRules = [
'/': ['permitAll'],
'/index': ['permitAll'],
'/index.gsp': ['permitAll'],
'/assets/**': ['permitAll'],
'/**/js/**': ['permitAll'],
'/**/css/**': ['permitAll'],
'/**/images/**': ['permitAll'],
'/**/favicon.ico': ['permitAll'],
'/oauth/authorize.dispatch': ["isFullyAuthenticated() and (request.getMethod().equals('GET') or request.getMethod().equals('POST'))"],
'/oauth/token.dispatch' : ["isFullyAuthenticated() and request.getMethod().equals('POST')"]
]
// Added by the Spring Security OAuth2 Provider plugin:
grails.plugin.springsecurity.oauthProvider.clientLookup.className = 'test.Client'
grails.plugin.springsecurity.oauthProvider.authorizationCodeLookup.className = 'test.AuthorizationCode'
grails.plugin.springsecurity.oauthProvider.accessTokenLookup.className = 'test.AccessToken'
grails.plugin.springsecurity.oauthProvider.refreshTokenLookup.className = 'test.RefreshToken'
grails.plugin.springsecurity.providerNames = [
'clientCredentialsAuthenticationProvider',
'daoAuthenticationProvider',
'anonymousAuthenticationProvider',
'rememberMeAuthenticationProvider'
]
grails.exceptionresolver.params.exclude = ['password', 'client_secret']
grails.plugin.springsecurity.filterChain.chainMap = [
'/oauth/token': 'JOINED_FILTERS,-oauth2ProviderFilter,-securityContextPersistenceFilter,-logoutFilter,-rememberMeAuthenticationFilter',
'/api/**': 'JOINED_FILTERS,-securityContextPersistenceFilter,-logoutFilter,-rememberMeAuthenticationFilter',
'/**': 'JOINED_FILTERS,-statelessSecurityContextPersistenceFilter,-oauth2ProviderFilter,-clientCredentialsTokenEndpointFilter'
]
- 我在做什麼錯?據我所知oAuth 2.0主要用於 授權而不是身份驗證。所以我必須明確地添加 過濾器進行身份驗證嗎?我從Grails開始,在Springs上沒有任何 的經驗,並且有關如何執行此操作的任何幫助,
- grant_type'password'是否需要客戶端身份驗證?對於補助
型「密碼」應用戶認證是不夠的?即使 需要它來驗證客戶端會根據我的理解使用基本身份驗證 。那麼我是否需要明確添加一個基本的 認證過濾器?
你傳遞的client_id和client_secret在Authorization頭?您是否嘗試在請求中傳遞它們? – 2015-02-10 21:05:35
是的,我在授權標頭中傳遞client_id和client_secret。是的,試着將它們作爲x-www-form-urlencoded傳遞給他們,並得到了相同的結果..沒有變化。 – user559668 2015-02-11 02:51:03
您是如何解決這個問題的? – JohnTheBeloved 2015-03-16 13:12:40