Почему идентификатор клиента и секрет клиента не вводятся в OAuth2ClientProperties? - PullRequest
0 голосов
/ 06 мая 2020

У меня есть приложение с весенней загрузкой с Azure AD в качестве поставщика OAuth2. Вот мой application.yml файл:

server:
  port: 8080
  address: localhost
security:
  oauth2:
    client:
      registration:
        azure:
          client-id: XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
          client-secret: ?h?_XXXXXXXXXXXXXXXXXXXXXXXX
azure:
  cosmosdb:
    uri: https://myapp.documents.azure.com:443/
    key: ${COSMOSDB_KEY}
    database: Core
  activedirectory:
    tenant-id: ${TENANT_ID}
    user-group:
      allowed-group: user-group

Итак, как вы видите, я использую client-id и client-secret в открытом виде (не через переменные среды), но он все равно не работает.

Вот мой файл сборки gradle:

plugins {
    id 'org.springframework.boot' version '2.2.6.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
}

group = 'group'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
    jcenter()
}

ext {
    set('azureVersion', "2.2.0")
}

dependencies {
//    Web
    implementation 'org.modelmapper:modelmapper:2.3.7'
    implementation 'org.springframework.boot:spring-boot-starter-web'

//    Azure
    implementation 'com.microsoft.azure:azure-spring-boot-starter'
    implementation 'com.microsoft.azure:azure-cosmosdb-spring-boot-starter'
    implementation 'com.microsoft.azure:azure-active-directory-spring-boot-starter'

//    OpenAPI
    implementation 'org.springdoc:springdoc-openapi-ui:1.3.7'
    implementation 'org.springdoc:springdoc-openapi-webmvc-core:1.3.7'

//    Security
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'

//    Lombok
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testCompileOnly 'org.projectlombok:lombok'
    testAnnotationProcessor 'org.projectlombok:lombok'

//    Tests
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    testImplementation 'org.springframework.security:spring-security-test'
}

dependencyManagement {
    imports {
        mavenBom "com.microsoft.azure:azure-spring-boot-bom:${azureVersion}"
    }
}

test {
    useJUnitPlatform()
}

Моя конфигурация безопасности:

@Slf4j
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(WebSecurity web) {
        web
                .ignoring()
                .antMatchers("/webjars/**", "/favicon.ico");
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login")
                .failureUrl("/login-error")
                .permitAll()
                .and()
                .oauth2Client();
    }
}

Во время запуска я получаю следующую ошибку:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration': Unsatisfied dependency expressed through method 'setConfigurers' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.security.config.annotation.web.configuration.OAuth2ClientConfiguration$OAuth2ClientWebMvcSecurityConfiguration': Unsatisfied dependency expressed through method 'setClientRegistrationRepository' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'clientRegistrationRepository' defined in class path resource [org/springframework/boot/autoconfigure/security/oauth2/client/servlet/OAuth2ClientRegistrationRepositoryConfiguration.class]: Unsatisfied dependency expressed through method 'clientRegistrationRepository' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'spring.security.oauth2.client-org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Client id must not be empty.

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.security.config.annotation.web.configuration.OAuth2ClientConfiguration$OAuth2ClientWebMvcSecurityConfiguration': Unsatisfied dependency expressed through method 'setClientRegistrationRepository' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'clientRegistrationRepository' defined in class path resource [org/springframework/boot/autoconfigure/security/oauth2/client/servlet/OAuth2ClientRegistrationRepositoryConfiguration.class]: Unsatisfied dependency expressed through method 'clientRegistrationRepository' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'spring.security.oauth2.client-org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Client id must not be empty.

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'clientRegistrationRepository' defined in class path resource [org/springframework/boot/autoconfigure/security/oauth2/client/servlet/OAuth2ClientRegistrationRepositoryConfiguration.class]: Unsatisfied dependency expressed through method 'clientRegistrationRepository' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'spring.security.oauth2.client-org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Client id must not be empty.

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'spring.security.oauth2.client-org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Client id must not be empty.

Caused by: java.lang.IllegalStateException: Client id must not be empty.

What я здесь скучаю?

1 Ответ

1 голос
/ 06 мая 2020

Я забыл добавить префикс spring. к свойствам security. Это должно выглядеть так:

security:
  oauth2:
    client:
      registration:
        azure:
          client-id: XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
          client-secret: ?h?_XXXXXXXXXXXXXXXXXXXXXXXX

Также мои свойства azure также неверны: вместо allowed-group мне нужно использовать allowed-groups.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...