活动地址:CSDN21天学习挑战赛
前面已经介绍了【Spring Security OAuth实现GitHub登录】,但由于GitHub会因网络原因,无法范围,因此该文将介绍如何使用国内的Gitee进行集成登录。
源代码地址:https://github.com/jujunchen/21Study Gitee 文档地址:https://gitee.com/api/v5/oauth_doc#/
新创建一个Spring Boot 工程,pom依赖如下
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--单元测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
注意: 必须引入spring-boot-starter-oauth2-client
依赖
进入Gitee->设置->数据管理->第三方应用
注册界面如下:
填写内容同GitHub
创建应用后,生成Client ID和Client Secret
接下来在配置文件中增加对于的配置
spring:
security:
oauth2:
client:
registration:
gitee:
client-id: gitee-client-id
client-secret: gitee-client-secret
authorization-grant-type: authorization_code
redirect-uri: '{baseUrl}/login/oauth2/code/{registrationId}'
client-name: Gitee
github:
client-id: b4713d47174917b34c28
client-secret: 898389369c2e9f3d1d0ff4543ba1d9b45adfd093
provider:
gitee:
authorization-uri: https://gitee.com/oauth/authorize
token-uri: https://gitee.com/oauth/token
user-info-uri: https://gitee.com/api/v5/user
user-name-attribute: name
其中: (1)client_id、client-secret替换为Gitee获取的数据
(2)authorization-grant-type:授权模式使用授权码模式
(3)redirect-uri:回调地址,填写的与Gitee上申请的一致
(4)client-name:客户端名称,可以在登录选择页面上显示
Gitee的OAuth登录需要自定义provider,Spring Security OAuth提供了配置的方式来实现。
(5)authorization-uri:授权服务器地址
(6)token-uri:授权服务器获取token地址
(7)user-info-uri:授权服务器获取用户信息的地址
(8)user-name-attribute:用户信息中的用户名属性
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(Principal principal) {
return "Hello," + principal.getName();
}
}
principal对象由Spring框架自动注入,表示当前登录的用户。
Spring Security OAuth 使用OAuth2ClientAutoConfiguration 来完成自动配置,也可以通过如下步骤覆盖自动配置:
@Configuration
public class OAuth2LoginConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(authorize -> authorize
.anyRequest().authenticated()
)
.oauth2Login(withDefaults());
return http.build();
}
@Bean
public ClientRegistrationRepository clientRegistrationRepository() {
return new InMemoryClientRegistrationRepository(this.giteeClientRegistration());
}
private ClientRegistration giteeClientRegistration() {
return ClientRegistration.withRegistrationId("gitee")
.clientId("gitee-client-id")
.clientSecret("gitee-client-secret")
.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
.redirectUri("{baseUrl}/login/oauth2/code/{registrationId}")
.authorizationUri("https://gitee.com/oauth/authorize")
.tokenUri("https://gitee.com/oauth/token")
.userInfoUri("https://gitee.com/api/v5/user")
.userNameAttributeName("name")
.clientName("Gitee")
.build();
}
}
效果: