• Spring Boot 2020 官方基础68课程第24个Spring Boot and OAuth2


    本节展示如何使用OAuth 2.0和Spring Boot构建一个示例应用程序,进行各种登录操作。

    它从一个简单的单一提供者单点登录开始 

    这些示例都是在后端使用Spring Boot和Spring Security的单页应用程序。它们也都在前端使用普通jQuery。但是,转换为不同的JavaScript框架或使用服务器端渲染所需的更改将是最小的。

    所有示例都使用Spring Boot中的本地OAuth 2.0支持实现。 

    • simple:一个非常基本的静态应用程序,只有一个主页,并通过Spring Boot的OAuth 2.0配置属性无条件登录(如果您访问主页,您将自动重定向到GitHub)。
    • click:添加用户必须单击才能登录的显式链接。
    • logout:为经过身份验证的用户添加一个logout链接。
    • 两个提供者:添加第二个登录提供者,以便用户可以在主页上选择使用哪个。
    • 自定义错误:为未经身份验证的用户添加错误消息,以及基于GitHub API的自定义身份验证。

    创建一个springBoot空的Web项目。

    在static下创建index.html ,内容如下:

    1. <!doctype html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="utf-8"/>
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    6. <title>Demo</title>
    7. <meta name="description" content=""/>
    8. <meta name="viewport" content="width=device-width"/>
    9. <base href="/"/>
    10. <link rel="stylesheet" type="text/css" href="/webjars/bootstrap/css/bootstrap.min.css"/>
    11. <script type="text/javascript" src="/webjars/jquery/jquery.min.js"></script>
    12. <script type="text/javascript" src="/webjars/bootstrap/js/bootstrap.min.js"></script>
    13. </head>
    14. <body>
    15. <h1>Demo</h1>
    16. <div class="container"></div>
    17. </body>
    18. </html>

    由于页面引入了bootstrap jquery.需要在Pom里引入依赖。

    1. <dependency>
    2. <groupId>org.webjars</groupId>
    3. <artifactId>jquery</artifactId>
    4. <version>3.4.1</version>
    5. </dependency>
    6. <dependency>
    7. <groupId>org.webjars</groupId>
    8. <artifactId>bootstrap</artifactId>
    9. <version>4.3.1</version>
    10. </dependency>
    11. <dependency>
    12. <groupId>org.webjars</groupId>
    13. <artifactId>webjars-locator-core</artifactId>
    14. </dependency>

    在github上创建一个appID 

     复制Client ID 和client Secret ,修改到yaml里。

    1. spring:
    2. security:
    3. oauth2:
    4. client:
    5. registration:
    6. github:
    7. clientId: 938a bbeaa4d4a
    8. clientSecret: 68d 5124e0f60c8587

    这时再访问,就会跳转到gitHub先登录了。

    如果您一直登录到GitHub,即使您在没有cookies和缓存数据的新浏览器中打开它,也无需重新验证此本地应用。(这就是单点登录的含义。)

    添加一个controller

    1. @GetMapping("/user")
    2. public Map user(@AuthenticationPrincipal OAuth2User principal) {
    3. return Collections.singletonMap("name", principal.getAttribute("name"));
    4. }

     

    1. <script type="text/javascript">
    2. $.get("/user", function(data) {
    3. $("#user").html(data.name);
    4. $(".unauthenticated").hide()
    5. $(".authenticated").show()
    6. });
    7. </script>

     

    看来可以试一下整合QQ登录。 先QQ互联官网首页

     

     

     

     1. 实现QQ登录功能

    2.1. 直接打开QQ登录弹窗

    2.2. 注销当前登录用户

    2.3. 检测当前登录状态

    2.4. 获取当前登录用户的Access Token以及OpenID

     

  • 相关阅读:
    视觉SLAM ch5——相机与图像
    学习nginx,这一篇就够了
    搭建搜题公众号【最新】
    计算机专业毕业论文java毕业设计开题报告SSM超市管理系统[包运行成功]
    力扣(LeetCode)177. 第N高的薪水(2022.06.26)
    丢了8年,含3.5亿美元的比特币密钥硬盘有望找回?
    应用案例|基于高精度三维机器视觉引导机器人自动分拣包裹的应用
    零配置python日志,安装即用
    [激光器原理与应用-9]: 开关电源主要指标
    2023年【陕西省安全员B证】考试报名及陕西省安全员B证模拟试题
  • 原文地址:https://blog.csdn.net/ldy889/article/details/127550812