资源服务器OAuth2-春季启动始终返回访问被拒绝,但令牌需要例外

时间:2019-07-16 16:44:24

标签: spring-boot oauth-2.0

使用Spring boot OAuth实现设置客户端凭据授予时,我能够从Auth服务器获取令牌,但是当我尝试访问资源服务器时,总是看到Oauth访问被拒绝。 下面是代码,任何指针?

资源服务器:

@

SpringBootApplication
@EnableResourceServer
public class EtaAuthServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EtaAuthServerApplication.class, args);
        System.out.println("------ETA RESOURCE SERVER-------");

    }
    @EnableGlobalMethodSecurity(prePostEnabled = true)
    protected static class GlobalSecurityConfiguration extends GlobalMethodSecurityConfiguration {

        @Override
        protected MethodSecurityExpressionHandler createExpressionHandler() {
            return new OAuth2MethodSecurityExpressionHandler();
        }
    }
    @RestController
    public class DetailsController extends ResourceServerConfigurerAdapter {


        @GetMapping("/mod")
        public String getDetails() {

            System.out.println("---Success---");

            return "Success";
        }

客户:

@SpringBootApplication
@EnableAutoConfiguration
@EnableOAuth2Client
public class EtaClientApplication  implements CommandLineRunner {

    @Value("#{ @environment['example.baseUrl'] }")
    private String serverBaseUrl;
    private final Logger log = LoggerFactory.getLogger(EtaClientApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(EtaClientApplication.class, args);
        System.out.println("------Client Application---");
    }
    @Bean
    @ConfigurationProperties("example.oauth2.client")
    protected ClientCredentialsResourceDetails oAuthDetails() {
        return new ClientCredentialsResourceDetails();
    }
    @Bean
    protected RestTemplate restTemplate() {

        OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(oAuthDetails());
        System.out.println("--Access Token "+oAuth2RestTemplate.getAccessToken());
        return oAuth2RestTemplate;
    }
@Override
    public void run(String... args) {
//
try{
    OAuth2RestTemplate oAuth2RestTemplate = (OAuth2RestTemplate) restTemplate();
   System.out.println(oAuth2RestTemplate.getAccessToken());
   log.info("SUCCESS PRINT: {}", oAuth2RestTemplate.getForObject(serverBaseUrl + "/mod", String.class));
}catch(Exception e){
    e.printStackTrace();}}}

错误消息: ------客户申请书- org.springframework.security.oauth2.client.http.AccessTokenRequiredException:OAuth2访问被拒绝。     在org.springframework.security.oauth2.client.http.OAuth2ErrorHandler.handleError(OAuth2ErrorHandler.java:162)     在org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)     在org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:778)     在org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:736)     在org.springframework.security.oauth2.client.OAuth2RestTemplate.doExecute(OAuth2RestTemplate.java:143)     在org.springframework.web.client.RestTemplate.execute(RestTemplate.java:670)     在org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:311)     在com.toyota.tme.vlps.eta.client.EtaClient.EtaClientApplication.run(EtaClientApplication.java:75)     在org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:779)     在org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:763)     在org.springframework.boot.SpringApplication.run(SpringApplication.java:318)     在org.springframework.boot.SpringApplication.run(SpringApplication.java:1213)     在org.springframework.boot.SpringApplication.run(SpringApplication.java:1202)     在com.toyota.tme.vlps.eta.client.EtaClient.EtaClientApplication.main(EtaClientApplication.java:31)     在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处     在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)     在org.springframework.boot.maven.AbstractRunMojo $ LaunchRunner.run(AbstractRunMojo.java:542)

0 个答案:

没有答案