我应该怎么做才能获得我的 javafx spring boot 桌面应用程序的 OAuth2 访问令牌?

Posted

技术标签:

【中文标题】我应该怎么做才能获得我的 javafx spring boot 桌面应用程序的 OAuth2 访问令牌?【英文标题】:What should I do to get an OAuth2 access token to my javafx spring boot desktop application? 【发布时间】:2018-12-31 01:18:34 【问题描述】:

我正在尝试使用 Oauth2 授权来访问我的基于 javafx spring boot 的应用程序。

我在 github 上查看了很多教程以开始使用 Spring Cloud,我成功地设置了一个使用 zuul 作为网关的 eureka 服务器,并且工作正常

我有这个授权服务器实现:

@SpringBootApplication
@EnableAuthorizationServer
@EnableEurekaClient
@RestController
@SessionAttributes("authorizationRequest")
public class AuthorizationApplication 

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

    @Configuration
    static class MvcConfig extends WebMvcConfigurerAdapter 

        @Override
        public void addViewControllers(ViewControllerRegistry registry) 
            registry.addViewController("login").setViewName("login");
            registry.addViewController("/oauth/confirm_access").setViewName("authorize");
            registry.addViewController("/").setViewName("index");
        
    

    @Configuration
    static class LoginConfig extends WebSecurityConfigurerAdapter 
        @Override
        protected void configure(HttpSecurity http) throws Exception 
            http
                .formLogin().loginPage("/login").permitAll()
                    .successHandler(new  AuthenticationSuccessHandler() 
                @Override
                public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException 
                    response.getWriter().write("writting a success message here");

                
            ).failureHandler(new AuthenticationFailureHandler() 
                @Override
                public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException 
                    response.getWriter().write("failure  message Bad Credentials");

                
            )
                .and()
                    .requestMatchers()
                    .antMatchers("/", "/login", "/oauth/authorize", "/oauth/confirm_access")
                .and()
                    .authorizeRequests()
                    .anyRequest().authenticated()
                    .and().httpBasic().and().csrf().disable();



        
        @Autowired
        MDSUserDetailService mdsUserServiceDetail;
        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception 
           auth.userDetailsService(mdsUserServiceDetail);
        


    

    @Profile("!cloud")
    @Bean
    RequestDumperFilter requestDumperFilter() 
        return new RequestDumperFilter();
    

这是 userServiceDetail 实现

@Service 
class MDSUserDetailService implements UserDetailsService 

    @Autowired
    private UserRepository repository;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException 
        demo.User user = repository.findByUtilisateur(username);
        String password =user.getPassword();
        boolean enabled = user.isEtatUtilisateur();
        boolean accountNonExpired = user.getDateFin().after(repository.getCurrentTime());
        boolean accountNonLocked = user.isEtatUtilisateur();
        Collection<? extends GrantedAuthority> authorities = new ArrayList();


        return new User(username, password, enabled, accountNonExpired, true, accountNonLocked, authorities);

    


这就是我的授权服务 yml

spring:
  application:
    name: uaa

security:
  oauth2:
    client:
      client-id: mds_group
      client-secret: mds_group
      scope: read, write
      auto-approve-scopes: .*
    authorization:
      check-token-access: permitAll()
server:
  port: 18080
  context-path: /uaa
logging:
  level:
    org.springframework.security: DEBUG

现在我将介绍我的 javafx 客户端,这是一个非常简单的登录表单

@SpringBootApplication
@EnableOAuth2Sso
@EnableEurekaClient
@RibbonClients(
        
            @RibbonClient(name = "uaa"),
            @RibbonClient(name = "article")
        
)
public class ClientApplication extends Application
    ConfigurableApplicationContext applicationContext;

    public static void main(String[] args) 
        ClientApplication.launch(args);
    


    @Bean
    @LoadBalanced
    OAuth2RestTemplate oauth2RestTemplate(OAuth2ClientContext oauth2ClientContext, OAuth2ProtectedResourceDetails details) 
        return new OAuth2RestTemplate(details, oauth2ClientContext);
    

    @Profile("!cloud")
    @Bean
    RequestDumperFilter requestDumperFilter() 
        return new RequestDumperFilter();
    

    @Override
    public void start(Stage stage) throws Exception 
        //launching java fx app here
        applicationContext = SpringApplication.run(ClientApplication.class);
        Parent root = FXMLLoader.load(getClass().getResource("/fxml/Login.fxml"));
        Scene scene  = new Scene(root);
        stage.setScene(scene);
        stage.setTitle("javafx oauth2 tutorial");
        stage.show();

    

    @Override
    public void stop() throws Exception 

        applicationContext.close();
    



