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

最终展示效果:

相关推荐
Coder_Boy_4 分钟前
Deeplearning4j+ Spring Boot 电商用户复购预测案例
java·人工智能·spring boot·后端·spring
Victory_orsh6 分钟前
AI雇佣人类,智能奴役肉体
后端
金牌归来发现妻女流落街头16 分钟前
【Springboot基础开发】
java·spring boot·后端
李日灐1 小时前
C++进阶必备:红黑树从 0 到 1: 手撕底层,带你搞懂平衡二叉树的平衡逻辑与黑高检验
开发语言·数据结构·c++·后端·面试·红黑树·自平衡二叉搜索树
是梦终空1 小时前
计算机毕业设计264—基于Springboot+Vue3+协同过滤的房屋租赁管理系统(源代码+数据库+万字论文+设计文档)
spring boot·毕业设计·vue3·课程设计·毕业论文·协同过滤·房屋租赁管理系统
qq_297574671 小时前
【实战】POI 实现 Excel 多级表头导出(含合并单元格完整方案)
java·spring boot·后端·excel
郝学胜-神的一滴2 小时前
超越Spring的Summer(一): PackageScanner 类实现原理详解
java·服务器·开发语言·后端·spring·软件构建
Tony Bai2 小时前
“Go 2,请不要发生!”:如果 Go 变成了“缝合怪”,你还会爱它吗?
开发语言·后端·golang
Victor3562 小时前
Hibernate(91)如何在数据库回归测试中使用Hibernate?
后端
Victor3562 小时前
MongoDB(1)什么是MongoDB?
后端