spring-boot - 我可以配置 Zuul 代理以仅在用户通过身份验证的情况下对用户和代理请求进行身份验证吗?
问题描述
目前我在端口 8082 和 8083 上有 2 个 Spring Boot 应用程序。
在 8082 端口上运行的应用程序是 Zuul 网关。我想使用此应用程序来防止无法访问可在 8083 上找到的资源的用户通过发送错误的令牌来减慢该应用程序的速度。
我使用 Keycloak 作为开放 ID 提供程序(在端口 8180 上运行)。
我让它与 keycloak 适配器和一些 java 配置类一起工作,但我想知道我是否可以让它更容易工作,只使用 Zuul?
我有这些 application.properties 和 application.yml:
zuul:
sensitiveHeaders: Cookie,Set-Cookie
routes:
resource:
path: /resource/**
url: http://localhost:8083
keycloak.auth-server-url = http://localhost:8180/auth
keycloak.resource = my-service
keycloak.credentials.secret = my-client-secret
keycloak.realm = dev
keycloak.ssl-required = none
keycloak.use-resource-role-mappings = true
keycloak.bearer-only = true
我的配置类:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, jsr250Enabled = true, securedEnabled = true)
@RequiredArgsConstructor
public class KeycloakSecurityConfig extends KeycloakWebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
http
.headers()
.frameOptions()
.disable()
.and()
.csrf()
.disable()
.authorizeRequests()
.antMatchers("/**").authenticated()
.and()
.sessionManagement(cust -> cust.sessionCreationPolicy(SessionCreationPolicy.STATELESS));
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) {
KeycloakAuthenticationProvider keycloakAuthenticationProvider = keycloakAuthenticationProvider();
keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(new SimpleAuthorityMapper());
auth.authenticationProvider(keycloakAuthenticationProvider);
}
@Bean
@Override
protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl());
}
@Bean
public KeycloakConfigResolver keycloakConfigResolver() {
return new KeycloakSpringBootConfigResolver();
}
}
我的主要课程:
@SpringBootApplication
@EnableZuulProxy
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
}
它工作得很好......如果我在 Postman 中发送一个http://localhost:8082/resource/hello
带有Authorization
包含 的标头的请求Bearer {access-token}
,它会在端口 8083 上代理到我的应用程序,并且我会Hello World!
按预期返回消息。但我想知道不使用 keycloak 适配器是否有更简单的设置?如果可能的话,只能通过配置 Zuul 吗?如果没有,在更改某些 URI/属性后,我如何制作一个适用于大多数 openid 提供程序的“通用”实现?(如果我想使用另一个 openid 提供程序而不是 keycloak,这可能会很有用......)
提前致谢。
解决方案
推荐阅读
- dynamics-crm - 我们如何在 Dynamic 365 中使用区分大小写的数据执行高级搜索
- android - 如何在服务中获取资产?
- javascript - 删除javascript中url的第一段和最后一段
- django - Django:一次性在所有应用程序上运行 pylint
- ember.js - 当我刷新页面时,Ember-simple-auth 会话过期
- sql - BigQuery:使用 COUNT 作为 LIMIT
- java - 通过 HTTP 在 Java 上放置
- sql-server - 对索引字段执行 sp_prepexec 查询时性能不佳,为什么?
- sql - SQL - 使用 Where 子句连接两个视图时查询性能变慢
- json - 将时间戳转换为液体(json)中的字符串时出错