A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

3.3.4.3 资源服务授权测试
这里我们测试课程图片查询
get http://localhost:31200/course/coursepic/list/4028e58161bd3b380161bd3bcd2f0000
请求时没有携带令牌则报错:
[AppleScript] 纯文本查看 复制代码
{  
   "error": "unauthorized",  
   "error_description": "Full authentication is required to access this resource" }

请求时携带令牌:
在http header中添加 Authorization: Bearer 令牌


3.3.4.4 解决swagger-ui无法访问
当课程管理加了授权之后再访问swagger-ui则报错:


修改授权配置类ResourceServerConfig的configure方法:
针对swagger-ui的请求路径进行放行:

[AppleScript] 纯文本查看 复制代码
//Http安全配置,对每个到达系统的http请求链接进行校验 @Override public void configure(HttpSecurity http) throws Exception {     //所有请求必须认证通过   
  http.authorizeRequests()        
     //下边的路径放行   
  .antMatchers("/v2/api‐docs", "/swagger‐resources/configuration/ui",      
       "/swagger‐resources","/swagger‐resources/configuration/security",        
     "/swagger‐ui.html","/webjars/**").permitAll()   
  .anyRequest().authenticated(); }

注意:
通过上边的配置虽然可以访问swagger-ui,但是无法进行单元测试,除非去掉认证的配置或在上边配置中添加所有 请求均放行("/**")。

3.4 Oauth2密码模式授权
密码模式(Resource Owner Password Credentials)与授权码模式的区别是申请令牌不再使用授权码,而是直接 通过用户名和密码即可申请令牌。
测试如下:
Post请求:http://localhost:40400/auth/oauth/token
参数:
grant_type:密码模式授权填写password
username:账号 password:密码 并且此链接需要使用 http Basic认证。


注意:当令牌没有过期时同一个用户再次申请令牌则不再颁发新令牌。

3.5校验令牌
Spring Security Oauth2提供校验令牌的端点,如下:
Get: http://localhost:40400/auth/oauth/check_token?token=
参数:
token:令牌
使用postman测试如下:


结果如下:

[AppleScript] 纯文本查看 复制代码
{  
   "companyId": null,   
  "userpic": null,   
  "user_name": "mrt",   
  "scope": [    
     "app"  
   ],   
  "name": null,  
   "utype": null,    
 "id": null,  
   "exp": 1531254828,  
   "jti": "6a00f227‐4c30‐47dc‐a959‐c0c147806462",   
  "client_id": "XcWebApp" }

exp:过期时间,long类型,距离1970年的秒数(new Date().getTime()可得到当前时间距离1970年的毫秒数)。 user_name: 用户名
client_id:客户端Id,在oauth_client_details中配置 scope:客户端范围,在oauth_client_details表中配置 jti:与令牌对应的唯一标识
companyId、userpic、name、utype、id:这些字段是本认证服务在Spring Security基础上扩展的用户身份信息


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马