Oauth2整合gitee

原理

首先,我们的目的是通过第三方账号的授权来实现用户的登录操作,也即用户不用直接创建我们网站或应用的账户,而是直接通过原有的第三方账户来登录,比如微信,qq等。
其原理图如下:
在这里插入图片描述
原理简单讲就是先跳转到第三方应用(如qq)的登录界面,然后用户输入账号密码登录成功,并且统一授权,第三方应用的服务器就会返回一个code给我方服务器,我方服务器再带着这个code去第三方应用的服务器去换取一个access_token,获取这个access_token之后我们就能够使用被授权的各种功能,比如查看用户信息这些,具体能够进行哪些操作要去看第三方应用提供的api,然后根据接口调用,调用时都需要带着这个access_token去操作。
而对于我们这里登录来说,我们获取access_token后,就去获取一些信息来创建一个我们网站的新账户,首先我们需要一个它再原第三方应用中就是unique的信息,比如id,然后还可以获取头像这些信息,然后注入到我们网站的给他新创建的哪个账户中,之后用户登录的时候也是先获取第三方应用中的信息,然后看看我们网站的用户数据中有没有哪个账户的第三方应用id字段等于这个用户的第三方应用id,有的话就直接登录,没有的话就要给他创建个账户。
总之,不是不创建账户,只是将其第三方账户关联到我们的用户账号中,自动从其第三方应用的账户中获取创建我们用户账户所需要的字段,然后帮用户省去自己创建账户的繁琐过程。那么这就要求我们从他的第三方账户中获取得到一个能唯一标识其身份的字段,比如前面说的id,这样才能唯一关联我们的用户账户。当然,如果我们无法获取唯一标识字段,只能获取到头像或者性别这样的一些信息,我们可以再第三方授权获得这些信息后再加一个步骤来补充需要的信息,比如授权后还需要其输入手机号码(好像看见很多小程序都这样干),用这个手机号码来唯一标识他。

实现:

参考gitee官方文档:gitee官网文档:https://gitee.com/api/v5/oauth_doc#/
去第三方应用的创建一个我们网站的接口,相对于去他那登记下,我们这个应用到时候需要它授权哪些东西。
这类平台有微博开放平台,微信开放平台,gitee,前两个审核麻烦,gitee直接就能用。

以gitee为例,首先我们需要去gitee创建一个第三方应用(相对gitee而言我们叫第三方应用),在个人信息的第三方应用,如下所示:
在这里插入图片描述
里面的主页地址指的是登录成功后跳转的地址,回调地址是指用户输入账户密码,然后统一授权后会得到一个code,然后这个code信息交给哪个接口来处理。这里地址可以就是本地的地址,像http://localhost:8080/path/to/xx都可以。

首先,用户点击我们登录页面的第三方登录按钮,比如gitee登录,我们给该登录按钮放上gitee的Oauth给的那个链接:https://gitee.com/oauth/authorize?client_id={client_id}&redirect_uri={redirect_uri}&response_type=code,里面的client_id和redirect_uri这些就是我们上面再gitee登记应用的信息,然后就会进入gitee的登录界面。
前端代码:

	<a href="https://gitee.com/oauth/authorize?client_id=4d34db3a2769cb4bd9a4a53cec8a387691844bab70f1172e35423ca9a6d0808e&redirect_uri=http://auth.gulimall.com/oauth2.0/gitee/success&response_type=code">
						<img style="width: 50px;height: 18px;" src="/static/login/JD_img/gittee.jpg"/>	</a>

过程如下:

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
然后就会带着code到我们在应用中写的后端接口处"http://auth.gulimall.com/oauth2.0/gitee/success".
我们在后端接口中再写代码向第三方服务器发请求,带着code这些参数

https://gitee.com/oauth/token?grant_type=authorization_code&code={code}&client_id={client_id}&redirect_uri={redirect_uri}&client_secret={client_secret}

然后得到访问的token。
然后根据token获取信息来创建我们的用户。

下雨了,不写了,回寝室去躺着了。

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot 2 和 OAuth2 可以通过 Spring Security 实现集成。 下面是一个简单的 Spring Boot 2 OAuth2 集成的示例: 1. 在 pom.xml 文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.security.oauth.boot</groupId> <artifactId>spring-security-oauth2-autoconfigure</artifactId> <version>2.0.0.RELEASE</version> </dependency> ``` 2. 创建一个 WebSecurityConfigurerAdapter 类并重写 configure 方法: ``` @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsService userDetailsService; @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/login").permitAll() .antMatchers("/oauth/authorize").authenticated() .and().formLogin().permitAll(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); } } ``` 3. 创建一个 ResourceServerConfigurerAdapter 类并重写 configure 方法: ``` @Configuration @EnableResourceServer public class ResourceServerConfig extends ResourceServerConfigurerAdapter { @Override public void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/api/**").authenticated() .and().authorizeRequests().anyRequest().permitAll(); } } ``` 4. 创建一个 AuthorizationServerConfigurerAdapter 类并重写 configure 方法: ``` @Configuration @EnableAuthorizationServer public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { @Autowired private AuthenticationManager authenticationManager; @Autowired private UserDetailsService userDetailsService; @Autowired private DataSource dataSource; @Bean public TokenStore tokenStore() { return new JdbcTokenStore(dataSource); } @Bean public ClientDetailsService clientDetailsService() { return new JdbcClientDetailsService(dataSource); } @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.jdbc(dataSource); } @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints.tokenStore(tokenStore()).authenticationManager(authenticationManager).userDetailsService(userDetailsService); } } ``` 5. 配置 application.yml 文件: ``` spring: datasource: url: jdbc:mysql://localhost:3306/oauth2 username: root password: root driver-class-name: com.mysql.jdbc.Driver security: oauth2: client: client-id: client client-secret: secret access-token-uri: http://localhost:8080/oauth/token user-authorization-uri: http://localhost:8080/oauth/authorize scope: read,write resource: user-info-uri: http://localhost:8080/api/userinfo ``` 在上面的配置中,我们使用 MySQL 数据库存储 OAuth2 的数据。 6. 创建一个 UserController 类,用于测试: ``` @RestController @RequestMapping("/api") public class UserController { @GetMapping("/userinfo") public Principal user(Principal user) { return user; } } ``` 这个类提供了一个 /api/userinfo 接口,用于返回当前用户的信息。 7. 启动应用程序,并尝试访问 /api/userinfo 接口: ``` http://localhost:8080/api/userinfo ``` 如果没有登录,将重定向到登录页面。登录后,您将获得当前用户的信息。 以上就是一个简单的 Spring Boot 2 OAuth2 集成的示例,可以根据自己的需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值