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

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

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

基于token的方式(令牌)

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

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

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

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

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

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

实现标准Oauth服务提供商

标准认证服务商

package com.rui.tiger.auth.app;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;

/**
 * 服务提供商-认证服务器
 * @author CaiRui
 * @date 2019-03-18 09:12
 */
@Configuration
@EnableAuthorizationServer
public class TigerAuthorizationServerConfig {

}

自定义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()  所以我们要进行改造

package com.rui.tiger.auth.app;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;

/**
 * @author CaiRui
 * @date 2019-04-09 12:12
 */
@Configuration
public class AppSecurityConfig extends WebSecurityConfigurerAdapter {


	@Autowired
	private UserDetailsService userDetailsService;
	@Override
	protected void configure(HttpSecurity http) throws Exception {
		//security5+ 认证默认为表单了也就是http.formLogin()
		http.httpBasic();
	}
}

再次访问出现如下错误:

 断点调试报错如下: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  参数和授权码不同 这种模式适用于公司内相互信任的应用之间,可以直接提供用户名密码

资源服务器

添加标准资源服务器实现

package com.rui.tiger.auth.app;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;

/**
 * @author CaiRui
 * @date 2019-04-17 08:38
 */
@Configuration
@EnableResourceServer
public class TigerResourceServerConfig {
	

}

访问用户信息 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 到此基本的认证和资源服务器测试开发完成,下一章我们进行简单的源码分析

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值