簡體   English   中英

okta oauth2 Spring 安全 所有受保護的頁面重定向到登錄

[英]okta oauth2 Spring security all protected pages redirect to login

我正在嘗試在我的應用程序中使用 spring 安全性來實現 okta(custom-provider) oauth2 sso 功能。 應用程序成功提示用戶登錄並授權用戶。 我的 spring 安全配置如下所示。 這里的問題是除了登錄名和一些其他被賦予 permitAll() 的 url,所有受保護的頁面都被重定向到下面給出的 application.yml 中配置的“redirect-uri”(即登錄 url)。 我不明白為什么會發生這種情況,我也找不到 okta 文檔中給出的任何解決方案。 我在日志中沒有看到任何錯誤消息。 每次我訪問受保護或受保護的頁面時,重定向都會發生在 http://localhost:8080/login 這是重定向 uri 但我需要重定向到 go 到用戶選擇的相應頁面。 我在這里錯過了什么嗎?

安全配置.java

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.antMatcher("/**").authorizeRequests()
            .antMatchers("/","/login**","/vendor/**", "https://fonts.googleapis.com/**", "/css/**",
                    "https://www.thymeleaf.org", "/js/**").permitAll()
            .anyRequest().authenticated()
            .and()
            .oauth2Login();
    }
}

應用程序.yml

security:
    oauth2:
      client:
        registration:
          custom-client:
            client-id: 
            client-secret: 
            scope: ["openid", "profile", "email", "address", "phone", "groups"]
            provider: custom-provider
            state: xoxoxo
            redirect-uri: http://localhost:8080/login
            client-authentication-method: basic
            authorization-grant-type: authorization_code
            filter-order: 3
        provider:
          custom-provider:
            token-uri: https://did.oktapreview.com/oauth2/v1/token
            authorization-uri: https://did.oktapreview.com/oauth2/v1/authorize
            user-info-uri: https://did.oktapreview.com/oauth2/v1/authorize
            user-name-attribute: name

有人可以嗎

是的,當然,有一個錯誤的事情。

重定向uri:http://localhost:8080/login

redirect-uri 應該是指向您的 OAuth2.0 客戶端的回調端點的鏈接

就 Spring Security OAuth2.0 框架而言,它應該是以下模式:

重定向 uri:“{baseUrl}/login/oauth2/code/{registrationId}”

其中“baseUrl”是您的 URL(http://localhost),“registrationId”是注冊名稱,在您的情況下是“custom-client”

實際上,您可以將沒有特定 registrationId 和 baseUrl 的模式指定為“redirect-uri”參數,這樣您的配置應該如下所示:

security:
    oauth2:
      client:
        registration:
          custom-client:
            client-id: 
            client-secret: 
            scope: ["openid", "profile", "email", "address", "phone", "groups"]
            provider: custom-provider
            state: xoxoxo
            redirect-uri: "{baseUrl}/login/oauth2/code/{registrationId}"
            client-authentication-method: basic
            authorization-grant-type: authorization_code
            filter-order: 3
        provider:
          custom-provider:
            token-uri: https://did.oktapreview.com/oauth2/v1/token
            authorization-uri: https://did.oktapreview.com/oauth2/v1/authorize
            user-info-uri: https://did.oktapreview.com/oauth2/v1/authorize
            user-name-attribute: name

另外,請注意您的憑據,應該填寫。

暫無
暫無

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

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