Google、Github账号等...登录Web应用
视频演示: https://www.bilibili.com/video/BV1M54y1z7A9/
需要完成以下步骤:
- 创建工程并引入依赖包
spring-boot-starter-oauth2-client
spring-boot-starter-security
spring-boot-starter-web
创建Security配置信息
创建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应用相关推荐
- 解决Google浏览器账号无法登录提示浏览器不安全
- 如何支持github账号登录
转载自:http://www.cooklife.cn 有些时候我们需要自己的网站支持第三方账号的登录,这里来讲讲如何让自己的网站支持github账号的支持. 1.在github上面建立一个应用对应自己 ...
- java github关联登录_没错,用三方Github做授权登录就是这么简单!(OAuth2.0实战)...
全2册git版本控制管理(第2版)+ 99.8元 包邮 (需用券) 去购买 > 本文收录在个人博客:www.chengxy-nds.top,技术资源共享. 上一篇<OAuth2.0 的四种 ...
- 没错,用三方 Github 做授权登录就是这么简单!(OAuth2.0实战)
本文收录在个人博客:www.chengxy-nds.top,技术资源共享. 上一篇<OAuth2.0 的四种授权方式>文末说过,后续要来一波OAuth2.0实战,耽误了几天今儿终于补上了. ...
- Android 自带浏览器谷歌账号自动登录的实现
Google 的账号自动登录功能主要依靠x-auto-login这个http头部字段来触发的. X-auto-login是google私有的一个头部字段,在android自带浏览器和chrome中存在 ...
- IDEA登录Github账号失败的解决办法
IDEA登录Github账号失败的解决办法 如图,密码输入正确也无法登录 解决办法:使用token登录 1.登录GitHub,点击settings 2.生成新的token 3.将token输入到IDE ...
- google 云开启 ssh 账号密码登录
google 云开启 ssh 账号密码登录 1.新买的 google 服务器是不支持使用账号密码通过 ssh 登录的,这样就很不方便,要使用账号密码登录需要修改 ssh 配置文件. 先通过浏览器打开 ...
- idea登录github账号,出现Incorrect Credentials 401
idea无法连接github账号. 使用网上的方法,修改密码,修改http代理都没有效果. 最后通过使用use token登录. 登录到github,找到设置界面. 点击红框 note里面随便填 下面 ...
- GitHub账号注册与登录
GitHub账号注册与登录 一.注册 1.打开官网:https://github.com/login 2.点击"Create an account",进入GitHub注册页面 3. ...
最新文章
- ThinkPHP 详细介绍
- 实现盒子动画和键盘特效
- 团队开发博客成员介绍(发在个人博客中了)
- 《过早退出是一切失败的根源》读后感
- Java 关于类的专题
- Pycharm 2019 添加 docker 解释器
- 使用Firebase、Angular 8和ASP.NET Core 3.1保护网站
- 乐高科技系列搭建指南 pdf_玩转乐高创意亲子搭建系列(4)小颗粒作品
- C中的malloc()和calloc()
- flash跟随鼠标样式
- 共轭梯度法matlab程序精确线搜索,具有精确线性搜索的改进共轭梯度法
- 如何使用代码调用API接口发送短信?(以SendCloud为例)
- 内存泄漏(Memory Leak )和内存溢出
- Android OpenCV(三十二):霍夫直线检测
- Android 集成友盟推送方案(1)
- webRTC(十):webrtc 实现web端对端视频
- 计算机英语复试自我介绍,计算机复试英语自我介绍
- android 4.4 x86 iso,安卓x86 4.4 iso下载
- Halcon例程分析8:投影变换矫正倾斜图像
- 接口规范-API接口