как избежать применения Token для страниц без TokenRelay при весенней безопасности - PullRequest
0 голосов
/ 28 марта 2020

Я использую Spring Cloud Starter Security и Spring Boot Starter Ouath2 Client в своем приложении Reactive и выполнил мои настройки в application.yml, как вы увидите ниже.

Он отлично применяет Token для страниц, на которых установлен TokenRelay но проблема в Я должен настроить, чтобы он не применял токен для страниц, у которых нет TokenRelay . Как я могу добиться успеха? Не могли бы вы помочь мне выяснить эту проблему? Вы можете найти мои коды ниже.

pom. xml

 <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>

application.yml

 routes:
    - id: securedApp
      uri: http://localhost:51687
      predicates:
        - Path=/keycloak-oidc-code/**
      filters:
        - TokenRelay=
        - RemoveRequestHeader=Cookie
  filter:
    # Removes Expect Header that send to the services
    remove-hop-by-hop:
      headers:
        - expect
security:
  basic:
    enable: false
  ignored: /**
oauth2:
  client:
    registration:
      sample-authorization-code:
        id: MyApplication
        client-id: MyApplication
        client-secret:f607d7c5-991d-4605-843f-330f419ed143
        client-name: SecondApplication
        provider: keycloak
        redirect-uri: '{baseUrl}/login/oauth2/code/{registrationId}'
        authorization-grant-type: authorization_code
        client-authentication-method: post
        scope: openid, address, fun
    provider:
        keycloak:
        issuer-uri: http://localhost:8080/auth/realms/MyApplication
server:
  port: 8001

SecurityConfig. java

@EnableWebFluxSecurity
@Configuration
public class SecurityConfig
{

 @Bean
 SecurityWebFilterChain springSecurityFilterChain( ServerHttpSecurity http ) throws Exception
{


    return http.authorizeExchange().matchers( EndpointRequest.to( InfoEndpoint.class, HealthEndpoint.class)).permitAll().anyExchange().authenticated().and().oauth2Login().and().build();


}

}

1 Ответ

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

Ваш подход не хватает точки. Вам нужно добавить новый маршрут для незащищенного приложения в application.yml, и вы должны разрешить этот маршрут в конфигурации безопасности, которую вы упомянули в SecurityConfig. java.

application.yml

routes:
- id: testMyApplication
  uri: localhost:61307
  predicates:
  - Path=/myApplication/**

SecurityConfig. java

return http.authorizeExchange().pathMatchers( "/myApplication/**"").
permitAll().anyExchange().authenticated().and().oauth2Login().and().build();
...