最近在学习微服务相关的知识及各类框架的使用,在搭建的过程当中,遇到了很多问题,可是都没有记录下来,致使过一段时间后,就没有什么印象了.. 因此决定在掘金写文章。 一是为了记录本身在写代码过程当中的知识点以及解决的问题,方便查阅; 二是为了能与其它朋友一块儿讨论,能够吸取不一样思想及不一样方案,扩展思路。html
标题之因此为
Spring Cloud Security
,是由于想要写Spring Cloud
相关的一系列技术。而Spring Cloud
依赖Springboot
,security
仍是Spring Security
模块的东西,本质上没有太大的区别。git
在写的过程当中,遇到本身不熟悉或没有把握的概念及知识,会谨慎的求证。也会尽可能保证发布的任何代码都是可运行的。 可是因为本身技术能力有限,不免会有错误,包括但不限于
拼写错误
、代码错误
、有代码洁癖的人看着缩进不舒服
、概念理解有误差
、代码不够优雅
等等,但愿各位能够不吝指教,不喜勿喷~github
microservice
,做为全部服务的parent
, 同时引入依赖<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<spring.cloud.dependencies.version>Greenwich.RELEASE</spring.cloud.dependencies.version>
<spring-security-oauth2-autoconfigure.version>2.1.5.RELEASE</spring-security-oauth2-autoconfigure.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.dependencies.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth.boot</groupId>
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
<version>${spring-security-oauth2-autoconfigure.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
复制代码
uaa-service
,做为认证受权服务,引入依赖<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.security.oauth.boot</groupId>
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth.boot</groupId>
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
</dependencies>
复制代码
spring-cloud-starter-oauth2
这个依赖应该已经包含了spring security
相关的jar
,可是spring-cloud-dependencies
版本为Greenwich.RELEASE
时,spring-security-oauth2-autoconfigure
子module中引入的版本一直是2.1.0.M4
,不论是从新import仍是删除本地maven repository
都无论用, 在官方的issue
中也有人遇到的相同的问题, 因此以上依赖暂时使用单独引入spring-security-oauth2-autoconfigure
,version
还必须在子module中指定,不知大家是否也碰到这种问题.spring
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private PasswordEncoder passwordEncoder;
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
super.configure(security);
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("client-id")
.secret(passwordEncoder.encode("client-secret"))
.scopes("read", "write")
.authorizedGrantTypes("password", "refresh_token")
.authorities("user:view");
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints
.authenticationManager(authenticationManager)
.tokenStore(tokenStore());
}
@Bean
public TokenStore tokenStore() {
return new InMemoryTokenStore();
}
}
复制代码
@EnableAuthorizationServer
注解告诉Spring
激活authorization server@Configuration
注解的实现了AuthorizationServerConfigurer
接口的类代表这是一个authorization server配置类。AuthorizationServerConfigurerAdapter
是Spring
提供的默认实现AuthorizationServerConfigurer
接口的类,里面都是空方法authenticationManager
,这是Spring
默认提供的,若是须要使用password
模式,必须显示地配置endpoints.authenticationManager(authenticationManager)
passwordEncoder
,用于用户登陆时密码校验以及建立用户时密码的加密tokenStore
这里暂时使用InMemoryTokenStore
,Spring
同时也提供了以下几种存储token
方式
ClientDetailsServiceConfigurer
是配置authorization server颁发的client的凭证
client.inMemory()
是在内存中存储client信息withClient
和secret
是client的username
和password
scopes
是受权范围,例如该client能够进行读和写authorizedGrantTypes
是配置受权方式, 能够是OAuth2.0
中支持的方式,也能够是自定义的@Configuration
@EnableGlobalMethodSecurity(proxyTargetClass = true, prePostEnabled = true)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("admin")
.password(passwordEncoder().encode("password"))
.roles("ADMIN");
}
@Bean
public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
}
复制代码
@EnableGlobalMethodSecurity(proxyTargetClass = true, prePostEnabled = true)
开启方法级别权限验证AuthenticationManagerBuilder
在内存中配置一个用户,用于password
模式获取token
BCryptPasswordEncoder
配置加密实现类以上项目启动完成以后,启动项目,spring security
会为咱们提供几个endpoint
, 其中一个就是获取token的: /oauth/token
。使用的http请求工具是postman
bash
如上图所示,输入以后,会把以前配置的client
的username
和password
的base64
编码放在http header
中服务器
在http body
中输入框架
发送请求以后,获得响应, 其中的access_token
就是咱们后续请求接口使用的令牌maven
以上就是利用spring security
简单地搭建authorization server, 能够看到仍是蛮方便的。可是若是是用在生产环境,还远不够,例如用户信息、client信息、token信息的持久化,各类异常的处理,资源服务器的配置等等,在后续的文章也会不断的完善,感谢!ide