7

我有一个简单的项目,需要以下简单的配置:

  • 我有一个“密码”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=password* 、用户名密码(用户提供)。
  • 我在 JSON 响应中收到 *ACCESS_TOKEN*(和其他一些东西)。
  • 我使用 *ACCESS_TOKEN* 来查询 URL(使用简单的 GET 请求),这将提供用户的信息。
  • 我在 HttpSession 中设置信息并认为用户已登录。

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

你能想到我需要做什么“简单”的配置来完成这项工作吗?

4

1 回答 1

8

不要让 sparklr 样本让您感到困惑(它所做的比您似乎需要的要多得多)。这你来说足够简单吗?

@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
    }

}

}

那是身份验证服务器。客户端也很简单(例如Spring OAuth 项目中的那个)。PS 这都是 Spring OAuth 2.0 的东西(尚未发布),但我们正在努力解决它(并且带有 XML 配置的 1.0 特性真的没有那么重)。

NB 这种破坏 OAuth2 的对象(webapp 客户端应该收集用户凭据)。您应该考虑使用grant_type=authorization_code.

于 2014-03-04T19:31:09.323 回答