Grails spring security oauth2提供者对具有正确承载令牌的资源的请求重定向到登录

时间:2015-12-11 23:55:58

标签: spring grails spring-security oauth-2.0 spring-security-oauth2

正如标题所暗示的,我有一个受oAuth2插件保护的控制器方法,但是当我向它发送请求时,包括正确的授权:Bearer< token> (使用Postman),我得到的响应是登录页面的HTML。

有问题的方法:

@Secured(["ROLE_USER", "#oauth2.clientHasAnyRole('ROLE_CLIENT', 'ROLE_TRUSTED_CLIENT')"])
    def getUserData(){
        response.setContentType("application/json")
        User u = springSecurityService.currentUser
        println u
        render u.mseUserInfo
    }

Config.groovy中:

// Added by the Spring Security Core plugin:
grails.plugin.springsecurity.auth.loginFormUrl = '/mse/login'

grails.plugin.springsecurity.userLookup.userDomainClassName = 'cz.improvisio.MSEauthProvider.user.User'
grails.plugin.springsecurity.userLookup.authorityJoinClassName = 'cz.improvisio.MSEauthProvider.user.UserRole'
grails.plugin.springsecurity.authority.className = 'cz.improvisio.MSEauthProvider.user.Role'
grails.plugin.springsecurity.controllerAnnotations.staticRules = [
    '/oauth/authorize.dispatch':[
        "ROLE_USER",
        "isFullyAuthenticated()"
    ],
    '/oauth/token.dispatch':[
        "ROLE_USER",
        "isFullyAuthenticated()"
    ],
    '/mse/login':["permitAll"],
    '/mse/':["permitAll"],
    '/**':["permitAll"]]



// Added by the Spring Security OAuth2 Provider plugin:
grails.plugin.springsecurity.oauthProvider.clientLookup.className = 'cz.improvisio.MSEauthProvider.user.Client'
grails.plugin.springsecurity.oauthProvider.authorizationCodeLookup.className = 'cz.improvisio.MSEauthProvider.user.AuthCode'
grails.plugin.springsecurity.oauthProvider.accessTokenLookup.className = 'cz.improvisio.MSEauthProvider.user.AccessToken'
grails.plugin.springsecurity.oauthProvider.refreshTokenLookup.className = 'cz.improvisio.MSEauthProvider.user.RefreshToken'

grails.plugin.springsecurity.filterChain.chainMap = [
    '/oauth/token': 'JOINED_FILTERS,-oauth2ProviderFilter,-securityContextPersistenceFilter,-logoutFilter,-authenticationProcessingFilter,-rememberMeAuthenticationFilter,-exceptionTranslationFilter',
    '/securedOAuth2Resources/**': 'JOINED_FILTERS,-securityContextPersistenceFilter,-logoutFilter,-authenticationProcessingFilter,-rememberMeAuthenticationFilter,-oauth2BasicAuthenticationFilter,-exceptionTranslationFilter',
    '/**': 'JOINED_FILTERS,-statelessSecurityContextPersistenceFilter,-oauth2ProviderFilter,-clientCredentialsTokenEndpointFilter,-oauth2BasicAuthenticationFilter,-oauth2ExceptionTranslationFilter'
]

这是Bootstrap.groovy创建的客户端:

new Client(
                clientId: 'testClient',
                authorizedGrantTypes: [
                    'authorization_code',
                    'refresh_token',
                    'implicit',
                    'password',
                    'client_credentials'
                ],
                authorities: ['ROLE_CLIENT'],
                scopes: ['read', 'write'],
                redirectUris: ['http://test.com']).save(flush: true)

还有一个稍微相关的问题:我无法找到一种方法来获取访问令牌应链接到其资源的用户,因此我假设Id能够通过springSecurityService获取它。这是正确的方法吗?或者我是否需要将userId传递给方法(OpenAM会这样做吗?)?

1 个答案:

答案 0 :(得分:2)

原来我没有为我的行动设置适当的过滤器链。将配置更改为

grails.plugin.springsecurity.filterChain.chainMap = [
    '/oauth/token': 'JOINED_FILTERS,-oauth2ProviderFilter,-securityContextPersistenceFilter,-logoutFilter,-authenticationProcessingFilter,-rememberMeAuthenticationFilter,-exceptionTranslationFilter',
    '/securedOAuth2Resources/**': 'JOINED_FILTERS,-securityContextPersistenceFilter,-logoutFilter,-authenticationProcessingFilter,-rememberMeAuthenticationFilter,-oauth2BasicAuthenticationFilter,-exceptionTranslationFilter',
'/myController/getUserData': 'JOINED_FILTERS,-securityContextPersistenceFilter,-logoutFilter,-authenticationProcessingFilter,-rememberMeAuthenticationFilter,-oauth2BasicAuthenticationFilter,-exceptionTranslationFilter',
    '/**': 'JOINED_FILTERS,-statelessSecurityContextPersistenceFilter,-oauth2ProviderFilter,-clientCredentialsTokenEndpointFilter,-oauth2BasicAuthenticationFilter,-oauth2ExceptionTranslationFilter'
]

修好了。