baoshiwei
2025-04-19 9d960ed0058f9087f49e9741a9af06c3f9116eb0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.zhitan.common.utils;
 
import cn.hutool.core.util.ObjectUtil;
import com.zhitan.common.config.keycloak.AuthKeycloakRequest;
import com.zhitan.common.config.keycloak.AuthRedisStateCache;
import com.zhitan.common.config.keycloak.SocialLoginConfigProperties;
import com.zhitan.common.utils.spring.SpringUtils;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.request.AuthDingTalkRequest;
import me.zhyd.oauth.request.AuthRequest;
 
 
/**
 * 认证授权工具类
 *
 * @author thiszhc
 */
public class SocialUtils {
 
    private static final AuthRedisStateCache STATE_CACHE = SpringUtils.getBean(AuthRedisStateCache.class);
    private static final SocialLoginConfigProperties obj = SpringUtils.getBean(SocialLoginConfigProperties.class);
    @SuppressWarnings("unchecked")
    public static AuthResponse<AuthUser> loginAuth( String code, String state) throws AuthException {
        AuthRequest authRequest = getAuthKeyloakRequest();
        AuthCallback callback = new AuthCallback();
        callback.setCode(code);
        callback.setState(state);
        return authRequest.login(callback);
    }
 
 
 
    public static AuthKeycloakRequest getAuthKeyloakRequest( ) {
 
        AuthConfig.AuthConfigBuilder builder = AuthConfig.builder()
            .clientId(obj.getClientId())
            .clientSecret(obj.getClientSecret())
            .redirectUri(obj.getRedirectUri())
            .scopes(obj.getScopes());
        return new AuthKeycloakRequest(builder.build(), STATE_CACHE);
    }
}