Skip to content

#oauth2.hasScope and Resource Server #5096

@silent-box

Description

@silent-box

Hello,

I have multiple Resource servers that share an Authorization server, everything works pretty good.

Now I'm trying to protect controllers in Resource servers with @PreAuthorize("#oauth2.hasScope('scope')") annotation, but it just rejects every call, even with the right scope.

Here is an example Resource server code:

@SpringBootApplication
@EnableResourceServer
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MyResourceApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyResourceApplication.class, args);
    }
}
@RestController
public class MyController {

    @PreAuthorize("#oauth2.hasScope('ui')")
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String hello(Principal principal) {
        return "hello, " + principal.getName();
    }
}

Auth-server have the user-info-uri controller, which returns Principal object.

I'm making request from Browser('ui' scope) to Account-service.
Account-service invokes Auth-server and receives the following response:

screen shot 2016-02-04 at 00 38 54

I can see this in my logs:

Previously Authenticated: org.springframework.security.oauth2.provider.OAuth2Authentication@48cce0de: Principal: account-service; Credentials: [PROTECTED]; Authenticated: true; Details: remoteAddress=127.0.0.1, tokenType=BearertokenValue=<TOKEN>; Not granted any authorities
Voter: org.springframework.security.access.prepost.PreInvocationAuthorizationAdviceVoter@4cd3541a, returned: -1
Voter: org.springframework.security.access.vote.RoleVoter@7e09868e, returned: 0
Voter: org.springframework.security.access.vote.AuthenticatedVoter@23c38124, returned: 0

So, UsernamePasswordAuthenticationToken contains target scope somewhere in details, but OAuth2Authentication object has no scopes and OAuth2SecurityExpressionMethods.hasScope doesn't work.

My question is, why 'ui' scope does not appear in OAuth2Authentication object after Resource-Auth servers talking? What am i doing wrong?

Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions