Skip to content

Commit

Permalink
✨ 支持GitHub第三方授权
Browse files Browse the repository at this point in the history
  • Loading branch information
twelvet-s committed Jan 27, 2025
1 parent 40e2400 commit 74a2952
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import com.twelvet.auth.service.Oauth2AuthService;
import com.twelvet.framework.core.application.controller.TWTController;
import com.twelvet.framework.core.application.domain.JsonResult;
import com.twelvet.framework.core.locale.I18nUtils;
import com.twelvet.framework.core.locale.constants.LocaleSystemConstants;
import com.twelvet.framework.security.annotation.AuthIgnore;
import com.twelvet.framework.security.constants.Oauth2GrantEnums;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import me.zhyd.oauth.model.AuthCallback;
Expand Down Expand Up @@ -34,9 +37,9 @@ public class Oauth2AuthController extends TWTController {
* @return
*/
@Operation(summary = "获取登录地址")
@GetMapping
public JsonResult<String> getAuthorize() {
return JsonResult.success(oauth2AuthService.getAuthorize());
@GetMapping("/{oauthCode}")
public JsonResult<String> getAuthorize(@PathVariable String oauthCode) {
return JsonResult.success(oauthCode, oauth2AuthService.getAuthorize(oauthCode));
}

@Operation(summary = "测试回调")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.twelvet.auth.service;

import me.zhyd.oauth.model.AuthCallback;

/**
* <p>
* 第三方登录
Expand All @@ -13,8 +11,9 @@ public interface Oauth2AuthService {

/**
* 获取第三方授权地址
* @return String
* @param oauthCode 需要获取登录的第三方
* @return 返回登录地址
*/
String getAuthorize();
String getAuthorize(String oauthCode);

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.twelvet.auth.service.impl;

import com.twelvet.auth.service.Oauth2AuthService;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.model.AuthCallback;
import com.twelvet.framework.core.exception.TWTException;
import com.twelvet.framework.security.constants.Oauth2GrantEnums;
import me.zhyd.oauth.request.AuthGithubRequest;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.utils.AuthStateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand All @@ -23,11 +22,15 @@ public class Oauth2AuthServiceImpl implements Oauth2AuthService {
private AuthGithubRequest authGithubRequest;

/**
* 第三方授权地址
* @return 第三方授权地址
* 获取第三方授权地址
* @param oauthCode 需要获取登录的第三方
* @return 返回登录地址
*/
public String getAuthorize() {
return authGithubRequest.authorize(AuthStateUtils.createState());
public String getAuthorize(String oauthCode) {
if (Oauth2GrantEnums.GITHUB.getGrant().equals(oauthCode)) {
return authGithubRequest.authorize(AuthStateUtils.createState());
}
throw new TWTException("不存在此第三方登录授权方式");
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.twelvet.auth.config;

import com.twelvet.auth.config.properties.Oauth2LoginProperties;
import com.xkcoding.http.config.HttpConfig;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.request.AuthGithubRequest;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.InetSocketAddress;
import java.net.Proxy;

/**
* 第三方登录配置
*
Expand All @@ -28,6 +32,10 @@ public AuthGithubRequest authGithubRequest(Oauth2LoginProperties oauth2LoginProp
.clientId(oauth2LoginProperties.getGithub().getClientId())
.clientSecret(oauth2LoginProperties.getGithub().getClientSecret())
.redirectUri(oauth2LoginProperties.getGithub().getRedirectUri())
.httpConfig(HttpConfig.builder()
.timeout(15000)
.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7890)))
.build())
.build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,16 @@
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.request.AuthGithubRequest;
import me.zhyd.oauth.request.AuthRequest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.core.Ordered;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.SpringSecurityMessageSource;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.core.AuthorizationGrantType;
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
Expand Down Expand Up @@ -82,13 +77,6 @@ public Authentication buildAuthenticationToken(Authentication authentication) {
String clientId = oAuth2ResourceOwnerPasswordAuthenticationToken.getClientPrincipal().getName();
String grantType = oAuth2ResourceOwnerPasswordAuthenticationToken.getAuthorizationGrantType().getValue();

// 设置代理
/*
* System.setProperty("http.proxyHost", "127.0.0.1");
* System.setProperty("http.proxyPort", "7890");
* System.setProperty("https.proxyHost", "127.0.0.1");
* System.setProperty("https.proxyPort", "7890");
*/
// 获取第三方登录信息
AuthCallback authCallback = AuthCallback.builder().code(code).state(state).build();
AuthResponse<AuthUser> authUserAuthResponse = authRequest.login(authCallback);
Expand All @@ -106,7 +94,8 @@ public Authentication buildAuthenticationToken(Authentication authentication) {

try {
// GitHub唯一用户ID进行绑定登录
UserDetails userDetails = optional.get().loadUserByOAuth2Id(Oauth2GrantEnums.GITHUB, authUser.getUuid());
UserDetails userDetails = optional.get()
.loadUserByOAuth2UserId(Oauth2GrantEnums.GITHUB, authUser.getUuid());
if (Objects.isNull(userDetails)) {
log.debug("Failed to authenticate since no credentials provided");
throw new BadCredentialsException(I18nUtils
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import cn.hutool.core.collection.CollUtil;
import com.twelvet.api.system.domain.SysUser;
import com.twelvet.api.system.model.UserInfo;
import com.twelvet.framework.core.domain.R;
import com.twelvet.framework.core.constants.SecurityConstants;
import com.twelvet.framework.core.domain.R;
import com.twelvet.framework.security.constants.Oauth2GrantEnums;
import com.twelvet.framework.security.domain.LoginUser;
import com.twelvet.framework.utils.TUtils;
import org.springframework.core.Ordered;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
Expand Down Expand Up @@ -56,11 +55,11 @@ default UserDetails loadUserByPhone(String phone) throws UsernameNotFoundExcepti
/**
* 根据第三方唯一ID进行获取登录
* @param oauth2GrantEnums 枚举第三方平台
* @param OAuth2Id 第三方唯一ID
* @param oAuth2UserId 第三方唯一ID
* @return UserDetails
* @throws UsernameNotFoundException UsernameNotFoundException
*/
default UserDetails loadUserByOAuth2Id(Oauth2GrantEnums oauth2GrantEnums, String OAuth2Id)
default UserDetails loadUserByOAuth2UserId(Oauth2GrantEnums oauth2GrantEnums, String oAuth2UserId)
throws UsernameNotFoundException {
return null;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.springframework.context.annotation.Primary;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.oauth2.core.AuthorizationGrantType;

/**
* @author twelvet
Expand Down Expand Up @@ -72,15 +71,15 @@ public UserDetails loadUserByUsername(String username) {
/**
* 根据第三方唯一ID进行获取登录
* @param oauth2GrantEnums 枚举第三方平台
* @param OAuth2Id 第三方唯一ID
* @param oAuth2UserId 第三方唯一ID
* @return UserDetails
* @throws UsernameNotFoundException UsernameNotFoundException
*/
@Override
public UserDetails loadUserByOAuth2Id(Oauth2GrantEnums oauth2GrantEnums, String OAuth2Id)
public UserDetails loadUserByOAuth2UserId(Oauth2GrantEnums oauth2GrantEnums, String oAuth2UserId)
throws UsernameNotFoundException {
if (Oauth2GrantEnums.GITHUB.equals(oauth2GrantEnums)) { // GitHub
return loadUserByUsername(OAuth2Id);
return loadUserByUsername(oAuth2UserId);
}
log.info("Oauth2GrantEnums:{} 不存在.", oauth2GrantEnums);
throw new UsernameNotFoundException("错误的登录类型");
Expand Down

0 comments on commit 74a2952

Please sign in to comment.