spring cloud 对外服务的统一认证,以及各微服务之间相互调用的身份认证,需要有个认证服务器,上两节学习了以cas中央认证服务器作认证,作为spring cloud全家桶,本身提供了oauth2的统一认证,能很好地集成在整个微服务集群中,这节就学习oauth2服务器的搭建
1. 新建spring boot start project 我这设置项目名为:MicroserviceOauth2Server8301
2. pom.xml 加入以下依赖
<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.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
3. application.properties 配置
server.port: 8301
spring.application.name=MicroserviceOauth2Server8301
spring.cloud.discovery.enabled=true
spring.redis.host= centos7.linbsoft.com
spring.redis.port=6379
eureka.client.serviceUrl.defaultZone=http://admin:123@centos7.linbsoft.com:8101/eureka/,http://admin:123@microservice1.linbsoft.com:8102/eureka/
logging.level.org.springframework.security=DEBUG
4. 启动类 MicroserviceOauth2Server8301Application
@EnableDiscoveryClient
@SpringBootApplication
public class MicroserviceOauth2Server8301Application {
public static void main(String[] args) {
SpringApplication.run(MicroserviceOauth2Server8301Application.class, args);
}
}
5. 授权服务配置类AuthorizationServerConfig,这里的登录客户是写死在内存的,实际部署可以通过数据库读取。
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConf