首页 > 编程语言 > 详细

世界上最好的SpringCloud Oauth2 DEMO教程1

时间:2020-06-11 12:00:56      阅读:81      评论:0      收藏:0      [点我收藏+]

最开始接触Oauth的时候,是阮一峰老师的博客,http://www.ruanyifeng.com/blog/2019/04/oauth-grant-types.html

可以说是通俗易懂的,但是如果用Java语言实现这个规范,那真的太难了,好在SpringCloud 提供了Oauth2的实现,但是问题真的是太多了,还无法在生产环境使用,经过自己的不懈努力,终于实现了一个比DEMO要好一个级别的DEMO。

本人能力有限,如果错误,请提出来告知,三人行必有我师。

带着这个疑问,我开始看SpringCloud官方文档,https://spring.io/projects/spring-cloud-security#overview

技术分享图片:" 技术分享图片".

我在百度上开始找啊找.

期望是这样的

这文章写的太好了,我操作了一遍问题解决了,感谢作者.

技术分享图片

实际是这样的

CNMD,我操作了一遍,浪费了1小时,脑瓜子要炸了.

技术分享图片

 

这事能怪我吗?

技术分享图片

第一种授权方式:授权码

什么是授权码呢? 

参考蜻蜓的这个案例

技术分享图片

 

 

 

技术分享图片

 

 

 

当我们点击QQ的时候,会跳转到QQ的认证服务器,之后让你用QQ登录成功后,把权限(全部,或者获取昵称等)赋给蜻蜓这个第三方应用。

通过查看Oauth2的规范我们知道

Oauth2包括几个部分

1,客户端,相当于第三方应用蜻蜓

2,认证服务器,腾讯的登录认证系统

3,授权服务器,腾讯的授权系统

4,资源服务器,腾讯存储用户相关信息的系统

 

实现授权码这种模式我们要做什么事情呢?

第一,编写Oauth2客户端代码配置Oauth2服务器端配置

第二,在Oauth2服务器上申请接入(申请接入意味着服务器端会给客户端一个clientId,一个clientSecret)

以Github为例,我们实现这个授权码模式的客户端

创建一个SpringCloud项目

技术分享图片

引入如下两个pom即可 

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>

在配置文件中配置Github服务器的相关信息

application.properties

security.oauth2.client.clientId=
security.oauth2.client.clientSecret=
security.oauth2.client.accessTokenUri=https://github.com/login/oauth/access_token
security.oauth2.client.userAuthorizationUri=https://github.com/login/oauth/authorize
security.oauth2.client.clientAuthenticationScheme=form
security.oauth2.resource.userInfoUri=https://api.github.com/user

登录到github
打开下面的网址
https://github.com/settings/developers

技术分享图片

 

点击新建App

 

 技术分享图片

 前三个随便填写,第四个必须填写正确

spring.application.name=client
server.servlet.context-path=/client
server.address=192.168.0.110
server.port=50000
上面的配置对应如下的URL
http://192.168.0.110:50000/client/login

编写如下类
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
@EnableOAuth2Sso
public class Oauth2WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.authorizeRequests().antMatchers("/login", "/login/", "/api/**").permitAll().anyRequest().authenticated();
}
}

接口测试类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
* @author admin
*/
@RestController
public class UserController {

@GetMapping("/order")
public String getOrder(String json) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
return "order id : " + json;
}

}
启动SpringCloud应用
http://192.168.0.110:50000/client/order?json=xddd
技术分享图片

 登录后等待授权

 技术分享图片

 

 点击授权后,会跳转到/order接口

 可以DEBUG 从下面的对象中获取github用户相关的信息,例如用户账号信息,头像,昵称等 

 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

技术分享图片

 

可以看到接口可以访问到了

 技术分享图片

 

 

注意:

有时候githu接口会超时等问题导致失败,可能是个人账号不稳定

security.oauth2.resource.userInfoUri=https://api.github.com/user

oauth2作为客户端引入后,默认的 spring.security 已经没有作用了

下一步,自己实现Oauth2 服务器端,完全不是普通DEMO级别的,全网唯一的DEMO

世界上最好的SpringCloud Oauth2 DEMO教程1

原文:https://www.cnblogs.com/qnmlgbb/p/13091724.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!