HelloWorld
Код: Выделить всё
@RequestMapping("/")
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello, world!";
}
@GetMapping("/secured")
public String secured(Authentication auth) {
// !!! auth == null -> NullPointerException !!!
return "This page is secured. Your role is "
+ auth.getAuthorities()
.stream()
.map(GrantedAuthority::getAuthority)
.collect(Collectors.joining(", ")) + ".";
}
}
Код: Выделить всё
@Configuration
@EnableWebFluxSecurity
public class WebSecurityConfig {
@Bean
public SecurityWebFilterChain securityFilterChain(ServerHttpSecurity http, ReactiveAuthenticationManager authManager) throws Exception {
return http
.authorizeExchange(request -> request
.pathMatchers("/hello").permitAll()
.pathMatchers("/secured").authenticated()
.anyExchange().permitAll()
)
.csrf(csrf -> csrf.disable())
.httpBasic(httpBasic -> httpBasic.authenticationManager(authManager))
.build();
}
// some security configuration ...
}
Код: Выделить всё
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
@Bean
public RouteLocator myRoutes(RouteLocatorBuilder builder) {
return builder.routes()
.route(p -> p
.path("/**")
.uri("http://localhost:8081"))
.build();
}
}
- Как «отправить» аутентификацию от шлюза в другой микросервис без необходимости повторной аутентификации пользователя.
- Обычно ли аутентифицировать пользователя в микросервисе Gateway или мне следует продублировать механизм аутентификации в все остальные микросервисы.

Подробнее здесь: https://stackoverflow.com/questions/792 ... way-to-mic