OAuth2.0(三)之spring-boot集成OAuth2.0[client]

前面介绍了oauth2的server及resource环境搭建,这篇继续介绍client的环境搭建

客户端项目搭建:

1.构建一个简单的maven项目

2.在项目中增加spring-boot和security及oauth的依赖支持

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>cn.majingjing.tm.oauth2</groupId>
<artifactId>tm-oauth-resource</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.7.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>


<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

</plugins>
</build>

</project>

3.配置项目参数和oauth的相关参数

server.context-path=/client
server.port=8082
server.session.cookie.name=OAUTH2SESSION

security.basic.enabled=false

security.oauth2.client.client-id=client-majj
security.oauth2.client.client-secret=client-secret-majj
security.oauth2.client.access-token-uri=http://localhost:8080/oauth/token
security.oauth2.client.user-authorization-uri=http://localhost:8080/oauth/authorize
security.oauth2.client.client-authentication-scheme=query

logging.level.root=debug

4.在启动类上增加 @EnableOAuth2Client 注解

@SpringBootApplication
@EnableOAuth2Client
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}

}

5.编写client的配置类,将资源的信息注入到OAuth2RestTemplate中

@Configuration
public class TmOAuth2ClientConfiguration {

@Autowired
OAuth2ProtectedResourceDetails auth2ProtectedResourceDetails;

@Bean
public OAuth2RestOperations restTemplate(OAuth2ClientContext oauth2ClientContext) {
OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(auth2ProtectedResourceDetails, oauth2ClientContext);
return restTemplate;
}

}

6.在host文件中增加如下配置

 
  1. 127.0.0.1 www.majj.cn

7.启动项目,浏览器访问
http://www.majj.cn:8082/client/user

c-1.png

c-2.png

扫描二维码关注公众号,回复: 5681542 查看本文章

c-3.png

可以仔细观察下浏览器url地址的变化情况

到此oauth2和spring-boot的整合已经完成了。

注意:请严格按照文章的描述进行编写,请在保证正确运行看到效果后再进行改造。

这里说明下,官方文档很多小细节并没有提及到,导致在做案例的时候碰到了很多奇葩的问题。这个案例看似代码很简单,但是我在调试的时候花了2天的时间来测试,基本上把源代码都debug了个遍。一直调用/oauth/authorize,不调用/oauth/token的步骤,完全按照官方文档进行的集成就是不对,后来查看了github发现了解决方案,例如 https://github.com/spring-projects/spring-security-oauth/issues/822 这个就非常关键。

这几篇文章的介绍只是简单描述如何完成整合,并没有对每个配置参数及特殊的config类做详细介绍。
后续如果有需要我会对源代码进行解析介绍下。

本案例参考文章:

https://projects.spring.io/spring-security-oauth/docs/oauth2.html

https://github.com/spring-projects/spring-security-oauth/tree/master/tests

猜你喜欢

转载自blog.csdn.net/qq_33454884/article/details/88222424