学习笔记:微服务13 spring boot2.x oauth2-client

倾然丶 夕夏残阳落幕 提交于 2019-11-26 09:15:31

前面oauth2 server设置好了,登录oauth2 server正常,但另外建一个oauth2 client 总是不能登录认证,网上说是spring boot 2 中去除了@EnableOAuth2Sso注解,今天终于找到适合我的spring boot 2.11的oauth2 client,实现了客户端认证。

1.新建spring start project 项目,我命名为microservice-oauth2-client-8805

2.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. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.1.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.linbsoft</groupId>
  12. <artifactId>microservice-oauth2-client-8805</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>microservice-oauth2-client-8805</name>
  15. <description>Demo project for Spring Boot microservice-oauth2-client-8805</description>
  16. <properties>
  17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  19. <java.version>1.8</java.version>
  20. <spring-cloud.version>Greenwich.M3</spring-cloud.version><!-- Finchley.M8 -->
  21. </properties>
  22. <dependencies>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-web</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.cloud</groupId>
  29. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.springframework.cloud</groupId>
  33. <artifactId>spring-cloud-starter-config</artifactId>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-starter-security</artifactId>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.springframework.security</groupId>
  41. <artifactId>spring-security-oauth2-client</artifactId>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.springframework.security</groupId>
  45. <artifactId>spring-security-oauth2-jose</artifactId>
  46. </dependency>
  47. <dependency>
  48. <groupId>org.springframework.security.oauth</groupId>
  49. <artifactId>spring-security-oauth2</artifactId>
  50. <version>2.3.3.RELEASE</version>
  51. </dependency>
  52. <dependency>
  53. <groupId>org.springframework.boot</groupId>
  54. <artifactId>spring-boot-starter-test</artifactId>
  55. <scope>test</scope>
  56. </dependency>
  57. </dependencies>
  58. <dependencyManagement>
  59. <dependencies>
  60. <dependency>
  61. <groupId>org.springframework.cloud</groupId>
  62. <artifactId>spring-cloud-dependencies</artifactId>
  63. <version>${spring-cloud.version}</version>
  64. <type>pom</type>
  65. <scope>import</scope>
  66. </dependency>
  67. </dependencies>
  68. </dependencyManagement>
  69. <repositories>
  70. <repository>
  71. <id>spring-milestones</id>
  72. <name>Spring Milestones</name>
  73. <url>https://repo.spring.io/milestone</url>
  74. <snapshots>
  75. <enabled>false</enabled>
  76. </snapshots>
  77. </repository>
  78. </repositories>
  79. <build>
  80. <plugins>
  81. <plugin>
  82. <groupId>org.springframework.boot</groupId>
  83. <artifactId>spring-boot-maven-plugin</artifactId>
  84. </plugin>
  85. </plugins>
  86. </build>
  87. </project>

3. application.peoperties

  1. server.port: 8805
  2. spring.application.name=MicroserviceOauth2Client8805
  3. spring.cloud.discovery.enabled=true
  4. eureka.client.serviceUrl.defaultZone=http://admin:123@centos7.linbsoft.com:8101/eureka/,http://admin:123@microservice1.linbsoft.com:8102/eureka/
  5. # logging.level.root= debug
  6. spring.security.oauth2.client.registration.my-client-1.client-id=admin
  7. spring.security.oauth2.client.registration.my-client-1.client-secret=123456
  8. spring.security.oauth2.client.registration.my-client-1.client-name=admin
  9. spring.security.oauth2.client.registration.my-client-1.scope=read
  10. spring.security.oauth2.client.registration.my-client-1.redirect-uri=http://microservice1.linbsoft.com:8805/login/oauth2/code/callback
  11. spring.security.oauth2.client.registration.my-client-1.client-authentication-method=basic
  12. spring.security.oauth2.client.registration.my-client-1.authorization-grant-type=authorization_code
  13. spring.security.oauth2.client.registration.my-client-1.provider=my-oauth-provider
  14. spring.security.oauth2.client.provider.my-oauth-provider.authorization-uri=http://centos7.linbsoft.com:8301/oauth/authorize
  15. spring.security.oauth2.client.provider.my-oauth-provider.token-uri=http://centos7.linbsoft.com:8301/oauth/token
  16. spring.security.oauth2.client.provider.my-oauth-provider.user-info-uri=http://centos7.linbsoft.com:8301/user
  17. spring.security.oauth2.client.provider.my-oauth-provider.user-name-attribute=name

这里要注意的是redirect-uri 和 oauth2 server不要相同域名,这个后面会说到为什么

4. 创建测试资源 ExampleController类

@RestController
public class ExampleController {

    @RequestMapping("/")
    public String email(Principal principal) {
        return "Hello " + principal.getName();
    }
    @RequestMapping("/hello")
    public String emailaa(Principal principal) {
        return "Hello1 " + principal.getName();
    }
}

5.修改 oauth2 server 增加对这个网站的回地址注册

在这个public void configure(ClientDetailsServiceConfigurer clients) 里

       clients.inMemory()
                .withClient("admin")
                .scopes("read")
                .secret(new BCryptPasswordEncoder().encode("123456"))
                .redirectUris("http://centos7.linbsoft.com:8301","http://microservice1.linbsoft.com:8805/login/oauth2/code/callback")
                .authorizedGrantTypes("password", "authorization_code", "refresh_token")

加入了http://microservice1.linbsoft.com:8805/login/oauth2/code/callback

6. 测试

结果出错了,提示[authorization_request_not_found]

后来看到网友文章说是定位到session=null,有个说法是因为oauth2客户端和服务端以都在同一个服务器地址,都在同一个浏览器,网友的原话是【这些错误意味着没有找到授权请求。 authorization request存储在会话中,所以一些会话没有被存储。 默认情况下会话由cookie管理。所以我认为这可能是因为你正在本地主机上运行所有东西,所以第一个cookie由localhost:8080设置以存储授权请求会话数据,并且当你登录到localhost:8081时,它将为其会话设置另一个cookie。】

怎么解决呢,想到的是把oauth2客户端和服务端的域名区分开来,因此,虽然在同一个ip,但设置两个域名:

192.168.49.141    centos7.linbsoft.com
192.168.49.141    microservice1.linbsoft.com
然后,oauth2 server 按部署在centos7.linbsoft.com:8301使用,而oauth2 client 按部署在microservice1.linbsoft.com:8805上使用,果然正常了。

oauth2客户端顺利登录oauth2服务器认证后访问客户端保护资源

 

 

前面oauth2 server设置好了,登录oauth2 server正常,但另外建一个oauth2 client 总是不能登录认证,网上说是spring boot 2 中去除了@EnableOAuth2Sso注解,今天终于找到适合我的spring boot 2.11的oauth2 client,实现了客户端认证。

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!