当前位置: 首页 > 知识库问答 >
问题:

使用keycloak-jwt令牌保护Spring Boot服务

融唯
2023-03-14

所以,我用keycloak来保护我的服务。客户端应用程序从keycloak服务器获取访问令牌,并使用它来保护对Spring boot应用程序的访问。我已经用keycloak属性配置了Spring Boot应用程序,使用的是只承载访问类型:

keycloak.realm = master
keycloak.realmKey = ...
keycloak.auth-server-url = http://localhost:8080/auth
keycloak.ssl-required = external
keycloak.resource = boot-app
keycloak.bearer-only = true
keycloak.cors = true

Spring靴键斗篷启动器:

<dependency>
    <groupId>org.keycloak</groupId>
    <artifactId>keycloak-spring-boot-starter</artifactId>
</dependency>
@Configuration
@ComponentScan(basePackageClasses = KeycloakSecurityComponents.class)
public class KeycloakSecurityConfig extends KeycloakWebSecurityConfigurerAdapter
{
  /**
   * Registers the KeycloakAuthenticationProvider with the authentication manager.
   */
  @Autowired
  public void configureGlobal(final AuthenticationManagerBuilder auth) throws Exception
  {
    final KeycloakAuthenticationProvider keycloakAuthenticationProvider = keycloakAuthenticationProvider();
    keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(new SimpleAuthorityMapper());
    auth.authenticationProvider(keycloakAuthenticationProvider);
  }

  @Bean
  public KeycloakConfigResolver keycloakConfigResolver()
  {
    return new KeycloakSpringBootConfigResolver();
  }

  /**
   * Defines the session authentication strategy.
   */
  @Bean
  @Override
  protected SessionAuthenticationStrategy sessionAuthenticationStrategy()
  {
    return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl());
  }

  @Override
  protected void configure(final HttpSecurity http) throws Exception
  {
    super.configure(http);
    http
        .authorizeRequests()
        .antMatchers(
            "/v2/api-docs",
            "/configuration/ui",
            "/swagger-resources",
            "/configuration/security",
            "/swagger-ui.html",
            "/webjars/**",
            "/swagger-resources/configuration/ui",
            "/swagge‌​r-ui.html",
            "/swagger-resources/configuration/security").permitAll()
        .antMatchers("/*").hasRole("user")
        .anyRequest().authenticated();
  }
}
keycloak.realmKey
keycloak.auth-server-url

公钥不就是你所需要的一切吗?

提前致谢

共有1个答案

商高谊
2023-03-14

实际上,对于只承载,您可能会想知道为什么需要KC URL,但是由于一些KC版本,realmkey不再是强制性的,因为我们使用密钥循环。这意味着应用程序将使用auth-server-url属性从KC服务器动态检索公钥。

 类似资料:
  • 问题内容: 该问题在某种程度上与以下链接的问题有关。但是,我需要在某些方面和一些其他信息上多一点清晰度。 我需要使用令牌为REST Web服务实现安全性 该Web服务旨在与Java客户端一起使用。因此,表单身份验证和凭据弹出窗口没有用。 我是REST安全和加密的新手 这是我到目前为止所了解的: 对于第一个请求: 用户建立https连接(或容器使用301确保https) 用户POST输入用户名和密码

  • 这个问题在某种程度上与下面的链接问题有关。然而,我需要在某些方面更多的澄清和一些额外的信息。参考:REST Web服务身份验证令牌实现 背景: null null 对于后续请求: 客户端发送此加密令牌和哈希组合(使用基本的用户名字段?) 我们使用哈希确保加密的令牌没有被篡改,然后对其解密 我们检查会话跟踪表中解密的令牌是否有未过期的条目,并获得实际的用户名(过期由代码管理?) 如果找到用户名,则根

  • 我所有的请求都有403个被禁止的回复。我发送的firebase令牌正在成功验证,所以我不确定设置时缺少了什么步骤?我只是想让一个简单的流工作,其中我的endpoint必须在头中有有效的firebase JWT令牌(在排除的健康endpoint之外)。 当前步骤: 自定义OncePerRequest estFilter已创建并正在相应的endpoint上命中 授权头令牌成功验证 身份验证对象创建为U

  • 我读到,当使用JWT时,不需要防止CSRF攻击,例如:“由于您不依赖cookie,您不需要防止跨站点请求”。 但是,有一点我不明白:如果我将令牌存储在localStorage中(正如我在同一网站的教程中被告知的那样),那么攻击者如何防止通过读取我的localStorage而不是Cookie来伪造恶意请求呢? 由于它是在服务器端生成的,我不知道如何在客户机请求中使用令牌而不将其存储在客户机的某个地方

  • 我们有一个简单的应用程序,只有两个消费者和5个endpoint。对于一个endpoint,我需要某种身份验证方式。我喜欢这样做的条纹方式,但我不知道我如何可以在spring Boot中构建这个。 “对API的身份验证是通过HTTP基本身份验证执行的。请提供API密钥作为基本身份验证用户名值。不需要提供密码。”

  • 我的问题是,对每个表单使用一个令牌,而不更新每个表单提交的令牌,安全吗?这能防止CSRF吗?

  • 我尝试用angular2前端实现jwt令牌。当我尝试使用Postman接收带有post方法的令牌时,我接收到授权令牌,但在Angular中这样做返回空响应对象。这里是我使用的Angular服务的代码片段。 问题是,当我尝试记录时,令牌是空的,与响应相同。对于代码的后端部分,我遵循了jwt令牌的这个实现。

  • 我正在尝试使用KeyCloak保护WildFly上的一个简单的。war应用程序。我已经完成了keycloak入门教程,创建了一个“演示”领域,并成功地将其部署到带有keycloak身份验证的vanilla应用程序中。我正在尝试将KeyCloak添加到一个示例WildFly webapp(https://github.com/WildFly/quickstart/tree/master/hellow