springboot oauth 鉴权之——password鉴权相当于jwt鉴权模式
近期一直在研究鉴权方面的各种案例,这几天有空,写一波总结及经验。
第一步:什么是 OAuth鉴权
OAuth2是工业标准的授权协议。OAuth2取代了在2006创建的原始OAuthTM协议所做的工作。OAuth 2.0关注客户端开发人员的简单性,同时为Web应用程序、桌面应用程序、移动电话和客厅设备提供特定的授权流。
参考理解: Oauth2.0 理解OAuth 2.0 QQ授权 微信授权
第二步:什么是密码模式
密码模式(Resource Owner Password Credentials Grant)中,用户向客户端提供自己的用户名和密码。客户端使用这些信息,向"服务商提供商"索要授权。
第三步:密码权流程

交互步骤:
(A)用户向客户端提供用户名和密码。 (B)客户端将用户名和密码发给认证服务器,向后者请求令牌。 (C)认证服务器确认无误后,向客户端提供访问令牌。
第四步:密码模式实践
源码地址:
github: https://github.com/GitHubZhangCom/spring-security-oauth-example/
码云:https://gitee.com/region/spring-security-oauth-example/
第五:密码模式的核心代码唯一和授权码模式的区别在于,将授权码模式改为密码模式就ok。
package com.auth.server.config; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore; /**
* 授权配置
* @author wb0024
*
*/
@Configuration
@EnableAuthorizationServer
public class ServerConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager; @Qualifier("myUserDetailsService")
@Autowired
private UserDetailsService userDetailsService; // @Autowired
// @Qualifier("dataSource")
// private DataSource dataSource; @Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
// 配置token获取和验证时的策略
security.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()");
} @Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("client")
// secret密码配置从 Spring Security 5.0开始必须以 {加密方式}+加密后的密码 这种格式填写 /* * 当前版本5新增支持加密方式:
* bcrypt - BCryptPasswordEncoder (Also used for encoding)
* ldap - LdapShaPasswordEncoder
* MD4 - Md4PasswordEncoder
* MD5 - new MessageDigestPasswordEncoder("MD5")
* noop - NoOpPasswordEncoder
* pbkdf2 - Pbkdf2PasswordEncoder
* scrypt - SCryptPasswordEncoder
* SHA-1 - new MessageDigestPasswordEncoder("SHA-1")
* SHA-256 - new MessageDigestPasswordEncoder("SHA-256")
* sha256 - StandardPasswordEncoder*/ .secret("{noop}secret")
.scopes("all")
.authorizedGrantTypes("password", "refresh_token")//password模式
//.authorizedGrantTypes("authorization_code", "password", "refresh_token")//授权码模式和password模式
//.authorizedGrantTypes("authorization_code", "refresh_token")//授权码模式
.autoApprove(true);
} @Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
// // 配置tokenStore,保存到redis缓存中
// endpoints.authenticationManager(authenticationManager)
// .tokenStore(new MyRedisTokenStore(redisConnectionFactory))
// // 不添加userDetailsService,刷新access_token时会报错
// .userDetailsService(userDetailsService); // 使用最基本的InMemoryTokenStore生成token
endpoints.authenticationManager(authenticationManager).tokenStore(memoryTokenStore()); } // 使用最基本的InMemoryTokenStore生成token
@Bean
public TokenStore memoryTokenStore() {
return new InMemoryTokenStore();
}
}
详情请看源码!!!
springboot oauth 鉴权之——password鉴权相当于jwt鉴权模式的更多相关文章
- Spring Boot 鉴权之—— JWT 鉴权
第一:什么是JWT鉴权 1. JWT即JSON Web Tokens,是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准((RFC 7519),他可以用来安全的传递信息,因为传递的信息是 ...
- HTTP基本认证和JWT鉴权
一.HTTP基本认证 Basic Authentication——当浏览器访问使用基本认证的网站的时候, 浏览器会提示你输入用户名和密码. http auth的过程: · 客户端发送http请求 · ...
- JWT鉴权
一.HTTP基本认证 Basic Authentication--当浏览器访问使用基本认证的网站的时候, 浏览器会提示你输入用户名和密码. http auth的过程: 客户端发送http请求 服务器发 ...
- jwt鉴权学习 (php示例代码)
前段时间听朋友讲起 jwt鉴权,博主我是一脸懵逼,通过朋友坚持不懈的讲解,我终于听懂了,jwt就是登陆token校验嘛 然而事情并不是博主想象的那么简单,在一个艳阳高照,晴空万里的夜晚,博主手贱百度了 ...
- go-zero docker-compose 搭建课件服务(六):完善jwt鉴权和返回结构
0.转载 go-zero docker-compose 搭建课件服务(六):完善jwt鉴权和返回结构 0.1源码地址 https://github.com/liuyuede123/go-zero-co ...
- springboot oauth 鉴权之——password、authorization_code鉴权
参考一下两个案例:https://www.cnblogs.com/haoliyou/p/9606018.html https://www.cnblogs.com/haoliyou/p/9606036. ...
- springboot oauth 鉴权之——授权码authorization_code鉴权
近期一直在研究鉴权方面的各种案例,这几天有空,写一波总结及经验. 第一步:什么是 OAuth鉴权 OAuth2是工业标准的授权协议.OAuth2取代了在2006创建的原始OAuthTM协议所做的工作. ...
- shiro,基于springboot,基于前后端分离,从登录认证到鉴权,从入门到放弃
这个demo是基于springboot项目的. 名词介绍: ShiroShiro 主要分为 安全认证 和 接口授权 两个部分,其中的核心组件为 Subject. SecurityManager. Re ...
- apigw鉴权分析(1-4)新浪微博开放平台 - 鉴权分析
一.访问入口 http://open.weibo.com/wiki/%E6%8E%88%E6%9D%83%E6%9C%BA%E5%88%B6%E8%AF%B4%E6%98%8E 微博开放接口的调用,如 ...
随机推荐
- Go-简介-发展
01-Go语言介绍 目录 Go语言介绍 Go语言特性 Go语言发展(版本/特性) Go语言应用 谁在用 应用领域 Go语言项目 Go语架构 Go语言发展前景 Go语言介绍 Go 即Golang,是Go ...
- Linux-进程关系
(1).无关系 (2).父子进程关系 (3).进程组(group):由若干个进程构成一个进程组 (4).会话(session):由若干个进程组构成一个会话
- Ubuntu下安装Docker,及Docker的一些常用命令操作
1.什么是 Docker Docker 是一个开源项目,Docker 项目的目标是实现轻量级的操作系统虚拟化解决方案. Docker 的基础是 Linux 容器(LXC ...
- idea快捷键(最常用)
--跳到上一空白行 ctrl+alt+enter --跳到下一空白行 shift+enter --为代码生成包裹快(try catch等) ctrl+alt+t --跳到某行 ctrl+g --实现父 ...
- php 连接oracle插入多张图片的方法
php连接oracle数据库的时候,其查询.更新.删除数据和MySQL类似,但是增加数据.特别是图片的时候就很不一样,这里面涉及到要创建一个blob对象,用blod对象去保存php图片,下面是当插入多 ...
- hibernate 持久化对象 save
hibernate 持久化对象 save new出来的user对象是游离状态的对象,执行session.save()方法保存后,user对象就变为持久化了,持久化的对象跟数据库表双向绑定的意思, 对象 ...
- spring security记录
4版本 登录跳转 自定义自己的成功失败handler 生成session后 set-cookie响应头用js取不到? https://github.com/axios/axios/issues/295 ...
- 对xgboost中dump_model生成的booster进行解析
xgboost原生包中有一个dump_model方法,这个方法能帮助我们看到基分类器的决策树如何选择特征进行分裂节点的,使用的基分类器有两个特点: 二叉树: 特征可以重复选择,来切分当前节点所含的数据 ...
- wireshark的过滤命令
1.ip.addr == 192.168.1.1 这种是目标地址和源地址都是 后面指定的IP
- python之接口自动化测试框架
梳理python+unittest接口自动化测试框架的思路: 1.确定目录: cases:存放测试用例的py文件:config:存放一些数据库,环境地址等固定不变的信息: core:核心的文件, ca ...