spring boot oauth2单点登录(三)token存储方式

相关文章

1、spring boot oauth2单点登录(一)-前后端分离例子
2、spring boot oauth2单点登录(二)-客户端信息存储
3、spring boot oauth2单点登录(三)-token存储方式
4、spring boot oauth2单点登录(四)-code存储方式

源码地址

后端:https://gitee.com/fengchangxin/sso
前端:https://gitee.com/fengchangxin/sso-page

准备

后端:三个spring boot应用,auth(授权管理),client1(客户端应用1),client2(客户端应用2)。
前端:三个Vue项目,auth,client1,client2。分别对应三个后端应用。
工具:nginx
域名:oauth.com,client1.com,client2.com,分别对应三个系统。
开发环境:先在host文件添加上面三个域名。
端口:
后端服务auth(8080),client1(8081),client2(8082)。
前端auth(8083),client1(8084),client2(8085)。
依赖:

<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>

测试地址:
client1:http://client1.com/client1Page/#/home
client2:http://client2.com/client2Page/#/home
登录用户:admin/123456

备注:此篇文章对应的授权管理应用:auth-token

token存储

token默认存储在内存里,单机应用没问题,但集群部署就不行了,集群所有的节点都应该用的是同一个token。框架已经提供了以下方式:InMemoryTokenStore(内存),JdbcTokenStore(数据库),JwtTokenStore(令牌),RedisTokenStore(redis)。

1、数据库存储:JdbcTokenStore

首先使用resources下的sql文件创建表,在项目引入jdbc依赖:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

在AuthorizationServerConfiguration下配置数据库存储:

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.tokenStore(new JdbcTokenStore(dataSource));
    }

2、redis存储:RedisTokenStore

使用redis存储需要引入依赖,并配置好redis:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

在AuthorizationServerConfiguration下配置redis存储:

    @Autowired
    private RedisConnectionFactory redisConnectionFactory;
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.tokenStore(new RedisTokenStore(redisConnectionFactory));
    }

3、令牌方式:JwtTokenStore

在AuthorizationServerConfiguration下配置jwt:

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.accessTokenConverter(jwtAccessTokenConverter()).tokenStore(jwtTokenStore());
    }

    @Bean
    public JwtTokenStore jwtTokenStore() {
        return new JwtTokenStore(jwtAccessTokenConverter());
    }

    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
        JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
        jwtAccessTokenConverter.setSigningKey("123456");
        return jwtAccessTokenConverter;
    }

需要注意的是在客户端的配置需加上jwt的配置,令牌方式对应的客户端是:client1-jwtclient2-jwt

security:
  oauth2:
    client:
      client-id: client1
      client-secret: client1_secret
      access-token-uri: http://oauth.com/auth/oauth/token
      user-authorization-uri: http://oauth.com/auth/oauth/authorize
    resource:
      user-info-uri: http://oauth.com/auth/user
      token-info-uri: http://oauth.com/auth/oauth/check_token
      jwt:
        key-uri: http://oauth.com/auth/oauth/token_key

4、自定义:实现TokenStore接口

TokenStore接口要实现的方法过多,不推荐此种方式。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。