【转载】spring-security-oauth2(十七 ) 实现标准的Oathh2服务提供商

基于传统的session机制,主要存在下面三个问题

  • 开发繁琐
  • 安全性和客户体验差
  • 有些前段技术不支持cookie,如小程序。

基于token的方式(令牌)

1.参数或请求头中添加token 

2.token可以进行定制(jwt)

SpringSecurityOAuth封装了服务提供商大部分的操作;而SpringSocial则是封装了客户端和服务提供商交互的流程 

协议中没有规定token怎么生成和存储,这个需要我们自己去实现。

本篇主要实现下面几个要点

  1. 实现标准的Oauth2协议中服务提供商(Provider)的各种功能
  2. 重构前面的三种登录方式,使其支持token的方式
  3. 高级特性,支持jwt和sso单点登录

实现标准Oauth服务提供商

标准认证服务商


 
 
  1. package com.rui.tiger.auth.app;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
  4. /**
  5. * 服务提供商-认证服务器
  6. * @author CaiRui
  7. * @date 2019-03-18 09:12
  8. */
  9. @Configuration
  10. @EnableAuthorizationServer
  11. public class TigerAuthorizationServerConfig {
  12. }

自定义clientId和clientSecret,这样不用系统每次给你自动分配

项目启动会增加几个认证控制器如下

ok下面我们来测试下是否可以,官方文档地址  

Oauth官网   https://tools.ietf.org/html/rfc6749#section-4  授权码模式文档

 由于spring oath2实现的是标准的oat2协议,所以参数什么的一般可以参考官网文档,如上链接

访问:http://localhost:8060/oauth/authorize?response_type=code&client_id=tigerauth&redirect_uri=http://www.example.com&scope=all 

出现以下报错信息

security5+ 认证默认为表单了也就是http.formLogin()  所以我们要进行改造


 
 
  1. package com.rui.tiger.auth.app;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  5. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  6. import org.springframework.security.core.userdetails.UserDetailsService;
  7. /**
  8. * @author CaiRui
  9. * @date 2019-04-09 12:12
  10. */
  11. @Configuration
  12. public class AppSecurityConfig extends WebSecurityConfigurerAdapter {
  13. @Autowired
  14. private UserDetailsService userDetailsService;
  15. @Override
  16. protected void configure(HttpSecurity http) throws Exception {
  17. //security5+ 认证默认为表单了也就是http.formLogin()
  18. http.httpBasic();
  19. }
  20. }

再次访问出现如下错误:

 断点调试报错如下:org.springframework.security.oauth2.provider.endpoint.DefaultRedirectResolver#resolveRedirect 

 我们要配置授权码回调域如下

ok再次请求终于调到了我们的同意授权界面

同意授权后返回授权码:http://www.example.com/?code=f1c9V7

 拿到这个授权码以后我们就可以请求token了 官方文档如下:

https://tools.ietf.org/html/rfc6749#section-4.1.3

 请求地址:org.springframework.security.oauth2.provider.endpoint.TokenEndpoint#postAccessToken ,用postman来发送请求

 头部放app账户和密码

发送请求返回响应如下:

{
    "access_token": "f61345b1-cfda-4599-ae9e-3215ad682bfc",
    "token_type": "bearer",
    "refresh_token": "f4b16714-c704-4f58-9659-815c2bd1c324",
    "expires_in": 43199,
    "scope": "all"
}

下面我们再来测试其它几种授权模式 

OAuth 2.0 的一个简单解释

OAuth 2.0 的四种方式

密码模式

http://localhost:8060/oauth/token  参数和授权码不同 这种模式适用于公司内相互信任的应用之间,可以直接提供用户名密码

资源服务器

添加标准资源服务器实现


 
 
  1. package com.rui.tiger.auth.app;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
  4. /**
  5. * @author CaiRui
  6. * @date 2019-04-17 08:38
  7. */
  8. @Configuration
  9. @EnableResourceServer
  10. public class TigerResourceServerConfig {
  11. }

访问用户信息 http://localhost:8060/user/me  报以下错误

{
    "error": "unauthorized",
    "error_description": "Full authentication is required to access this resource"
}

重启服务器,密码模式获取token 返回如下

{
    "access_token": "039ed20d-1f41-4914-bab2-c75ab53d0e73",
    "token_type": "bearer",
    "refresh_token": "3137ab8c-3668-43ff-90b7-d3e57be49f37",
    "expires_in": 43199,
    "scope": "all"
}

访问 http://localhost:8060/user/me  随带token

或者直接在请求参数中添加token

http://localhost:8060/user/me?access_token=039ed20d-1f41-4914-bab2-c75ab53d0e73

ok 到此基本的认证和资源服务器测试开发完成,下一章我们进行简单的源码分析

 

文章转载至:[https://blog.csdn.net/ahcr1026212/article/details/88632557](https://blog.csdn.net/ahcr1026212/article/details/88632557)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值