使用第三方开源库JustAuth登录案例

分享一波第三方登录的案例

案例文档

我这里使用支付宝登录(其他的基本一样)

步骤

1.导入依赖


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.1.RELEASE
         
    
    com.huacheng
    demo
    0.0.1-SNAPSHOT
    demo
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
        
            me.zhyd.oauth
            JustAuth
            1.15.6
        

        
        
            com.alipay.sdk
            alipay-sdk-java
            3.7.4.ALL
        
    



2.去文档看《支付宝登录》(截图如下),按着步骤来,可能有点复杂,耐心点,我就是傻瓜式搞的

使用第三方开源库JustAuth登录案例_第1张图片

主要记录下这四个东西

使用第三方开源库JustAuth登录案例_第2张图片

3.新建一个配置类去放那四个东西(这一步可省略,我装逼就写了,没有可以直接复制来就得了)
package com.huacheng.config;


import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

@Data
@ConfigurationProperties(prefix = "hc.oa")
public class AuthAlipayProperties {

    private String clientId;
    private String clientSecret;
    private String alipayPublicKey;
    private String redirectUri;
}
4.application.yml配置文件内容
server:
  port: 8443


hc:
  oa:
    #APPID
    clientId: 你申请的APPID
    #应用私钥
    clientSecret: 你申请的应用私钥
    #支付宝公钥 
    alipayPublicKey: 你申请的支付宝公钥

    #授权回调地址(我随便写了个地址)
    redirectUri: https://www.jianshu.com/
5.在启动类引入配置类
@EnableConfigurationProperties({AuthAlipayProperties.class})
6.控制器测试
package com.huacheng.controller;


import com.huacheng.config.AuthAlipayProperties;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.request.AuthAlipayRequest;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.utils.AuthStateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/oauth")
public class AuthAlipayController {

    //配置文件
    @Autowired
    private AuthAlipayProperties prop;

    @RequestMapping("/render")
    public void renderAuth(HttpServletResponse response) throws IOException {
        AuthRequest authRequest = getAuthRequest();
        response.sendRedirect(authRequest.authorize(AuthStateUtils.createState()));
    }

    @RequestMapping("/callback")
    public Object login(AuthCallback callback) {
        AuthRequest authRequest = getAuthRequest();
        return authRequest.login(callback);
    }

    private AuthRequest getAuthRequest() {
        return new AuthAlipayRequest(AuthConfig.builder()
                .clientId(prop.getClientId())
                .clientSecret(prop.getClientSecret())
                .alipayPublicKey(prop.getAlipayPublicKey())
                .redirectUri(prop.getRedirectUri())
                .build());
    }
}
7.浏览器访问地址

你可能感兴趣的:(springboot)