我有一个项目https://github.com/ndrone/sample-gateway-oauth2login/tree/feature/allowAllToHealth我试图允许特定 URL 对任何请求它的人开放。在这种情况下,它是 Actuator 的健康端点,同时保护所有其他 Actuator 端点。我发现的是,TokenRelayGatewayFilterFactory
尽管它仅设置为应用于一条路线,但它正在应用于所有路线。不知道我做错了什么。
资源服务中的 SecurityConfig
@EnableWebFluxSecurity
public class SecurityConfig {
@Bean
SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) throws Exception {
http.authorizeExchange().pathMatchers("/manage/health").permitAll();
http
.authorizeExchange()
.pathMatchers("/resource", "/manage/**").hasAuthority("SCOPE_resource.read")
.anyExchange().authenticated()
.and()
.oauth2ResourceServer()
.jwt();
return http.build();
}
}
网关路由
@Controller
@SpringBootApplication
public class GatewayApplication {
@Autowired
private TokenRelayGatewayFilterFactory filterFactory;
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
//@formatter:off
return builder.routes()
.route("resource-health", r -> r.path("/resource/manage/health")
.filters(f -> f.stripPrefix(1))
.uri("http://localhost:9000"))
.route("resource-actuator-protected", r -> r.path("/resource/manage/**")
.filters(f -> f.stripPrefix(1).filter(filterFactory.apply()))
.uri("http://localhost:9000"))
.route("resource", r -> r.path("/resource")
.filters(f -> f.filter(filterFactory.apply()))
.uri("http://localhost:9000"))
.build();
//@formatter:on
}
@GetMapping("/")
public String index(Model model,
@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authorizedClient,
@AuthenticationPrincipal OAuth2User oauth2User) {
model.addAttribute("userName", oauth2User.getName());
model.addAttribute("clientName", authorizedClient.getClientRegistration().getClientName());
model.addAttribute("userAttributes", oauth2User.getAttributes());
return "index";
}
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}