这是我的 Login.fxml 控制器

@Component
public class LoginController implements Initializable 

    @Autowired
    private RestTemplate restTemplate;
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) 
        // TODO
        

    @FXML
    private void doConnect(ActionEvent event) 

        if(validate())
            Map<String,String> values = new HashMap<>();
            values.put("username",username.getText());
            values.put("password",password.getText());
            restTemplate = new RestTemplate();
            restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
            //https://localhost:9999/oauth/token?grant_type=password?username=user&password=user
            ResponseEntity<String> request = restTemplate.postForEntity("http://localhost:9999/uaa/login?username="+username.getText()+"&password="+password.getText(), values, String.class);
            request = restTemplate.getForEntity("http://localhost:9999/uaa/oauth/token?grant_type=password?username="+username.getText()+"&password="+password.getText(), String.class , values);
            //ResponseEntity<String> postForEntity = restTemplate.postForEntity(loginUrl, values, String.class);
            System.out.println("post response "+request.getStatusCode().getReasonPhrase());
            System.out.println("post response "+request.toString());
            System.out.println("post response "+request.getHeaders().values());


        

    

    private boolean validate() 
        return true;
    


在运行第一个帖子后工作正常,我得到了一个 jsessionID,但由于我尝试使用 OAUTH2,第二个 get 工作不正常,我在我的授权服务器中得到了这个日志异常

2018-07-23 15:55:13.002 DEBUG 10328 --- [io-18080-exec-9] o.s.security.web.FilterChainProxy        : /oauth/token?password=HyiUucZK8elkbOiuSf5nx05CZwVNEYLiqW%2FzagK6iwg%3D&grant_type=password%3Fusername%3Dadministrateur at position 5 of 11 in additional filter chain; firing Filter: 'BasicAuthenticationFilter'
2018-07-23 15:55:13.002 DEBUG 10328 --- [io-18080-exec-9] o.s.security.web.FilterChainProxy        : /oauth/token?password=HyiUucZK8elkbOiuSf5nx05CZwVNEYLiqW%2FzagK6iwg%3D&grant_type=password%3Fusername%3Dadministrateur at position 6 of 11 in additional filter chain; firing Filter: 'RequestCacheAwareFilter'
2018-07-23 15:55:13.002 DEBUG 10328 --- [io-18080-exec-9] o.s.security.web.FilterChainProxy        : /oauth/token?password=HyiUucZK8elkbOiuSf5nx05CZwVNEYLiqW%2FzagK6iwg%3D&grant_type=password%3Fusername%3Dadministrateur at position 7 of 11 in additional filter chain; firing Filter: 'SecurityContextHolderAwareRequestFilter'
2018-07-23 15:55:13.002 DEBUG 10328 --- [io-18080-exec-9] o.s.security.web.FilterChainProxy        : /oauth/token?password=HyiUucZK8elkbOiuSf5nx05CZwVNEYLiqW%2FzagK6iwg%3D&grant_type=password%3Fusername%3Dadministrateur at position 8 of 11 in additional filter chain; firing Filter: 'AnonymousAuthenticationFilter'
2018-07-23 15:55:13.002 DEBUG 10328 --- [io-18080-exec-9] o.s.s.w.a.AnonymousAuthenticationFilter  : Populated SecurityContextHolder with anonymous token: 'org.springframework.security.authentication.AnonymousAuthenticationToken@905571d8: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@0: RemoteIpAddress: 192.168.44.1; SessionId: null; Granted Authorities: ROLE_ANONYMOUS'
2018-07-23 15:55:13.002 DEBUG 10328 --- [io-18080-exec-9] o.s.security.web.FilterChainProxy        : /oauth/token?password=HyiUucZK8elkbOiuSf5nx05CZwVNEYLiqW%2FzagK6iwg%3D&grant_type=password%3Fusername%3Dadministrateur at position 9 of 11 in additional filter chain; firing Filter: 'SessionManagementFilter'
2018-07-23 15:55:13.002 DEBUG 10328 --- [io-18080-exec-9] o.s.security.web.FilterChainProxy        : /oauth/token?password=HyiUucZK8elkbOiuSf5nx05CZwVNEYLiqW%2FzagK6iwg%3D&grant_type=password%3Fusername%3Dadministrateur at position 10 of 11 in additional filter chain; firing Filter: 'ExceptionTranslationFilter'
2018-07-23 15:55:13.003 DEBUG 10328 --- [io-18080-exec-9] o.s.security.web.FilterChainProxy        : /oauth/token?password=HyiUucZK8elkbOiuSf5nx05CZwVNEYLiqW%2FzagK6iwg%3D&grant_type=password%3Fusername%3Dadministrateur at position 11 of 11 in additional filter chain; firing Filter: 'FilterSecurityInterceptor'
2018-07-23 15:55:13.003 DEBUG 10328 --- [io-18080-exec-9] o.s.s.w.u.matcher.AntPathRequestMatcher  : Checking match of request : '/oauth/token'; against '/oauth/token'
2018-07-23 15:55:13.003 DEBUG 10328 --- [io-18080-exec-9] o.s.s.w.a.i.FilterSecurityInterceptor    : Secure object: FilterInvocation: URL: /oauth/token?password=HyiUucZK8elkbOiuSf5nx05CZwVNEYLiqW%2FzagK6iwg%3D&grant_type=password%3Fusername%3Dadministrateur; Attributes: [fullyAuthenticated]
2018-07-23 15:55:13.003 DEBUG 10328 --- [io-18080-exec-9] o.s.s.w.a.i.FilterSecurityInterceptor    : Previously Authenticated: org.springframework.security.authentication.AnonymousAuthenticationToken@905571d8: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@0: RemoteIpAddress: 192.168.44.1; SessionId: null; Granted Authorities: ROLE_ANONYMOUS
2018-07-23 15:55:13.004 DEBUG 10328 --- [io-18080-exec-9] o.s.s.access.vote.AffirmativeBased       : Voter: org.springframework.security.web.access.expression.WebExpressionVoter@2d8c2c29, returned: -1
2018-07-23 15:55:13.005 DEBUG 10328 --- [io-18080-exec-9] o.s.s.w.a.ExceptionTranslationFilter     : Access is denied (user is anonymous); redirecting to authentication entry point

