视频演示: https://www.bilibili.com/video/BV1M54y1z7A9/

需要完成以下步骤:

  1. 创建工程并引入依赖包

    • spring-boot-starter-oauth2-client

    • spring-boot-starter-security

    • spring-boot-starter-web

  2. 创建Security配置信息

  3. 创建Github、Google的client-id和client-secret

#Oauth2GoogleLoginApplication.java

package com.deepincoding.oauth2googlelogin;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class Oauth2GoogleLoginApplication {public static void main(String[] args) {SpringApplication.run(Oauth2GoogleLoginApplication.class, args);}}

#MessageController.java

package com.deepincoding.oauth2googlelogin;import lombok.extern.log4j.Log4j2;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@Log4j2
public class MessageController {@GetMapping("/")public String hello(){return "Hello Google Github.";}@GetMapping("/guest")public String guest(){return "Hello Guest.";}@GetMapping("/admin")public OAuth2User admin(@AuthenticationPrincipal OAuth2User principal){return principal;}}

#SecurityConfig.java

package com.deepincoding.oauth2googlelogin;import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;@Configuration
public class SecurityConfig  extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.antMatcher("/**").authorizeRequests().antMatchers("/", "/guest").permitAll().anyRequest().authenticated().and().oauth2Login();}
}

#application.yml


spring:security:oauth2:client:registration:github:client-id: d64d35f9f66ea04ba64fclient-secret: b84a72f20fcec596deb947860cd4eacaf55b0f5bgoogle:client-id: 195066099347-nj4et113vfl9p0aq9k5cb4hd6kg0c3hh.apps.googleusercontent.comclient-secret: Wk5XNm8wv36R0h4zGCxWOfYn

本文使用 mdnice 排版

Google、Github账号等...登录Web应用相关推荐

  1. 解决Google浏览器账号无法登录提示浏览器不安全

  2. 如何支持github账号登录

    转载自:http://www.cooklife.cn 有些时候我们需要自己的网站支持第三方账号的登录,这里来讲讲如何让自己的网站支持github账号的支持. 1.在github上面建立一个应用对应自己 ...

  3. java github关联登录_没错,用三方Github做授权登录就是这么简单!(OAuth2.0实战)...

    全2册git版本控制管理(第2版)+ 99.8元 包邮 (需用券) 去购买 > 本文收录在个人博客:www.chengxy-nds.top,技术资源共享. 上一篇<OAuth2.0 的四种 ...

  4. 没错,用三方 Github 做授权登录就是这么简单!(OAuth2.0实战)

    本文收录在个人博客:www.chengxy-nds.top,技术资源共享. 上一篇<OAuth2.0 的四种授权方式>文末说过,后续要来一波OAuth2.0实战,耽误了几天今儿终于补上了. ...

  5. Android 自带浏览器谷歌账号自动登录的实现

    Google 的账号自动登录功能主要依靠x-auto-login这个http头部字段来触发的. X-auto-login是google私有的一个头部字段,在android自带浏览器和chrome中存在 ...

  6. IDEA登录Github账号失败的解决办法

    IDEA登录Github账号失败的解决办法 如图,密码输入正确也无法登录 解决办法:使用token登录 1.登录GitHub,点击settings 2.生成新的token 3.将token输入到IDE ...

  7. google 云开启 ssh 账号密码登录

    google 云开启 ssh 账号密码登录 1.新买的 google 服务器是不支持使用账号密码通过 ssh 登录的,这样就很不方便,要使用账号密码登录需要修改 ssh 配置文件. 先通过浏览器打开 ...

  8. idea登录github账号,出现Incorrect Credentials 401

    idea无法连接github账号. 使用网上的方法,修改密码,修改http代理都没有效果. 最后通过使用use token登录. 登录到github,找到设置界面. 点击红框 note里面随便填 下面 ...

  9. GitHub账号注册与登录

    GitHub账号注册与登录 一.注册 1.打开官网:https://github.com/login 2.点击"Create an account",进入GitHub注册页面 3. ...

最新文章

  1. ThinkPHP 详细介绍
  2. 实现盒子动画和键盘特效
  3. 团队开发博客成员介绍(发在个人博客中了)
  4. 《过早退出是一切失败的根源》读后感
  5. Java 关于类的专题
  6. Pycharm 2019 添加 docker 解释器
  7. 使用Firebase、Angular 8和ASP.NET Core 3.1保护网站
  8. 乐高科技系列搭建指南 pdf_玩转乐高创意亲子搭建系列(4)小颗粒作品
  9. C中的malloc()和calloc()
  10. flash跟随鼠标样式
  11. 共轭梯度法matlab程序精确线搜索,具有精确线性搜索的改进共轭梯度法
  12. 如何使用代码调用API接口发送短信?(以SendCloud为例)
  13. 内存泄漏(Memory Leak )和内存溢出
  14. Android OpenCV(三十二):霍夫直线检测
  15. Android 集成友盟推送方案(1)
  16. webRTC(十):webrtc 实现web端对端视频
  17. 计算机英语复试自我介绍,计算机复试英语自我介绍
  18. android 4.4 x86 iso,安卓x86 4.4 iso下载
  19. Halcon例程分析8:投影变换矫正倾斜图像
  20. 接口规范-API接口

热门文章

  1. 【Android App 开发基础】
  2. MYSQL修改密码(附报错解决方案)
  3. 要是我们得不到我们希望的东西,最好不要让忧虑和悔恨来苦恼我们的生活。”且让我们原谅自己,学得豁达一点。 “身外物,不奢恋。
  4. 课程实验 【八路抢答器】
  5. WORD实用奇招妙技大荟萃
  6. Play on Words-uva 精短欧拉回路
  7. WebGL浅入浅出,不深入了解一下吗?
  8. python自动抓包手机_Python爬虫入门:教你通过Fiddler进行手机抓包!
  9. Tomcat 的安装与环境配置教程(超详细)
  10. 中英翻译《森林火灾的预防措施》