首页>代码>Spring Boot整合Java开源OAuth库ScribeJava实现第三方用户登录授权实例,支持QQ,微信,微博等平台>/scribejava-oauth2/src/main/java/com/hellokoding/springboot/oauth2/OAuth2ServiceFactory.java
package com.hellokoding.springboot.oauth2;

import com.github.scribejava.core.builder.ServiceBuilder;
import com.github.scribejava.core.builder.api.DefaultApi20;
import com.github.scribejava.core.extractors.OAuth2AccessTokenExtractor;
import com.github.scribejava.core.extractors.OAuth2AccessTokenJsonExtractor;
import com.github.scribejava.core.extractors.TokenExtractor;
import com.github.scribejava.core.model.OAuth2AccessToken;
import com.github.scribejava.core.oauth.OAuth20Service;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@RequiredArgsConstructor
@Component
public class OAuth2ServiceFactory {
    private static Map<String, OAuth20Service> services = new HashMap<>();
    private final OAuth2Properties oAuth2Properties;

    public OAuth20Service getService(String serviceId) {
        if (services.containsKey(serviceId))
            return services.get(serviceId);

        OAuth2Properties.Registration registration = oAuth2Properties.getRegistration().get(serviceId);
        OAuth2Properties.Provider provider = oAuth2Properties.getProvider().get(serviceId);

        OAuth20Service oAuth20Service = new ServiceBuilder(registration.getClientId())
            .apiSecret(registration.getClientSecret())
            .callback(registration.getRedirectUri())
            .defaultScope(registration.getScope())
            .responseType(registration.getAuthorizationGrantType())
            .userAgent("HelloKoding")
            .build(new OAuth2Api(provider));
        services.put(serviceId, oAuth20Service);

        return oAuth20Service;
    }

    @RequiredArgsConstructor
    class OAuth2Api extends DefaultApi20 {
        private final OAuth2Properties.Provider provider;

        @Override
        public String getAccessTokenEndpoint() {
            return provider.getTokenUri();
        }

        @Override
        public String getAuthorizationBaseUrl() {
            return provider.getAuthorizationUri();
        }

        @Override
        public String getRevokeTokenEndpoint() {
            return provider.getRevokeTokenUri();
        }

        @Override
        public TokenExtractor<OAuth2AccessToken> getAccessTokenExtractor() {
            if ("github".equalsIgnoreCase(provider.getName()))
                return OAuth2AccessTokenExtractor.instance();

            return OAuth2AccessTokenJsonExtractor.instance();
        }

        public String getUserInfoEndpoint() {
            return provider.getUserInfoUri();
        }

        public String getUserNameAttribute() {
            return provider.getUserNameAttribute();
        }

        public String getRevokePermissionEndpoint() {
            return provider.getRevokePermissionUri();
        }
    }
}
最近下载更多
dfsyke  LV13 2020年11月1日
醉心客  LV5 2020年10月23日
最代码官方  LV168 2020年10月23日
最近浏览更多
sjwc88  LV4 2024年12月15日
15578157792  LV7 2024年12月6日
哪里的完整版  LV8 2024年9月20日
dapeng0011  LV15 2024年8月25日
唐僧洗头爱飘柔  LV22 2024年6月21日
叁卍石  LV5 2024年3月20日
kaye7549317 2024年1月16日
暂无贡献等级
mukoooo  LV2 2023年12月5日
hhsoft  LV1 2023年10月22日
601601lmy  LV5 2023年7月12日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友