Springboot 整合JustAuth实现gitee授权登录

使用JustAuth 开始集成第三方应用授权登录,以下以gitee 为例实现

项目依赖

xml 复制代码
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

   <dependency>
            <groupId>me.zhyd.oauth</groupId>
            <artifactId>JustAuth</artifactId>
            <version>1.16.7</version>
      </dependency>

        <!-- 如果你需要处理JSON,可添加fastjson依赖(可选) -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>2.0.45</version>
        </dependency>

Gitee 创建应用获取clientId和clientSecret 并填写回调地址

首先你需要去 Gitee 开放平台创建应用,获取关键配置:

访问Gitee 第三方应用管理,登录后点击创建应用。

填写应用名称、应用描述,重定向 URI要和你代码中配置的redirectUri完全一致(这是授权后 Gitee 回调的地址)。

创建完成后,你会得到Client ID和Client Secret,这两个是核心配置。

实现代码

java 复制代码
package com.learn.controller;


import com.learn.result.ResData;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import me.zhyd.oauth.AuthRequestBuilder;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.utils.AuthStateUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
public class AuthController {
    @GetMapping("/login")
    public ResData<?> login(HttpServletResponse response) throws IOException {
        AuthRequest authRequest = getAuthRequest();
        // 2. 生成授权地址(前端跳转这个地址进行Gitee登录授权)
        String authorizeUrl = authRequest.authorize(AuthStateUtils.createState());
        return ResData.ok("操作成功", authorizeUrl);
    }

    @GetMapping(value = "/lgb")
    public ResData<?> lgb(HttpServletRequest request, AuthCallback callback) {
//        String code = request.getParameter("code");
//        String state = request.getParameter("state");
//        AuthCallback callback = AuthCallback.builder()
//                .code(code)
//                .state(state)
//                .build();
        AuthRequest authRequest = getAuthRequest();
        AuthResponse<AuthUser> login = authRequest.login(callback);
        AuthUser authUser = login.getData();
        return ResData.ok(authUser);
    }

    private AuthRequest getAuthRequest() {
        return AuthRequestBuilder.builder()
                .source("gitee")
                .authConfig((source) -> {
                    // 通过 source 动态获取 AuthConfig
                    // 此处可以灵活的从 sql 中取配置也可以从配置文件中取配置
                    return AuthConfig.builder()
                            .clientId("67a73a6ebd5f659e365eab214ddc516ebd8580b1a61c390127350eb9119da6a0")
                            .clientSecret("84b06497a1d0c113b99553c04ab6d116337f5490f2fcfa573f84065346920d87")
                            .redirectUri("http://me64995b.natappfree.cc/lgb")
                            .build();
                })
                .build();
    }
}

这里需要注意的是默认和返回xml文件信息给您 那您可能需要配置返回JSON 两种方式解决
方式1:

java 复制代码
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 添加对 favicon.ico 的处理,避免 NoResourceFoundException 错误
        registry.addResourceHandler("/favicon.ico")
                .addResourceLocations("classpath:/static/favicon.ico");
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 清空默认的所有消息转换器
        converters.clear();
        // 只添加JSON转换器(使用Jackson处理JSON)
        converters.add(new MappingJackson2HttpMessageConverter());
    }
}

方式2:

java 复制代码
 // 2. 回调接口:强制返回JSON
    @GetMapping(value = "/gitee/callback", produces = MediaType.APPLICATION_JSON_VALUE)
    public AuthResponse giteeCallback(String code, String state) {
        // 原代码...
    }

最终展示效果:

相关推荐
Tom·Ge20 分钟前
【Spring Boot 4】Spring Boot 4.0 AI Agent实战:Skills Agent + MCP协议集成
人工智能·spring boot·microsoft
Devin~Y23 分钟前
电商场景下的Java面试实战:从Spring Boot微服务到Kafka、Redis与AI RAG
java·spring boot·redis·elasticsearch·spring cloud·微服务·kafka
御坂嘀喵2 小时前
Speed Tools:一套低侵入的 Android 插件化 + 动态换肤 + 字体切换框架
android·gitee
IguoChan2 小时前
4. d2l — 模型选择、欠拟合和过拟合
后端
shepherd1262 小时前
一次把 Spring MVC 文件上传参数“查没了”的排查:multipart、Filter 与 request body 的连环坑
java·后端·spring·mvc
宠友信息2 小时前
Spring Boot与异步审核构建仿小红书源码内容发布全流程
java·数据库·spring boot·redis·mysql·oracle·uni-app
贺国亚2 小时前
Prompt-Injection攻防与OWASP-LLM
后端
Listen·Rain2 小时前
Springboot整合RedisStack
java·spring boot·redis
杨运交2 小时前
[046][Crypto模块]Spring Boot 自动配置进阶:按需装配加解密处理器
spring boot
卷无止境2 小时前
《Deep Analysis with Polars》进阶分析技巧——深入解读
后端·python