什么是oauth2

OAuth 2.0 是一个授权协议,它允许软件应用代表(而不是充当)资源拥有者去访问资源拥有者的资源。应用向资源拥有者请求授权,然后取得令牌(token),并用它来访问资源,并且资源拥有者不用向应用提供用户名和密码等敏感数据。
当前有一个开放接口 该接口
会被非常多的商户端来调用
管理商户端
开放接口平台设计

第三方支付接口 或者 第三方知名平台接口
微信 支付宝 等。

流程

1.申请一个appid 和 秘钥
Appid=QQ账户—终生无法变化
Apppwd改===QQ密码
2.appid 和密码 获取token
3.需要使用该token调用接口
4.Token 临时且唯一 2个小时 8个小时
Token 失效----刷新token

Oauth角色划分

1、Resource Server:被授权访问的资源
2、Authotization Server:OAUTH2认证授权中心
3、Resource Owner: 用户
4、Client:使用API的合作伙伴

整合代码

1. Authotization Server:OAUTH2认证授权中心模块

  <dependencies><!-- SpringBoot整合Web组件 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><!-- springboot整合freemarker --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-freemarker</artifactId></dependency><!-->spring-boot 整合security --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency><!-- Spring Security OAuth2 --><dependency><groupId>org.springframework.security.oauth</groupId><artifactId>spring-security-oauth2</artifactId><version>2.2.1.RELEASE</version></dependency><dependency><groupId>io.jsonwebtoken</groupId><artifactId>jjwt</artifactId><version>0.6.0</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.62</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId></dependency></dependencies>

security认证 里面认证用户名和密码

@Component
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {@Beanpublic PasswordEncoder passwordEncoder() {return new BCryptPasswordEncoder();}/*** 需要填写 认证账户  mayikt** @param auth* @throws Exception*/@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.inMemoryAuthentication().withUser("mayikt").password(passwordEncoder().encode("mayikt")).authorities("/*");}@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().anyRequest().authenticated() //所有请求都需要通过认证.and().httpBasic() //Basic登录.and().csrf().disable(); //关跨域保护}
}

认证授权Server端 需提供appId,密钥,回调地址 ,资源Id

/*** 认证授权Server端*/
@Component
@EnableAuthorizationServer
public class AuthorizationConfig extends AuthorizationServerConfigurerAdapter {@Autowiredprivate PasswordEncoder passwordEncoder;@Overridepublic void configure(AuthorizationServerSecurityConfigurer security) throws Exception {//允许表单提交security.allowFormAuthenticationForClients().checkTokenAccess("permitAll()");}/*** appid mayikt secret= 123456** @param clients* @throws Exception*/@Overridepublic void configure(ClientDetailsServiceConfigurer clients) throws Exception {clients.inMemory()// appid 表里取 这里写死.withClient("appId")// 密钥  表里取 这里写死.secret(passwordEncoder.encode("123456"))// 授权码.authorizedGrantTypes("authorization_code")// 作用域.scopes("all")// 资源的id  表里取 这里写死.resourceIds("mayikt_resource")// 回调地址  表里取 这里写死.redirectUris("http://www.mayikt.com/callback");}
}

获取access_token步骤

1. 获取授权码
http://localhost:8080/oauth/authorize?client_id=appId&response_type=code
通过访问这个地址获取授权码 ,client_id值为

访问接口填写账户和密码

访问成功后通过回调地址获取授权码

2.根据授权码获取accessToken
http://localhost:8080/oauth/token?code=6s9qUj&grant_type=authorization_code&redirect_uri=http://www.mayikt.com/callback&scope=all

redirect_uri:回调地址
code:授权码

通过postman post请求 填写appid和密钥就可以获取token

2. Resource Server:被授权访问的资源

 <dependencies><!-- SpringBoot整合Web组件 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><!-- springboot整合freemarker --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-freemarker</artifactId></dependency><!-->spring-boot 整合security --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency><!-- Spring Security OAuth2 --><dependency><groupId>org.springframework.security.oauth</groupId><artifactId>spring-security-oauth2</artifactId><version>2.2.1.RELEASE</version></dependency><dependency><groupId>io.jsonwebtoken</groupId><artifactId>jjwt</artifactId><version>0.6.0</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.62</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId></dependency></dependencies>

资源Server端

/*** 资源Server端*/
@Configuration
@EnableResourceServer
public class ResourceConfig extends ResourceServerConfigurerAdapter {//appIDprivate String mayiktAppId ="appId";//密钥private String mayiktAppSecret ="123456";@Beanpublic PasswordEncoder passwordEncoder() {return new BCryptPasswordEncoder();}@Primary@Beanpublic RemoteTokenServices remoteTokenServices() {final RemoteTokenServices tokenServices = new RemoteTokenServices();//设置授权服务器check_token端点完整地址tokenServices.setCheckTokenEndpointUrl("http://localhost:8080/oauth/check_token");//设置客户端id与secret,注意:client_secret值不能使用passwordEncoder加密!tokenServices.setClientId(mayiktAppId);tokenServices.setClientSecret(mayiktAppSecret);return tokenServices;}@Overridepublic void configure(HttpSecurity http) throws Exception {//设置创建session策略http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED);//@formatter:off//所有请求必须授权http.authorizeRequests().anyRequest().authenticated();//@formatter:on}@Overridepublic void configure(ResourceServerSecurityConfigurer resources) {resources.resourceId("mayikt_resource").stateless(true);}
}
@RestController
public class MemberService {@GetMapping("/getMember")public String getMember() {return "我是会员服务接口";}
}

带上token访问成功

oauth2整合security相关推荐

  1. oauth2整合security(密码模式)

    oauth2 password 密码模式获取access_token流程 密码模式(Resource Owner Password Credentials Grant)中,用户向客户端提供自己的用户名 ...

  2. Spring Security OAuth2整合JWT

    文章目录 1. Spring Security 与 OAuth2 2. Spring Security OAuth2的配置和使用 ①:引入依赖 ②:配置 spring security ③:配置授权服 ...

  3. JWT实战 Spring Security Oauth2整合JWT 整合SSO单点登录

    文章目录 一.JWT 1.1 什么是JWT 1.2 JWT组成 头部(header) 载荷(payload) 签名(signature) 如何应用 1.3 JJWT 快速开始 创建token toke ...

  4. springboot整合security,mybatisPlus,thymeleaf实现登录认证及用户,菜单,角色权限管理

    介绍 本系统为springboot整合security,mybatisPlus,thymeleaf实现登录认证及用户,菜单,角色权限管理.页面为极简模式,没有任何渲染. 源码:https://gite ...

  5. 微信官方你真的懂OAuth2?Spring Security OAuth2整合企业微信扫码登录

    ❝ 企业微信扫码登录DEMO参见文末. 现在很多企业都接入了企业微信,作为私域社群工具,企业微信开放了很多API,可以打通很多自有的应用.既然是应用,那肯定需要做登录.正好企业微信提供了企业微信扫码授 ...

  6. SpringCloud+SpringBoot+OAuth2+Spring Security+Redis实现的微服务统一认证授权

    因为目前做了一个基于Spring Cloud的微服务项目,所以了解到了OAuth2,打算整合一下OAuth2来实现统一授权.关于OAuth是一个关于授权的开放网络标准,目前的版本是2.0,这里我就不多 ...

  7. 基于 OAuth2 和 Security 的单点登录 原理与实战

    单点登录是多域名企业站点流行的登录方式.本文以现实生活场景辅助理解,力争彻底理清 OAuth2.0 实现单点登录的原理流程.同时总结了权限控制的实现方案,及其在微服务架构中的应用. 作者:王克锋  出 ...

  8. SpringBoot+OAuth2+Spring Security+Redis+mybatis-plus+mysql+swagger搭建实现

    一.项目结构介绍 源码下载 直接看图就行: 关键点都在config包里面,那我们看看config: 很明了, Excepition 是自定义异常封装类 redis redis缓存配置 security ...

  9. Spring Boot整合Security系列步骤及问题排查(十一)—— 集成QQ登录

    工具类准备: Repository: Connection: ConnectionFactory(ServiceProvider.ApiAdapter): ServiceProvider(OAuth2 ...

最新文章

  1. Java开发利器--Lombok,IDEA端安装教程
  2. mysql 子查询概念_聊聊MySQL的子查询
  3. HDU - 1907 John(尼姆博弈变形)
  4. 从入门到精通!java可视化编程软件哪个好
  5. enum 有什么好处_高新技术企业认定四个核心评分标准是什么?软著能加分吗?...
  6. Java中的内部类与匿名内部类
  7. 内部推荐岗位信息201508
  8. 【信息学奥赛一本通】题解目录
  9. ICCV2019论文题目中文列表
  10. 吴昊品游戏核心算法 Round 18 —— 吴昊教你把妹纸 第一弹 facemash中的妹纸性感程度PK算法...
  11. .NetCore异常:Could not load file or assembly ‘Microsoft.AI.Web‘ or one of its dependencies. The system
  12. 用matlab绘制幅相特性曲线(Nyquist图)
  13. 动感影集制作音乐相册,超简单实用的方法!风靡QQ空间、微信、抖音
  14. 妖人柴:快速拥有一个赚钱的adsense账号终极秘密
  15. 副业赚钱的路子有哪些?分享6种较为实用的副业方式
  16. OpenStack裸金属使用总结
  17. 黄金分割法_python
  18. 大sd卡 裂开了,写保护掉了。重新装好后,被写保护的解决办:
  19. 华硕rt-n16无线打印服务器,华硕无线路由器RT-N16解决无线信号难题
  20. 【信息系统项目管理师学习笔记】10大管理|47个过程组:项目整体管理|指导与管理项目执行

热门文章

  1. 英语----时态---将来时态的四种对比
  2. easyexcel内容追加与单元格合并
  3. UML类图中符号的含义
  4. 索尼播放器Android系统,好物推荐 | 索尼ZX505:Android系统的播放器香不香?
  5. 饿了么手机版-VUE2
  6. 计算机cad教学,计算机CAD绘图基础教程
  7. 斯蒂文斯理工学院计算机科学硕士,美国史蒂文斯理工学院计算机科学专业申请(附案例)...
  8. VMware虚拟机Mac增大容量
  9. 自己的第一个C语言程序 实现Cas9靶序列自动输出为cacc-/aaac-的引物列表——20211020
  10. 高速公路多车协同驾驶控制策略研究:笔记