Oauth2的学习总结
这段时间领导叫我学习oauth2准备一下,以后上spring cloud的时候可以上手就使用。陆陆续续学习了两周,三周还是更多,发现oauth2还是很复杂的。这篇文章就是想总结一下这段时间学习oauth2的东西,希望可以给刚学习oauth2的做一些铺垫,有些地方不对的还希望指正。
1.oauth2的简单介绍
oauth2总体我的理解是做认证授权的,最常见的就是第三方认证(QQ,微信,淘宝等),还有就是微服务的时候做认证使用。具体的概念,可以详见oauth2的解释,详细的oauth2的原理可以看大佬的博客
2.oauth2的四种模式
1.简单模式(implicit)
一般不使用,完全不可信
2.客户端模式(client credentials)
客户端是可信的,只要传入一些参数就能获取access token
3.密码模式(authorizaiton code)
需要提供用户名和密码去实现获取access token
4.code模式(password)
完整的模式,先获取code,然后通过code去获取access token
3.具体实现
3.1 本文实现的逻辑
3.2 代码逻辑的实现
3.2.1 认证中心实现
AuthorizationServerConfigurerAdapter这个是用来设定认证中心相关配置
@Configuration
@EnableAuthorizationServer
public class OAuth2Configure extends AuthorizationServerConfigurerAdapter {
@Autowired
@Qualifier("authenticationManagerBean")
private AuthenticationManager authenticationManager;
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Autowired
private MyUserDetailsService myUserDetailsService;
@Bean
public MyRedisTokenStore tokenStore(){
return new MyRedisTokenStore(redisConnectionFactory);
}
//client模式的设定
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception{
clients.inMemory()
.withClient("user-service")
.secre