springboot接入gitee登录

时间: 2023-07-01 17:04:04 浏览: 75
要在Spring Boot应用程序中实现Gitee登录,你需要执行以下步骤: 1. 创建一个Gitee应用程序并获取客户端ID和客户端密钥。可以在Gitee的开发者中心创建应用程序。 2. 添加Spring Security依赖项。 3. 在Security Configuration中配置OAuth2客户端。 下面是一个简单的配置示例: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/login/**") .permitAll() .anyRequest() .authenticated() .and() .oauth2Login() .loginPage("/login") .defaultSuccessURL("/") .failureUrl("/login?error=true") .and() .csrf() .disable(); } @Bean public ClientRegistrationRepository clientRegistrationRepository() { return new InMemoryClientRegistrationRepository(giteeClientRegistration()); } private ClientRegistration giteeClientRegistration() { return ClientRegistration.withRegistrationId("gitee") .clientId("your-client-id") .clientSecret("your-client-secret") .clientAuthenticationMethod(ClientAuthenticationMethod.POST) .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE) .redirectUriTemplate("{baseUrl}/login/oauth2/code/{registrationId}") .scope("user_info") .authorizationUri("https://gitee.com/oauth/authorize") .tokenUri("https://gitee.com/oauth/token") .userInfoUri("https://gitee.com/api/v5/user") .userNameAttributeName(IdTokenClaimNames.SUB) .jwkSetUri("https://gitee.com/oauth/keys") .clientName("Gitee") .build(); } } ``` 在上面的代码中,我们使用Spring Security OAuth2客户端支持来配置Gitee登录。 在configure()方法中,我们通过.antMatchers()方法指定/login/**路径可公开访问,任何人都可以访问它。 我们还用.anyRequest().authenticated()确保所有其他请求都需要一个已经认证的用户。 在oauth2Login()方法中,我们设置了登录页面,成功的默认URL和错误URL。 最后,我们通过clientRegistrationRepository()方法将客户端注册添加到Spring Security中。 在giteeClientRegistration()方法中,我们使用ClientRegistration.withRegistrationId()方法创建一个新的客户端注册。我们设置客户端ID和密钥,并指定客户端认证方法。 我们还设置了授权类型,重定向URI模板,作用域,授权URI,令牌URI,用户信息URI,用户名属性名称,JWK集合URI和客户端名称。 为了使此配置生效,你需要在Spring Boot应用程序中提供以下属性: ``` spring.security.oauth2.client.registration.gitee.client-id=your-client-id spring.security.oauth2.client.registration.gitee.client-secret=your-client-secret ``` 这样,你就可以使用Gitee登录来保护你的Spring Boot应用程序了。

相关推荐

最新推荐

recommend-type

SpringBoot接口加密解密统一处理

主要为大家详细介绍了SpringBoot接口加密解密统一处理,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

SpringBoot实现接口数据的加解密功能

主要介绍了SpringBoot实现接口数据的加解密功能,对接口的加密解密操作主要有两种实现方式,文中给大家详细介绍,需要的朋友可以参考下
recommend-type

Springboot接口项目如何使用AOP记录日志

主要介绍了Springboot接口项目如何使用AOP记录日志,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

使用springboot结合vue实现sso单点登录

主要为大家详细介绍了如何使用springboot+vue实现sso单点登录,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

详解SpringBoot中Controller接收对象列表实现

主要介绍了详解SpringBoot中Controller接收对象列表实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。