簡體   English   中英

Spring Security OAuth2簡單配置

[英]Spring Security OAuth2 simple configuration

我有一個簡單的項目,需要簡單的以下配置:

  • 我有一個“密碼”grant_type,這意味着我可以提交用戶名/密碼(用戶在我的登錄表單中輸入),並在成功時獲得access_token。
  • 使用access_token,我可以請求API並獲取用戶的信息。

我知道API的URI,我不想要任何大的東西(我在https://github.com/spring-projects/spring-security-oauth/tree/master/samples上看到了配置)並且看起來很大。

我可以這樣想:

  • 做一個簡單的HTTP請求,給出* client_id *,* client_secret *,* grant_type =密碼*, 用戶名密碼 (用戶提供的)。
  • 我在JSON響應中收到* ACCESS_TOKEN *(和其他一些東西)。
  • 我使用* ACCESS_TOKEN *來查詢URL(使用簡單的GET請求),它將提供用戶的信息。
  • 我在HttpSession中設置信息並將用戶視為已登錄。

它可以在2個HTTP請求中完成。 我只是不想這樣做,而是使用“更安全”的方式而不是Spring Security OAuth2。

你能想到我需要做什么“簡單”配置才能完成這項工作嗎?

不要讓火花樣品混淆你(它比你似乎需要的更多)。 對你很簡單?

@ComponentScan
@EnableAutoConfiguration
public class Application {

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}

@Configuration
@Order(Ordered.LOWEST_PRECEDENCE - 100)
protected static class OAuth2Config extends OAuth2AuthorizationServerConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // @formatter:off
        auth.apply(new InMemoryClientDetailsServiceConfigurer())
            .withClient("my-trusted-client")
                .authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit")
                .authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT")
                .scopes("read", "write", "trust")
                .accessTokenValiditySeconds(60)
        .and()
            .withClient("my-client-with-secret")
                .authorizedGrantTypes("client_credentials")
                .authorities("ROLE_CLIENT")
                .scopes("read")
                .secret("secret");
    // @formatter:on
    }

}

}

那是auth服務器。 客戶端也很簡單(例如Spring OAuth項目中的客戶端)。 PS這是所有Spring OAuth 2.0的東西(尚未發布),但我們正在研究它(而且使用XML配置的1.0功能確實不那么重)。

注意這種方式會破壞OAuth2的對象(webapp客戶端應該收集用戶憑據)。 您應該考慮使用grant_type=authorization_code

暫無
暫無

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

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