引起:org.springframework.security.oauth2.common.exceptions.UserDeniedAuthorizationException:访问被拒绝

时间:2018-04-10 08:19:58

标签: java spring-boot spring-cloud spring-security-oauth2

我正在开发 Spring Cloud 项目并在访问虽然客户端代码时遇到以下错误但不确定我收到的原因无法获取用户详细信息:class org.springframework.security.oauth2.client.resource.OAuth2AccessDeniedException, Unable to obtain a new access token for resource 'null'. The provider manager is not configured to support it.

你能指导一下这个问题吗?代码取自复数,而且没有任何自定义。

ron job started
Token: 77f012d4-7258-4314-9cfc-280762421ccb
2018-04-10 13:43:12.634  INFO 9056 --- [           main] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2018-04-10 13:43:12.655 ERROR 9056 --- [           main] o.s.boot.SpringApplication               : Application startup failed

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:803) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:784) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
    at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:771) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
    at pluralsight.demo.PluralsightSpringcloudM4SecurecliApplication.main(PluralsightSpringcloudM4SecurecliApplication.java:16) [classes/:na]
Caused by: org.springframework.security.oauth2.common.exceptions.UserDeniedAuthorizationException: Access is denied
    at org.springframework.security.oauth2.common.exceptions.OAuth2ExceptionJackson2Deserializer.deserialize(OAuth2ExceptionJackson2Deserializer.java:116) ~[spring-security-oauth2-2.0.11.RELEASE.jar:na]
    at org.springframework.security.oauth2.common.exceptions.OAuth2ExceptionJackson2Deserializer.deserialize(OAuth2ExceptionJackson2Deserializer.java:33) ~[spring-security-oauth2-2.0.11.RELEASE.jar:na]
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3789) ~[jackson-databind-2.8.3.jar:2.8.3]
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2913) ~[jackson-databind-2.8.3.jar:2.8.3]
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:225) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:213) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:95) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    at org.springframework.security.oauth2.client.http.OAuth2ErrorHandler.handleError(OAuth2ErrorHandler.java:130) ~[spring-security-oauth2-2.0.11.RELEASE.jar:na]
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:667) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:620) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    at org.springframework.security.oauth2.client.OAuth2RestTemplate.doExecute(OAuth2RestTemplate.java:128) ~[spring-security-oauth2-2.0.11.RELEASE.jar:na]
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:580) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:287) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    at pluralsight.demo.PluralsightSpringcloudM4SecurecliApplication.run(PluralsightSpringcloudM4SecurecliApplication.java:45) [classes/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:800) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
    ... 6 common frames omitted

PluralsightSpringcloudM4SecurecliApplication

@SpringBootApplication
public class PluralsightSpringcloudM4SecurecliApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(PluralsightSpringcloudM4SecurecliApplication.class, args);
    }

    @Override
    public void run(String... arg0) throws Exception {

        System.out.println("cron job started");

        ResourceOwnerPasswordResourceDetails resourceDetails = new ResourceOwnerPasswordResourceDetails();
        resourceDetails.setClientAuthenticationScheme(AuthenticationScheme.header);
        resourceDetails.setAccessTokenUri("http://localhost:9000/services/oauth/token");

        //must be a valid scope or get an error; if empty, get all scopes (default); better to ask for one
        resourceDetails.setScope(Arrays.asList("toll_read"));

        //must be valid client id or get an error
        resourceDetails.setClientId("pluralsight");
        resourceDetails.setClientSecret("pluralsightsecret");

        //diff user results in diff authorities/roles coming out; preauth on roles fails for adam, works for barry
        resourceDetails.setUsername("agoldberg");
        resourceDetails.setPassword("pass1");

        OAuth2RestTemplate template = new OAuth2RestTemplate(resourceDetails);
        //could also get scopes: template.getAccessToken().getScope()
        String token =  template.getAccessToken().toString();//.getValue();

        System.out.println("Token: " + token);

        String s = template.getForObject("http://localhost:9001/services/tolldata", String.class);

        System.out.println("Result: " + s);
    }
}

0 个答案:

没有答案
相关问题