org.springframework.security.access.AccessDeniedException: Access is denied
    at org.springframework.security.access.vote.AffirmativeBased.decide(AffirmativeBased.java:84) ~[spring-security-core-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.access.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:233) ~[spring-security-core-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:124) ~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91) ~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114) ~[spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilterInternal(BasicAuthenticationFilter.java:158) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) [spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262) [spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) [spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:105) [spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) [spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) [spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.springframework.boot.actuate.autoconfigure.MetricsFilter.doFilterInternal(MetricsFilter.java:106) [spring-boot-actuator-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_131]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_131]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.16.jar:8.5.16]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_131]

我应该怎么做才能获得 AccessToken 并避免这个异常?

【问题讨论】:

【参考方案1】:

有关 Spring 的 OAuth 客户端实现,请参阅此开发人员指南。 https://projects.spring.io/spring-security-oauth/docs/oauth2.html 您可能正在寻找的答案如下。

@Bean
public OAuth2RestOperations restTemplate() 
OAuth2RestTemplate template = new OAuth2RestTemplate(resource(), new 
DefaultOAuth2ClientContext(accessTokenRequest));
AccessTokenProviderChain provider = new AccessTokenProviderChain(Arrays.asList(new 
AuthorizationCodeAccessTokenProvider()));
provider.setClientTokenServices(clientTokenServices());
return template;
.

将上述 bean 用于 Spring 的 OAuth REST 客户端,它将请求一个令牌,然后最终传递该令牌进行身份验证。您不必担心获取令牌并将其作为标头传递,spring 会使用上面的 rest 模板为您完成。更多细节可以在文档中找到。

【讨论】:

我看到您使用的是我提到的同一个库。为清楚起见,您收到的是 401 还是 403 响应? 如果是 401,那么它一定是令牌有问题,或者令牌本身可能为空。您可以登录以查看令牌是否正在生成吗?

以上是关于我应该怎么做才能获得我的 javafx spring boot 桌面应用程序的 OAuth2 访问令牌?的主要内容,如果未能解决你的问题,请参考以下文章

我该怎么做才能让画布在不同的浏览器中工作[关闭]

javafx怎么通过表格组件做一个课程表的界面

如何在javafx treeview中返回已删除的treeitems?

JavaFX应用程序 - 需要签名的内容?

我应该怎么做才能使这个菜单栏在 Internet Explorer 7 及更高版本中正常工作?

如何在 JavaFX 中获得一个阶段的关闭事件?