【问题标题】:Spring Security OAuth2 simple configurationSpring Security OAuth2 简单配置
【发布时间】:2023-10-06 15:16:01
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: java spring spring-mvc spring-security spring-security-oauth2


    【解决方案1】:

    不要让 sparklr 示例让您感到困惑(它的功能远远超出您的需要)。 this 对你来说足够简单吗?

    @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)。附言这是 Spring OAuth 2.0 的全部内容(尚未发布),但我们正在努力解决(并且带有 XML 配置的 1.0 功能确实没有那么重)。

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

    【讨论】:

    • 谢谢戴夫。实际上 OAuth 服务器是在公司内部完成的,所以我得到了grant_type=password,因为我的应用程序是目前唯一使用 OAuth 服务器的东西。至于我正在开发的客户端应用程序,我只需要一个方法,我可以说“转到SERVER/token?client_id=&client_secret=&grant_type=password&username=request.getParameter("username")&password=request.getParameter("password") 获取 JSON response.get("access_token"),转到/user_info?access_token=access_token 和 BINGO,你得到了用户的信息,请将其存储在HttpSession。我似乎迷路了:(
    • 我的客户端应用程序中也没有要“保护”的页面(使用auth.doThis().doThat()),它使用AngularJS,这意味着它是一个安静的应用程序,当用户来到/doSomething?someParameters时,它会检查他们的会话(HttpSession),如果他们已登录,则 OK,否则它会在 JSON 中发送错误。
    • Dave,“这都是 Spring OAuth 2.0 的东西”是什么意思?旧好的 AuthEndpoint 和 TokenEndpoint 怎么了?
    • 没什么。仍然存在,只是即将发布一个新版本。
    • 是否有适用于 spring oauth release 2.0.5 的版本/示例?