All Downloads are FREE. Search and download functionalities are using the official Maven repository.

club.zhcs.lina.starter.oidc.OpenIDConnectAutoConfiguration Maven / Gradle / Ivy

/**
 * 
 */
package club.zhcs.lina.starter.oidc;

import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;

import club.zhcs.lina.auth.service.AuthService;
import club.zhcs.lina.auth.service.UserDetailService;
import club.zhcs.lina.oidc.OpenIDConnectController;
import club.zhcs.lina.oidc.callback.CallbackHanlder;
import club.zhcs.lina.oidc.callback.CookieCallbackHanlder;
import club.zhcs.lina.oidc.callback.RedirctToFrontCallbackHanlder;
import club.zhcs.lina.oidc.service.CacheableOpenidConnectionAuthService;
import club.zhcs.lina.oidc.service.OpenIDConnectService;
import club.zhcs.lina.oidc.service.jwt.JwtDecoder;
import club.zhcs.lina.oidc.service.user.IAMUserDetailService;
import club.zhcs.lina.starter.oidc.OpenIDConnectConfigurationProperties.Type;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;

/**
 * 
 */
@AutoConfiguration
@RequiredArgsConstructor
@ConditionalOnExpression("${lina.oidc.enabled:false}")
@EnableConfigurationProperties(OpenIDConnectConfigurationProperties.class)
public class OpenIDConnectAutoConfiguration {

    private final OpenIDConnectConfigurationProperties config;

    @Bean
    @Lazy
    OpenIDConnectService openIDConnectService() {
        return new OpenIDConnectService(config.getDiscoveryUrl(), config.getClientId(), config.getClientSecret(), config.getRedirectUrl());
    }

    @Bean
    @Lazy
    @ConditionalOnMissingBean
    JwtDecoder riemannJwtDecoder(OpenIDConnectService openIDConnectService) {
        return openIDConnectService.createDecoder();
    }

    @Bean
    @Lazy
    @ConditionalOnMissingBean
    AuthService cacheableOpenidConnectionAuthService(JwtDecoder jwtDecoder,
                                                     UserDetailService userDetailService,
                                                     HttpServletRequest request,
                                                     HttpServletResponse response) {
        return new CacheableOpenidConnectionAuthService(jwtDecoder, config.getCookieSettings(), request, response, userDetailService);
    }

    @Bean
    @ConditionalOnMissingBean
    UserDetailService userDetailService() {
        return new IAMUserDetailService(config.iam() + "/api/open/%s/user-info", config.getClientId());
    }

    @Bean
    CallbackHanlder callbackHandler(AuthService authService, OpenIDConnectService openIDConnectService) {
        if (config.getCallbackHanlderType() == Type.COOKIE) {
            return new CookieCallbackHanlder(config.getHanlderSuccessPage(), openIDConnectService, authService);
        }
        return new RedirctToFrontCallbackHanlder(config.getHanlderSuccessPage());
    }

    @Bean
    @Lazy
    OpenIDConnectController openIDConnectController(OpenIDConnectService openIDConnectService,
                                                    CallbackHanlder callbackHandler,
                                                    AuthService authService) {
        return new OpenIDConnectController(openIDConnectService, callbackHandler, authService);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy