Spring Security OAuth2——自定义OAuth2第三方登录(Gitee)

万俟靖
2023-12-01

官方文档

https://docs.spring.io/spring-security/site/docs/current/reference/html5/#oauth2login-custom-provider-properties

Gitee OAuth2 文档

https://gitee.com/api/v5/oauth_doc#/

解决方案

application.yml

spring:
  # Security Config
  security:
    oauth2:
      client:
        registration:
          gitee:
            provider: gitee
            client-id: {mm}
            client-secret: {mm}
            authorization-grant-type: authorization_code
            redirect-uri: '{baseUrl}/login/oauth2/code/{registrationId}'
            scope: user_info
            client-name: Gitee
            client-alias: gitee
        provider:
          gitee:
            authorization-uri: https://gitee.com/oauth/authorize
            token-uri: https://gitee.com/oauth/token
            user-name-attribute: id
            user-info-uri: https://gitee.com/api/v5/user

 WebSecurityConfigurerAdapter

    @Override
    protected void configure(HttpSecurity http)throws Exception{

            // OAuth2登录
            http.oauth2Login()
                    .redirectionEndpoint()
                    .baseUri("/login/oauth2/code/*")
                .and()
                    .userInfoEndpoint()
                    .customUserType(GiteeOAuth2User.class,"gitee")
                .and()
                .permitAll()
    }

 GiteeOAuth2User

package com.hailiu.entity;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.core.user.OAuth2User;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.Map;

/**
 * @author ShenTuZhiGang
 * @version 1.0.0
 * @date 2021-03-16 01:31
 */
@Data
@EqualsAndHashCode(callSuper = false)
@ApiModel(value = "Gitee OAuth2用户",description = "Gitee OAuth2用户")
public class GiteeOAuth2User implements OAuth2User, Serializable {

    private static final long serialVersionUID = 1L;

    private Integer id;
    private String login;
    private String name;
    @JsonProperty("avatar_url")
    private String avatarUrl;
    private String url;
    @JsonProperty("html_url")
    private String htmlUrl;
    @JsonProperty("followers_url")
    private String followersUrl;
    @JsonProperty("following_url")
    private String followingUrl;
    @JsonProperty("gists_url")
    private String gistsUrl;
    @JsonProperty("starred_url")
    private String starredUrl;
    @JsonProperty("subscriptions_url")
    private String subscriptionsUrl;
    @JsonProperty("organizations_url")
    private String organizationsUrl;
    @JsonProperty("repos_url")
    private String reposUrl;
    @JsonProperty("events_url")
    private String eventsUrl;
    @JsonProperty("received_events_url")
    private String receivedEventsUrl;
    private String type;
    private String blog;
    private String weibo;
    private String bio;
    @JsonProperty("public_repos")
    private Integer publicRepos;
    @JsonProperty("public_gists")
    private Integer publicGists;
    private Integer followers;
    private Integer following;
    private Integer stared;
    private Integer watched;
    @JsonProperty("created_at")
    private Date createdAt;
    @JsonProperty("updated_at")
    private Date updatedAt;
    private String email;
    @Override
    public String getName() {
        return name;
    }

    @Override
    public Map<String, Object> getAttributes() {
        return null;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }
}

参考文章

Web三方登录实现(基于OAuth2.0,包含Github和QQ登录,附源码)

 类似资料: