欢迎访问Spring Cloud中国社区

《重新定义Spring Cloud实战》由Spring Cloud中国社区倾力打造,基于Spring Cloud的Finchley.RELEASE版本,本书内容宽度足够广、深度足够深,而且立足于生产实践,直接从生产实践出发,包含大量生产实践的配置。欢迎加微信Software_King进群答疑,国内谁在使用Spring Cloud?欢迎登记

求助,spring cloud 集成oauth2时/oauth/check_token无法访问的问题

dinsy · 4月前 · 1546 ·

spring cloud 集成oauth2时/oauth/check_token无法访问的问题。效果如下:

[spring cloud 版本 Finchley.RELEASE]

AuthorizationServerConfigurerAdapter:

  1. @Configuration
  2. @EnableAuthorizationServer
  3. @EnableGlobalMethodSecurity(prePostEnabled = true)
  4. public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
  5. private static final String DEMO_RESOURCE_ID = "order";
  6. @Bean
  7. public static NoOpPasswordEncoder passwordEncoder() {
  8. return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
  9. }
  10. @Override
  11. public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
  12. //配置两个客户端,一个用于password认证一个用于client认证
  13. clients.inMemory().withClient("client_1")
  14. .resourceIds(DEMO_RESOURCE_ID)
  15. .authorizedGrantTypes("client_credentials", "refresh_token")
  16. .scopes("select")
  17. .authorities("client")
  18. .secret("123456")
  19. .and().withClient("client_2")
  20. .resourceIds(DEMO_RESOURCE_ID)
  21. .authorizedGrantTypes("password", "refresh_token")
  22. .scopes("select")
  23. .authorities("client")
  24. .secret("123456");
  25. }
  26. @Override
  27. public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
  28. //enable client to get the authenticated when using the /oauth/token to get a access token
  29. //there is a 401 authentication is required if it doesn't allow form authentication for clients when access /oauth/token
  30. oauthServer.allowFormAuthenticationForClients();
  31. oauthServer.passwordEncoder(passwordEncoder());
  32. }
  33. }

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.example</groupId>
  6. <artifactId>oauthservice</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>oauthservice</name>
  10. <description>Demo project for Spring Boot</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.0.3.RELEASE</version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <java.version>1.8</java.version>
  21. </properties>
  22. <repositories>
  23. <repository>
  24. <id>spring-milestones</id>
  25. <name>Spring Milestones</name>
  26. <url>https://repo.spring.io/milestone</url>
  27. <snapshots>
  28. <enabled>false</enabled>
  29. </snapshots>
  30. </repository>
  31. </repositories>
  32. <build>
  33. <plugins>
  34. <plugin>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-maven-plugin</artifactId>
  37. </plugin>
  38. </plugins>
  39. </build>
  40. <dependencyManagement>
  41. <dependencies>
  42. <dependency>
  43. <groupId>org.springframework.cloud</groupId>
  44. <artifactId>spring-cloud-dependencies</artifactId>
  45. <version>Finchley.RELEASE</version>
  46. <type>pom</type>
  47. <scope>import</scope>
  48. </dependency>
  49. </dependencies>
  50. </dependencyManagement>
  51. <dependencies>
  52. <dependency>
  53. <groupId>org.springframework.boot</groupId>
  54. <artifactId>spring-boot-starter-web</artifactId>
  55. </dependency>
  56. <dependency>
  57. <groupId>org.springframework.cloud</groupId>
  58. <artifactId>spring-cloud-starter-oauth2</artifactId>
  59. </dependency>
  60. </dependencies>
  61. </project>