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) {
        // 原代码...
    }

最终展示效果:

相关推荐
Slow菜鸟15 小时前
Spring Cloud 教程(四) | OpenFeign 的作用
后端·spring·spring cloud
GetcharZp15 小时前
告别付费云盘!这款开源 AI 相册神器,颜值与实力双飞,满足你对私人云端的一切幻想!
后端
朦胧之15 小时前
AI 编程工具使用浅谈
前端·后端
架构谨制@涛哥15 小时前
《哥谭神话-Palantir故事篇》Palantir 产品战略与架构全景
后端·系统架构·软件构建
Flandern111115 小时前
Go程序员学习AI大模型项目实战02:给 AI 装上“大脑”:从配置解包到流式生成的深度拆解
人工智能·后端·python·学习·golang
历程里程碑16 小时前
1 . Git本地操作:版本控制 跨平台协作 仓库核心
java·开发语言·数据结构·c++·git·gitee·github
阿丰资源16 小时前
java项目(附资料)-基于SpringBoot+MyBatisPlus+MySQL+Layui的药品管理系统
java·spring boot·mysql
indexsunny16 小时前
互联网大厂Java面试实战:从Spring Boot到微服务架构的深度探讨
java·数据库·spring boot·安全·微服务·监控·面试实战
逸风尊者17 小时前
2026 主流 Claw 类产品技术报告
人工智能·后端·算法
宸津-代码粉碎机17 小时前
Spring Boot 4.0 实战技巧全解析
java·大数据·spring boot·后端·python