Ask*_*ons 5 csrf spring-security keycloak websecurity
对于活跃开发的软件,我们使用 Spring Boot(带有 Spring Security)和 Keycloak Adapter。
目标是:
@Public
(参见代码片段)(这有效)一切正常,但我在理解一些细节时遇到了一些问题:
KeycloakWebSecurityConfigurerAdapter
能够CSRF
保护。我认为这样做只是为了它可以注册自己的匹配器以允许来自 Keycloak 的请求JSESSIONID
也会返回 cookie根据我的理解:
KeycloakWebSecurityConfigurerAdapter
启用它)。这只是BASIC Auth
部分吗?CSRF
确实需要保护 - 但我首先不想要会话,然后 API 就不需要CSRF
保护,对吗?http.sessionManagement().disable()
在super.configure(http)
通话后设置了JSESSIONID
cookie(所以这是从哪里来的?)正如代码片段中所述,SessionAuthenticationStrategy
由于我们使用了Authorization
Keycloak的一部分并且应用程序是 a Service Account Manager
(因此管理这些资源记录),因此一次未设置为 null 。
如果有人可以解决问题,那就太好了。提前致谢!
@KeycloakConfiguration
public class WebSecurityConfiguration extends KeycloakWebSecurityConfigurerAdapter {
@Inject private RequestMappingHandlerMapping requestMappingHandlerMapping;
@Override
protected void configure(final HttpSecurity http) throws Exception {
super.configure(http);
http
.authorizeRequests()
.requestMatchers(new PublicHandlerMethodMatcher(requestMappingHandlerMapping))
.permitAll()
.anyRequest()
.authenticated();
}
// ~~~~~~~~~~ Keycloak ~~~~~~~~~~
@Override
@ConditionalOnMissingBean(HttpSessionManager.class)
@Bean protected HttpSessionManager httpSessionManager() {
return new HttpSessionManager();
}
/**
* {@link NullAuthenticatedSessionStrategy} is not used since we initiate logins
* from our application and this would not be possible with {@code bearer-only}
* clients (for which the null strategy is recommended).
*/
@Override
@Bean protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl());
}
/**
* HTTP session {@link ApplicationEvent} publisher needed for the
* {@link SessionRegistryImpl} of {@link #sessionAuthenticationStrategy()}
* to work properly.
*/
@Bean public HttpSessionEventPublisher httpSessionEventPublisher() {
return new HttpSessionEventPublisher();
}
@Override
@Bean public KeycloakAuthenticationProvider keycloakAuthenticationProvider() {
return super.keycloakAuthenticationProvider();
}
}
Run Code Online (Sandbox Code Playgroud)
您可能会过度使用 JWT 令牌。例如,请查看这篇文章https://blog.logrocket.com/jwt-authentication-best-practices/。特别是请查看文章末尾有关 JWT 作为会话令牌的参考资料。
对于您的 Web 应用程序 UI,您在大多数情况下都使用会话。使用什么类型的令牌进行身份验证并不重要。Keycloak 正确执行所有操作 - 它返回用于会话管理的 httpOnly 安全 cookie,并在后端跟踪用户状态。为了更好地理解它是如何工作的,您可以查看这里的示例代码:示例
为了更好地分离无状态后端(微)服务和用户 UI 会话 keycloak文档建议使用 2 种不同的身份验证策略:RegisterSessionAuthenticationStrategy
用于会话和NullAuthenticatedSessionStrategy
仅承载服务
归档时间: |
|
查看次数: |
3283 次 |
最近记录: |