Spring cloud security combat service -4-7 micro refactor the code to adapt to the real world

Now, with the authentication server, configure the server resources. The protocol also OAuth, based authorization token authentication also ran through. The basic concept has also been simple to understand.
Before we dive down, there are a few points need to talk about

the use of scopes to control permissions, scopes can understand ACL Chapter III is before the time to write their own ACL to control the read and write permissions. Rights inside the OAuth protocol with scopes to implement ACL control two areas, first at the server side, you can go to different rights issue tokens for different applications.

Such as for oderApp can have read access, you can also have the authority to write.


Issued only read permission to go read for orderService. This is the server side, I can control tokens issued to configure permissions may contain,

in addition in the server-side resources, I can go to control some authority in accordance with the token in the scopes. So let's look at how to do this thing on the server side.
Or control such a scenario presentation read and write permissions

Read and write permissions demo


orderController write a getInfo. Just to read and write both requests.



This adapter there is also a method that can be used to control our access.

The default configuration is any request requires authentication

has scope write the time to post a request to call.

get the scope of the request to have read permissions

Start Testing Services

首先启动认证服务

现在只去拿reade的权限

现在拿到的token只有read的权限。

启动订单服务。用令牌去创建订单的方法。

错误,争对这个资源没有scope。需要的scope是write。但是你的令牌没有write的权限。

这样我们就基于OAuth2实现了ACL
我们发送一个get请求 可以成功。

在资源服务器中获取用户信息

之前将的是在这里拿到用户名

能不能在之类直接拿到用户信息呢?????创建一个UserDetailsService的实现类。

它里面只有一个方法就是根据用户名返回 UserDetails对象。

创建一个user类。

也要实现UserDetais的接口



把字符串转换成 GrantedAuthority集合返回。


getPassword和getUserName这里就删掉

在上面定义 username和password。然后生成get和set属性。

这两个方法都返回true

密码是不是没过期,也返回true

账户是不是可用的 也返回true

加一个自己特有的属性id并生成get和set方法。这个就是除了UserDetails要求以外,我们自己声明的信息。


把这个UserDetailsService接口的实现,声明成一个Spring的Bean。

有了这个Bean 我们需要修改下配置 ,让它用这个bean来读取相关的用户信息。

我们写一个getAccessTokenConvert这样的一个方法。


设置用userDetailsService转换成user对象。

最终返回

测试

启动认证服务

启动订单服务。


因为当前令牌是存在内存里面的。一旦认证服务器重启了。这些令牌就都消失了 。我们要重新申请令牌。

重新申请令牌



控制台获取到了用户的id

下面的这段逻辑就可以根据用户名去数据库内去查询

只要用户id

这里只要用户的id 也不需要用户的对象了。


这样写了以后,它就会把User对象里面的id单独拿出来 传递出去。可以用这个表达式把对象里面任何一个属性 当做参数传出来。



orderAPi服务 重启


查看控制台的输出

数据持久化

一块是客户端应用的数据,server里面的这段配置。这块信息要放到数据库里。

令牌的信息持久化。生产环境一般是集群,至少是两台机器。
在SpringSceurityOAuth的测试代码里面提供了一组构件表的scheme。这个scheme是争对内存的数据库来写的。主要用来测试的。


把代码复制出来,改一下 争对MySql 主要改了下数据类型。这个目录下的 就是可以直接创建表结构的

建好的表

目前可能就用到这两张表

加上数据库的依赖,一个是jdbc的启动器,它会替我去声明一个数据源,还有jdbcTemplate。
另外一个就是MySql的驱动。


数据库相关的配置也复制到server的微服务里面



首先是把这些放到数据库的表内

写一个main函数,让把加密后的密码打印出来 存到我们的数据库内

这个就是加密后的密码串。


填写多个的用逗号隔开



添加另外一条数据。这样就把客户端的信息都加如到数据库表内了。

读取数据库内的配置

注入数据源。然后用jdbc。把数据源给它。它会自动根据数据源 到数据库内读取auth_client_details表内的数据

把token的信息放进去。

tokenStore是一个接口,作用就是存取token的。把数据源dataSource给它

告诉服务器用这个tokenStore来存取token。

这样token聚会存到数据库内。

测试


访问getToken的请求

这句说明 信息都挪到数据库里面了 但是也可以正常的拿到这些信息。

生成的令牌也存到了access_token的表内

这样就有了一个持久化的令牌。
这个时候不管我是集群也好 ,还是认证服务器重启也好。令牌信息都不会丢失了。

结束

 

Guess you like

Origin www.cnblogs.com/wangjunwei/p/11939253.html