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

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

There is a newer version: 3.3.4
Show newest version
/**
 * 
 */
package club.zhcs.lina.starter.oidc;

import java.net.MalformedURLException;
import java.net.URL;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

import club.zhcs.lina.auth.jwt.JWTGenerator;
import club.zhcs.lina.auth.service.AuthService;
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.OpenIDConnectService;
import club.zhcs.lina.oidc.service.TokenRefreshableRiemannAuthService;
import club.zhcs.lina.starter.oidc.OpenIDConnectConfigurationProperties.Type;
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
    AuthService tokenRefreshableRiemannAuthService(JWTGenerator jwtGenerator, HttpServletRequest request, HttpServletResponse response) {
        URL url;
        try {
            url = new URL(config.getDomain());
        }
        catch (MalformedURLException e) {
            throw Lang.wrapThrow(e);
        }
        return new TokenRefreshableRiemannAuthService(jwtGenerator,
                                                      url.getHost(),
                                                      config.getTokenCookieName(),
                                                      config.getRefreshTokenCookieName(),
                                                      config.getAuthorizationHeaderName(),
                                                      request,
                                                      response);
    }

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

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

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy