我有一个简单的项目,需要以下简单的配置:
- 我有一个“密码”grant_type,这意味着我可以提交用户名/密码(用户在登录表单中输入),并在成功时获得 access_token。
- 有了该 access_token,我就可以请求 API 并获取用户的信息。
我知道 API 的 URI,我不需要任何巨大的东西(我看到了配置https://github.com/spring-projects/spring-security-oauth/tree/master/samples https://github.com/spring-projects/spring-security-oauth/tree/master/samples)而且看起来很大。
我可以这样想:
- 执行一个简单的 HTTP 请求,给出 *client_id* 、 *client_secret* 、 *grant_type=password* 、username and password(用户提供的)。
- 我在 JSON 响应中收到 *ACCESS_TOKEN* (和一些其他内容)。
- 我使用 *ACCESS_TOKEN* 查询 URL(使用简单的 GET 请求),这将提供用户的信息。
- 我在 HttpSession 中设置信息并认为用户已登录。
它可以在 2 个 HTTP 请求中完成。我只是不想这样做,而是使用“更安全”的方式来代替 Spring Security OAuth2。
你能想到我需要进行什么“简单”配置才能完成此任务吗?
不要让 Sparklr 示例让您感到困惑(它的作用比您看起来需要的要多得多)。是this https://github.com/scratches/sparklr-boot/blob/master/src/main/java/demo/Application.java对你来说足够简单吗?
@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 项目中的一个 https://github.com/spring-projects/spring-security-oauth/blob/master/samples/oauth2/tonr/src/main/java/org/springframework/security/oauth/examples/config/SecurityConfig.java)。附:这都是 Spring OAuth 2.0 的内容(尚未发布),但我们正在研究它(带有 XML 配置的 1.0 功能实际上并没有那么重)。
注意:这种方式违背了 OAuth2 的目标(webapp 客户端是not应该收集用户凭据)。你应该考虑使用grant_type=authorization_code
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)