簡體   English   中英

在 Spring 引導中無狀態地使用 Keycloak 和 JWT

[英]Use Keycloak and JWT Statelessly in Spring Boot

我需要使用 Keycloak,並且更喜歡在我的 Spring 引導應用程序中使用無狀態 JWT 令牌。 我可以讓它在會話中正常運行,但是在轉換它時我需要幫助

  • 強制 Spring 引導安全檢查登錄
  • 允許 /logout URL(進入 Keycloak)

我的代碼“運行”,但是當我點擊初始頁面時,我看到的日志消息似乎表明它沒有檢測到任何登錄跡象。發生這種情況時,我想強制 Spring Boot 重定向到登錄頁面就像這是一個有狀態的應用程序一樣。

org.springframework.web.servlet.FrameworkServlet: Failed to complete request: java.lang.NullPointerException: Cannot invoke "org.springframework.security.authentication.AbstractAuthenticationToken.getName()" because "authenticationToken" is null
org.springframework.security.web.context.HttpSessionSecurityContextRepository$SaveToSessionResponseWrapper: Did not store anonymous SecurityContext
org.springframework.security.web.context.SecurityContextPersistenceFilter: Cleared SecurityContextHolder to complete request
org.apache.juli.logging.DirectJDKLog: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException: Cannot invoke "org.springframework.security.authentication.AbstractAuthenticationToken.getName()" because "authenticationToken" is null] with root cause
java.lang.NullPointerException: Cannot invoke "org.springframework.security.authentication.AbstractAuthenticationToken.getName()" because "authenticationToken" is null
    

這是我的 HttpSecurity 片段:

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
        http
        .csrf()
//      .disable().exceptionHandling().accessDeniedPage("/access-denied")
        .and()
        .authorizeRequests()
        .antMatchers("/sso/**").permitAll()
        .antMatchers("/error/**").permitAll()
        .antMatchers("/css/**","/contact-us","/actuator/**","/isalive/**").permitAll()
        .anyRequest().authenticated()
        .and()
        .oauth2Login()
        .defaultSuccessUrl("/myfirstpage",true)
        .and().exceptionHandling().authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED))
        .and()
        .oauth2ResourceServer().jwt();

    }

我知道我遺漏了一些東西,但我認為 Keycloak 提供了很多這樣的東西 OOTB。 初始 URL 是 /。 我曾希望 the.authenticated() 會強制它針對所有不允許的模式進行身份驗證,但我可能錯了。 我錯過了什么?

請注意,互聯網上充斥着 Spring Boot + Keycloak 的示例(有些甚至還不錯)。 它還有很多 Spring Boot + OAuth + Stateless JWT。 它沒有(我可以說)很多 Spring Boot + Keycloak + Stateless JWT。 我從這個 JHipster repo 中得到了一點我可以找到的東西,但我覺得我錯過了一些偉大的神奇步驟。

您將需要 spring-security 和 keycloak-adapters 這個指南有完整的解釋如何設置和保護它

https://keepgrowth.in/java/springboot/keycloak-with-spring-boot-1-configure-spring-security-with-keycloak/

當身份驗證丟失或無效(過期、錯誤的頒發者等)時,資源服務器應返回 401(未經授權),並且客戶端應處理重定向到授權服務器

當您嘗試將不同的 OAuth2 參與者(客戶端、資源服務器和授權服務器)合並到單個應用程序中時,事情會變得一團糟。

你確定你想要一個 Spring客戶端(而不是 Angular / React / Vue / Flutter / 任何客戶端渲染框架)?

如果是,也許您應該首先拆分客戶端(顯示登錄、注銷和 Thymeleaf 或任何頁面)和資源服務器(REST API)應用程序。 您將更好地理解您編寫的 spring-security conf(並且可以斷言它單獨按預期工作)。

資源服務器配置 (REST API)

請注意,不推薦使用 spring 的 Keycloak 適配器

最簡單的解決方案在這里(支持多租戶,默認情況下無狀態,CORS 從屬性配置,簡單的 Keycloak 角色映射到 Spring 權限等等)。

您也可以直接使用spring-boot-starter-oauth2-resource-server ,但它需要更多 java conf

客戶端配置(包括登錄和注銷的頁面)

  • 如果保留 Spring,請參閱 spring-boot 文檔:它很清楚並且始終是最新的(與大多數教程相反)
  • 如果使用“現代”客戶端框架,請從認證列表中找到一個庫

我有一個完整的示例(帶有 spring RESTful API 的 Ionic-Angular UI) 但作為初學者可能有點復雜。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM