近期一直在研究鉴权方面的各种案例,这几天有空,写一波总结及经验。

第一步:什么是 OAuth鉴权

OAuth2是工业标准的授权协议。OAuth2取代了在2006创建的原始OAuthTM协议所做的工作。OAuth 2.0关注客户端开发人员的简单性,同时为Web应用程序、桌面应用程序、移动电话和客厅设备提供特定的授权流。

参考理解:  Oauth2.0     理解OAuth 2.0     QQ授权      微信授权

第二步:什么是密码模式

密码模式(Resource Owner Password Credentials Grant)中,用户向客户端提供自己的用户名和密码。客户端使用这些信息,向"服务商提供商"索要授权。

第三步:密码权流程

  1. 交互步骤:
    A)用户向客户端提供用户名和密码。
  2.  
  3. B)客户端将用户名和密码发给认证服务器,向后者请求令牌。
  4.  
  5. C)认证服务器确认无误后,向客户端提供访问令牌。

第四步:密码模式实践

       源码地址:

               github: https://github.com/GitHubZhangCom/spring-security-oauth-example/

               码云:https://gitee.com/region/spring-security-oauth-example/

第五:密码模式的核心代码唯一和授权码模式的区别在于,将授权码模式改为密码模式就ok。

       

  1. package com.auth.server.config;
  2.  
  3. import javax.sql.DataSource;
  4.  
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.beans.factory.annotation.Qualifier;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. import org.springframework.security.authentication.AuthenticationManager;
  10. import org.springframework.security.core.userdetails.UserDetailsService;
  11. import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
  12. import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
  13. import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
  14. import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
  15. import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
  16. import org.springframework.security.oauth2.provider.token.TokenStore;
  17. import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;
  18.  
  19. /**
  20. * 授权配置
  21. * @author wb0024
  22. *
  23. */
  24. @Configuration
  25. @EnableAuthorizationServer
  26. public class ServerConfig extends AuthorizationServerConfigurerAdapter {
  27. @Autowired
  28. private AuthenticationManager authenticationManager;
  29.  
  30. @Qualifier("myUserDetailsService")
  31. @Autowired
  32. private UserDetailsService userDetailsService;
  33.  
  34. // @Autowired
  35. // @Qualifier("dataSource")
  36. // private DataSource dataSource;
  37.  
  38. @Override
  39. public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
  40. // 配置token获取和验证时的策略
  41. security.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()");
  42. }
  43.  
  44. @Override
  45. public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
  46. clients.inMemory()
  47. .withClient("client")
  48. // secret密码配置从 Spring Security 5.0开始必须以 {加密方式}+加密后的密码 这种格式填写
  49.  
  50. /* * 当前版本5新增支持加密方式:
  51. * bcrypt - BCryptPasswordEncoder (Also used for encoding)
  52. * ldap - LdapShaPasswordEncoder
  53. * MD4 - Md4PasswordEncoder
  54. * MD5 - new MessageDigestPasswordEncoder("MD5")
  55. * noop - NoOpPasswordEncoder
  56. * pbkdf2 - Pbkdf2PasswordEncoder
  57. * scrypt - SCryptPasswordEncoder
  58. * SHA-1 - new MessageDigestPasswordEncoder("SHA-1")
  59. * SHA-256 - new MessageDigestPasswordEncoder("SHA-256")
  60. * sha256 - StandardPasswordEncoder*/
  61.  
  62. .secret("{noop}secret")
  63. .scopes("all")
  64. .authorizedGrantTypes("password", "refresh_token")//password模式
  65. //.authorizedGrantTypes("authorization_code", "password", "refresh_token")//授权码模式和password模式
  66. //.authorizedGrantTypes("authorization_code", "refresh_token")//授权码模式
  67. .autoApprove(true);
  68. }
  69.  
  70. @Override
  71. public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
  72. // // 配置tokenStore,保存到redis缓存中
  73. // endpoints.authenticationManager(authenticationManager)
  74. // .tokenStore(new MyRedisTokenStore(redisConnectionFactory))
  75. // // 不添加userDetailsService,刷新access_token时会报错
  76. // .userDetailsService(userDetailsService);
  77.  
  78. // 使用最基本的InMemoryTokenStore生成token
  79. endpoints.authenticationManager(authenticationManager).tokenStore(memoryTokenStore());
  80.  
  81. }
  82.  
  83. // 使用最基本的InMemoryTokenStore生成token
  84. @Bean
  85. public TokenStore memoryTokenStore() {
  86. return new InMemoryTokenStore();
  87. }
  88. }

详情请看源码!!!

springboot oauth 鉴权之——password鉴权相当于jwt鉴权模式的更多相关文章

  1. Spring Boot 鉴权之—— JWT 鉴权

    第一:什么是JWT鉴权 1. JWT即JSON Web Tokens,是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准((RFC 7519),他可以用来安全的传递信息,因为传递的信息是 ...

  2. HTTP基本认证和JWT鉴权

    一.HTTP基本认证 Basic Authentication——当浏览器访问使用基本认证的网站的时候, 浏览器会提示你输入用户名和密码. http auth的过程: · 客户端发送http请求 ·  ...

  3. JWT鉴权

    一.HTTP基本认证 Basic Authentication--当浏览器访问使用基本认证的网站的时候, 浏览器会提示你输入用户名和密码. http auth的过程: 客户端发送http请求 服务器发 ...

  4. jwt鉴权学习 (php示例代码)

    前段时间听朋友讲起 jwt鉴权,博主我是一脸懵逼,通过朋友坚持不懈的讲解,我终于听懂了,jwt就是登陆token校验嘛 然而事情并不是博主想象的那么简单,在一个艳阳高照,晴空万里的夜晚,博主手贱百度了 ...

  5. go-zero docker-compose 搭建课件服务(六):完善jwt鉴权和返回结构

    0.转载 go-zero docker-compose 搭建课件服务(六):完善jwt鉴权和返回结构 0.1源码地址 https://github.com/liuyuede123/go-zero-co ...

  6. springboot oauth 鉴权之——password、authorization_code鉴权

    参考一下两个案例:https://www.cnblogs.com/haoliyou/p/9606018.html https://www.cnblogs.com/haoliyou/p/9606036. ...

  7. springboot oauth 鉴权之——授权码authorization_code鉴权

    近期一直在研究鉴权方面的各种案例,这几天有空,写一波总结及经验. 第一步:什么是 OAuth鉴权 OAuth2是工业标准的授权协议.OAuth2取代了在2006创建的原始OAuthTM协议所做的工作. ...

  8. shiro,基于springboot,基于前后端分离,从登录认证到鉴权,从入门到放弃

    这个demo是基于springboot项目的. 名词介绍: ShiroShiro 主要分为 安全认证 和 接口授权 两个部分,其中的核心组件为 Subject. SecurityManager. Re ...

  9. 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 微博开放接口的调用,如 ...

随机推荐

  1. Spring核心实现篇

    一.Spring Framework的核心:IoC容器的实现 1.1Spring IoC的容器概述 1.1.1 IoC容器和控制反转模式 依赖控制反转的实现有很多种方式.在Spring中,IOC容器是 ...

  2. java添加后台缓存

    public class Cache { private String key;//缓存ID private Object value;//缓存数据 private long timeOut;//更新 ...

  3. request请求生命周期

    request请求生命周期 一.request请求分析 1.1. request数据请求 # views.py from rest_framework.views import APIView fro ...

  4. redis简单了解与简单使用

    redis数据库 为什么要学习redis """ 1.redis是内存 no-sql 数据库,相比mysql等硬盘数据库效率高 2.在内存值配置数据库使用,而不直接使用内 ...

  5. [原]livekd使用问题记录

    sysinternal suite中的livekd.exe可谓神器.可以用来观察本地内核的一些状态,当然抓内核dump再合适不过了. 在使用livekd的时候遇到了一些问题,现总结如下: 使用live ...

  6. spark docker java kubernetes 获取cpu内核/线程数问题

    升级服务从spark2.3.0-hadoop2.8 至 spark2.4.0 hadoop3.0 一日后导致spark streaming kafka消费数据积压 服务不是传统的部署在yarn上,而是 ...

  7. tc/traffic control 网络控制工具

    第一个例子 增加延时 tc qdsic add dev enp0s3 root netem delay 200ms qdisc : queuing discipline, 当内核需要发送包到某个接口时 ...

  8. 跟踪路由(tracert)及ping命令

    由于最近学校网络不好,老是有问题,加上最近写了个数据展示系统,要部署到买的域名下,用到了这两个命令 首先,一台服务器,一台工作站,一个笔记本(我的,来测试ip是否通的) 服务器已经部署了三个网站(一个 ...

  9. 我的 xelatex 模板

    \documentclass[twoside,11pt]{article} \usepackage{amsmath,amsfonts} \usepackage{hyperref} \usepackag ...

  10. memcached_高可用

    memcached高可用 一.magent 1.安装 cd /usr/local/mkdir ./magentcd ./magentwget -c http://memagent.googlecode ...