首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Spring安全OAuth2简单配置

Spring安全OAuth2简单配置
EN

Stack Overflow用户
提问于 2014-03-04 18:05:15
回答 1查看 7.9K关注 0票数 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中设置了信息,并将用户视为登录。

它可以在两个HTTP请求中完成。我只是不想这样做,但是使用“更安全”的方法来代替Security OAuth2。

你能想到我需要做什么“简单”配置才能做到这一点吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-04 19:31:09

不要让火花样本迷惑你(它做的比你看起来需要的多得多)。this对你来说够简单吗?

代码语言:javascript
运行
复制
@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
    }

}

}

那是奥斯服务器。客户端也很容易(例如the one in the Spring OAuth project)。P.S.这都是SpringXML2.0的内容(还没有发布),但是我们正在开发它(而且带有XML的OAuth特性并没有那么重)。

注:这类方法击败了OAuth2的对象(webapp客户端不应该收集用户凭据)。您应该考虑使用grant_type=authorization_code

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22179573

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档