diff --git a/twelvet-auth/src/main/java/com/twelvet/auth/controller/Oauth2AuthController.java b/twelvet-auth/src/main/java/com/twelvet/auth/controller/Oauth2AuthController.java index 2c845280..7d528f02 100644 --- a/twelvet-auth/src/main/java/com/twelvet/auth/controller/Oauth2AuthController.java +++ b/twelvet-auth/src/main/java/com/twelvet/auth/controller/Oauth2AuthController.java @@ -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; @@ -34,9 +37,9 @@ public class Oauth2AuthController extends TWTController { * @return */ @Operation(summary = "获取登录地址") - @GetMapping - public JsonResult getAuthorize() { - return JsonResult.success(oauth2AuthService.getAuthorize()); + @GetMapping("/{oauthCode}") + public JsonResult getAuthorize(@PathVariable String oauthCode) { + return JsonResult.success(oauthCode, oauth2AuthService.getAuthorize(oauthCode)); } @Operation(summary = "测试回调") diff --git a/twelvet-auth/src/main/java/com/twelvet/auth/service/Oauth2AuthService.java b/twelvet-auth/src/main/java/com/twelvet/auth/service/Oauth2AuthService.java index 056000b7..2f2f2e48 100644 --- a/twelvet-auth/src/main/java/com/twelvet/auth/service/Oauth2AuthService.java +++ b/twelvet-auth/src/main/java/com/twelvet/auth/service/Oauth2AuthService.java @@ -1,7 +1,5 @@ package com.twelvet.auth.service; -import me.zhyd.oauth.model.AuthCallback; - /** *

* 第三方登录 @@ -13,8 +11,9 @@ public interface Oauth2AuthService { /** * 获取第三方授权地址 - * @return String + * @param oauthCode 需要获取登录的第三方 + * @return 返回登录地址 */ - String getAuthorize(); + String getAuthorize(String oauthCode); } diff --git a/twelvet-auth/src/main/java/com/twelvet/auth/service/impl/Oauth2AuthServiceImpl.java b/twelvet-auth/src/main/java/com/twelvet/auth/service/impl/Oauth2AuthServiceImpl.java index ce70e34e..7455a7a0 100644 --- a/twelvet-auth/src/main/java/com/twelvet/auth/service/impl/Oauth2AuthServiceImpl.java +++ b/twelvet-auth/src/main/java/com/twelvet/auth/service/impl/Oauth2AuthServiceImpl.java @@ -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; @@ -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("不存在此第三方登录授权方式"); } } diff --git a/twelvet/twelvet-framework/twelvet-framework-security/src/main/java/com/twelvet/auth/config/Oauth2LoginConfiguration.java b/twelvet/twelvet-framework/twelvet-framework-security/src/main/java/com/twelvet/auth/config/Oauth2LoginConfiguration.java index 85e2feda..027166e2 100644 --- a/twelvet/twelvet-framework/twelvet-framework-security/src/main/java/com/twelvet/auth/config/Oauth2LoginConfiguration.java +++ b/twelvet/twelvet-framework/twelvet-framework-security/src/main/java/com/twelvet/auth/config/Oauth2LoginConfiguration.java @@ -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; + /** * 第三方登录配置 * @@ -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()); } diff --git a/twelvet/twelvet-framework/twelvet-framework-security/src/main/java/com/twelvet/framework/security/support/grant/oauth2/github/OAuth2ResourceOwnerGiHubAuthenticationProvider.java b/twelvet/twelvet-framework/twelvet-framework-security/src/main/java/com/twelvet/framework/security/support/grant/oauth2/github/OAuth2ResourceOwnerGiHubAuthenticationProvider.java index 539c002e..15c605d3 100644 --- a/twelvet/twelvet-framework/twelvet-framework-security/src/main/java/com/twelvet/framework/security/support/grant/oauth2/github/OAuth2ResourceOwnerGiHubAuthenticationProvider.java +++ b/twelvet/twelvet-framework/twelvet-framework-security/src/main/java/com/twelvet/framework/security/support/grant/oauth2/github/OAuth2ResourceOwnerGiHubAuthenticationProvider.java @@ -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; @@ -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 authUserAuthResponse = authRequest.login(authCallback); @@ -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 diff --git a/twelvet/twelvet-framework/twelvet-framework-security/src/main/java/com/twelvet/framework/security/support/service/TwUserDetailsService.java b/twelvet/twelvet-framework/twelvet-framework-security/src/main/java/com/twelvet/framework/security/support/service/TwUserDetailsService.java index e7b09983..3a3cc1eb 100644 --- a/twelvet/twelvet-framework/twelvet-framework-security/src/main/java/com/twelvet/framework/security/support/service/TwUserDetailsService.java +++ b/twelvet/twelvet-framework/twelvet-framework-security/src/main/java/com/twelvet/framework/security/support/service/TwUserDetailsService.java @@ -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; @@ -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; }; diff --git a/twelvet/twelvet-framework/twelvet-framework-security/src/main/java/com/twelvet/framework/security/support/service/manager/TwTUserDetailsServiceImpl.java b/twelvet/twelvet-framework/twelvet-framework-security/src/main/java/com/twelvet/framework/security/support/service/manager/TwTUserDetailsServiceImpl.java index a0558373..49891b1e 100644 --- a/twelvet/twelvet-framework/twelvet-framework-security/src/main/java/com/twelvet/framework/security/support/service/manager/TwTUserDetailsServiceImpl.java +++ b/twelvet/twelvet-framework/twelvet-framework-security/src/main/java/com/twelvet/framework/security/support/service/manager/TwTUserDetailsServiceImpl.java @@ -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 @@ -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("错误的登录类型");