前言:
本文意在抛砖引玉,帮大家将基本的环境搭起来,具体实战方案还要根据自己的业务需求进行制定。我们最终没有使用Spring Security OAuth2来搭建授权服务,而是完全根据OAuth2标准自己实现的服务。
Spring Cloud Security OAuth2 是 Spring 对 OAuth2 的开源实现,优点是能与Spring Cloud技术栈无缝集成,如果全部使用默认配置,开发者只需要添加注解就能完成 OAuth2 授权服务的搭建。
1. 添加依赖
授权服务是基于Spring Security的,因此需要在项目中引入两个依赖:
<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>
前者为 Security,后者为Security的OAuth2扩展。
2. 添加注解和配置
在启动类中添加@EnableAuthorizationServer
注解:
@SpringBootApplication
@EnableAuthorizationServer
public class AlanOAuthApplication {
public static void main(String[] args) {
SpringApplication.run(AlanOAuthApplication.class, args);
}
}
完成这些我们的授权服务最基本的骨架就已经搭建完成