Аннотация EnableWebSecurity выдает ошибку в spring-security-oauth2 - PullRequest
0 голосов
/ 26 марта 2020

Я использую Spring Boot, включая Spring 2.1.2 Release Security, и использую KeyCloak Oauth2.0. Но когда я перезапускаю приложение, я получаю следующую ошибку:

Параметр 0 метода tokenRelayGatewayFilterFactory в org.springframework.cloud.security.oauth2.gateway.TokenRelayAutoConfiguration требует наличия bean-компонента типа 'org.springframework.security.oa2 .client.web.server.ServerOAuth2AuthorizedClientRepository ', который не найден.

    The following candidates were found but could not be injected:
  - Bean method 'authorizedClientRepository' in 'ReactiveOAuth2ClientAutoConfiguration' not loaded because NoneNestedConditions 1 matched 0 did not; NestedCondition on     ReactiveOAuth2ClientAutoConfiguration.NonServletApplicationCondition.ServletApplicationCondition found 'session' scope

 Action:

Consider revisiting the entries above or defining a bean of type 'org.springframework.security.oauth2.client.web.server.ServerOAuth2AuthorizedClientRepository' in your configuration.

Вы можете найти pom. xml, application.yml и SecurityConfig ранее. Проблема возникает, когда я расширяю WebSecurityConfigurerAdapter. Как вы думаете, я должен внести некоторые изменения в pom. xml или в сам класс? Спасибо за вашу помощь.

pom. xml

 <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-consul-discovery</artifactId>
    </dependency>
    <dependency>
        <groupId>de.siegmar</groupId>
        <artifactId>logback-gelf</artifactId>
        <version>1.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zipkin</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-oauth2-client</artifactId>
    </dependency>


    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.projectreactor</groupId>
        <artifactId>reactor-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>au.com.dius</groupId>
        <artifactId>pact-jvm-consumer-junit_2.11</artifactId>
        <version>3.5.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!--START: OAUTH2 Client for Authorization Code-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-oauth2-client</artifactId>
    </dependency>

    <!--Auto Configure Oauth Spring Security Stuff-->
    <dependency>
        <groupId>org.springframework.security.oauth.boot</groupId>
        <artifactId>spring-security-oauth2-autoconfigure</artifactId>
    </dependency>
    <!--END-->

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>

    <!--START: Thymeleaf configs, no need to add those if you are not using thymeleaf-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity5</artifactId>
    </dependency>
    <!--END-->

    <!--START: Eureka Client Config-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <!--END-->





</dependencies>

часть application.yaml

         routes:
            - id:myApp
              uri: http://localhost:5287
              predicates:
                  - Path=/keycloak-oidc-code/**
              filters:
                  - TokenRelay=
                  - RemoveRequestHeader=Cookie

SecurityConfig. java

@ Configuration @EnableWebSecurity publi c класс SecurityConfig. java extends WebSecurityConfigurerAdapter {

@Override
public void configure( HttpSecurity http ) throws Exception
{ //TODO
    http
        .authorizeRequests( )
        .anyRequest( ).authenticated( )
        .antMatchers( "/login**", "/error**" ).permitAll( ).and( )
        .oauth2Login( );
}

}

1 Ответ

1 голос
/ 27 марта 2020

Вы используете модули Reactive Spring (WebFlux, Spring-Cloud-Gateway). Итак, настройка безопасности не может быть традиционным способом. Вам необходимо настроить конфигурацию безопасности, как показано ниже:

@EnableWebFluxSecurity
public class MySecurityConfiguration {

    @Bean
    public SecurityWebFilterChain securitygWebFilterChain(ServerHttpSecurity http) {
        return http.authorizeExchange().pathMatchers("/login**", "/error**").permitAll()
    .anyExchange().authenticated().and().oauth2Login().and().build();;
    }

}
...