我正在设置一个 Keycloak 实例以使用包含Spring安全性的 spring 启动应用程序。我使用邮递员来测试服务。我从获取新的访问令牌开始,工作正常。当我对安全endpoint进行 HTTP GET 调用时 - 一切正常,员工返回。但是当我对安全endpoint进行 HTTP POST/PUT/DELETE 调用时,Keycloak 说错误 403 禁止。请看一看,告诉我出了什么问题。
我已经测试过http.csrf()。disable()选项,然后工作正常,但它不是生产的解决方案。
安全配置.java
@Configuration
@EnableWebSecurity
@ComponentScan(basePackageClasses = KeycloakSecurityComponents.class)
class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) {
KeycloakAuthenticationProvider keycloakAuthenticationProvider = keycloakAuthenticationProvider();
keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(new SimpleAuthorityMapper());
auth.authenticationProvider(keycloakAuthenticationProvider);
}
@Bean
public KeycloakSpringBootConfigResolver KeycloakConfigResolver() {
return new KeycloakSpringBootConfigResolver();
}
@Bean
@Override
protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
http
.authorizeRequests()
.antMatchers( "/api/**").hasRole("my_admin")
.anyRequest().permitAll();
}
应用程序. yml:
keycloak:
auth-server-url: http://localhost:11080/auth
realm: myrealm
resource: myclient
public-client: true
principal-attribute: preferred_username
ssl-required: external
use-resource-role-mappings: true
应用程序的一些日志(keyclock跟踪日志级别):
2019-04-01 14:04:54.741 DEBUG 2952 --- [io-1080-exec-10] o.k.adapters.PreAuthActionsHandler : adminRequest http://localhost:1080/api/my-endpoint
2019-04-01 14:04:54.741 DEBUG 2952 --- [io-1080-exec-10] .k.a.t.AbstractAuthenticatedActionsValve : AuthenticatedActionsValve.invoke /api/my-endpoint
2019-04-01 14:04:54.741 DEBUG 2952 --- [io-1080-exec-10] o.k.a.AuthenticatedActionsHandler : AuthenticatedActionsValve.invoke http://localhost:1080/api/my-endpoint
2019-04-01 14:04:54.741 DEBUG 2952 --- [io-1080-exec-10] o.k.a.AuthenticatedActionsHandler : Policy enforcement is disabled.
2019-04-01 14:04:54.742 DEBUG 2952 --- [io-1080-exec-10] o.k.adapters.PreAuthActionsHandler : adminRequest http://localhost:1080/error
2019-04-01 14:04:54.743 DEBUG 2952 --- [io-1080-exec-10] f.KeycloakAuthenticationProcessingFilter : Request is to process authentication
2019-04-01 14:04:54.743 DEBUG 2952 --- [io-1080-exec-10] f.KeycloakAuthenticationProcessingFilter : Attempting Keycloak authentication
2019-04-01 14:04:54.743 TRACE 2952 --- [io-1080-exec-10] o.k.adapters.RequestAuthenticator : --> authenticate()
2019-04-01 14:04:54.743 TRACE 2952 --- [io-1080-exec-10] o.k.adapters.RequestAuthenticator : try bearer
2019-04-01 14:04:54.743 DEBUG 2952 --- [io-1080-exec-10] o.k.a.BearerTokenRequestAuthenticator : Found [1] values in authorization header, selecting the first value for Bearer.
2019-04-01 14:04:54.743 DEBUG 2952 --- [io-1080-exec-10] o.k.a.BearerTokenRequestAuthenticator : Verifying access_token
2019-04-01 14:04:54.743 TRACE 2952 --- [io-1080-exec-10] o.k.a.BearerTokenRequestAuthenticator : access_token: eyJhbs...blablab....signature
2019-04-01 14:04:54.744 DEBUG 2952 --- [io-1080-exec-10] o.k.a.BearerTokenRequestAuthenticator : successful authorized
2019-04-01 14:04:54.744 TRACE 2952 --- [io-1080-exec-10] o.k.a.RefreshableKeycloakSecurityContext : checking whether to refresh.
2019-04-01 14:04:54.744 TRACE 2952 --- [io-1080-exec-10] org.keycloak.adapters.AdapterUtils : useResourceRoleMappings
2019-04-01 14:04:54.744 TRACE 2952 --- [io-1080-exec-10] org.keycloak.adapters.AdapterUtils : Setting roles:
2019-04-01 14:04:54.744 TRACE 2952 --- [io-1080-exec-10] org.keycloak.adapters.AdapterUtils : role: my_admin
2019-04-01 14:04:54.744 DEBUG 2952 --- [io-1080-exec-10] a.s.a.SpringSecurityRequestAuthenticator : Completing bearer authentication. Bearer roles: [my_admin]
2019-04-01 14:04:54.745 DEBUG 2952 --- [io-1080-exec-10] o.k.adapters.RequestAuthenticator : User 'my_user' invoking 'http://localhost:1080/error' on client 'myclient'
2019-04-01 14:04:54.745 DEBUG 2952 --- [io-1080-exec-10] o.k.adapters.RequestAuthenticator : Bearer AUTHENTICATED
2019-04-01 14:04:54.745 DEBUG 2952 --- [io-1080-exec-10] f.KeycloakAuthenticationProcessingFilter : Auth outcome: AUTHENTICATED
2019-04-01 14:04:54.745 DEBUG 2952 --- [io-1080-exec-10] f.KeycloakAuthenticationProcessingFilter : Authentication success using bearer token/basic authentication. Updating SecurityContextHolder to contain: org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken@fb0506b7: Principal: my_user; Credentials: [PROTECTED]; Authenticated: true; Details: org.keycloak.adapters.springsecurity.account.SimpleKeycloakAccount@ecf147d; Granted Authorities: ROLE_my_admin
2019-04-01 14:04:54.745 DEBUG 2952 --- [io-1080-exec-10] o.k.a.AuthenticatedActionsHandler : AuthenticatedActionsValve.invoke http://localhost:1080/error
2019-04-01 14:04:54.745 DEBUG 2952 --- [io-1080-exec-10] o.k.a.AuthenticatedActionsHandler : Policy enforcement is disabled.
这是Spring Security公司提供的CSRF保护。用Keycloak token真的需要这种保护吗?令牌只包含在cookie中,还是也存在于授权头中?
我发现我没有在ajax请求中使用CSRF令牌。Spring Security会自动启用CSRF保护。CSRF令牌是为web服务的第一次调用自动生成的,它具有会话范围。您需要将该令牌保存在meta标记中。要求每个非GET请求都包含CSRF令牌(GET请求不需要用CSRF令牌保护,因为它们是为非修改API调用设计的)。
解决方案:只需在html页面(thymeeaf)的标题中包含该行:
并在 ajax 调用中使用它
headers: { 'X-CSRF-TOKEN': $('#_csrf').attr('content') },
您应该执行以下两个配置:
http.csrf(). disable()
我在试用Quarkus与keycloak的安全集成 下面是我的restendpoint 我的应用程序属性 任何帮助都将不胜感激
我使用http://localhost:8080/auth/realms/{realm_name}/protocol/openid-connect/token endpoint创建令牌。 grant_type=client_credentials 客户端-id:------------- 客户端-secret:78296D38-CC82-4010-A817-65C283484E51 现在我想获得r
我有以下配置,可以使用用户名/密码连接到active directory,以验证Camel中的REST DSL服务。http://localhost:3000/api/data/1234 会不会是角色引起的什么? 如果你知道任何解决办法,我很感激。
每次尝试从数据库中获取用户信息时,我都会收到一个错误。关于我下面的代码,每次我按下Ajax测试按钮尝试请求时,它都无法运行并向我发出警报,但控制台中也会向我发出403禁止的错误。我不确定这是否与Spring security有关? 用户JSP页面: 用户控制器类:
在一段时间内,通过HTTPS与我们的服务器的通信工作得很好。 上周提供商更改了一些东西,从现在开始,我总是得到403错误。 我使用最新的OpenSSL DLLs V0.9.8 我已将UserAgent更改为“Mozilla/5.0(Windows NT 10.0)” SSL方法为“SSLVSSLV23” SSL模式为“SSLMClient” 什么都帮不上忙。:( 这是我得到的日志:
问题内容: 每当我尝试从数据库中获取用户信息时,都会收到-error 消息。关于下面的代码,每当我尝试通过按Ajax测试按钮尝试请求时,它都无法运行并给我发出警报,但是在控制台中也给我一个-error。我不确定是否与Spring安全性有关? 用户JSP页面: 用户控制器类: 问题答案: 它通常是由Spring默认的CSRF保护引起的。 例如,如果你使用JS代码中的DELETE HTTP请求,则还需