背景
我按照本教程使用 spring-security-oauth2 配置 OAuth2 服务器。
到目前为止,它是这样工作的:
- 当用户尝试连接到服务器时,会显示一个普通的旧身份验证对话框
- 如果提供了正确的凭据,用户将被重定向到默认的 Spring 确认表单。
- 如果用户选择“允许”,则用户将被重定向到我想要的任何页面,并带有可用于获取访问令牌的授权码。
SSCCE
我将这个准备运行的示例放在我的 github 帐户上(这或多或少是教程给出的代码)
克隆:
git clone https://github.com/ArnaudDenoyelle/sscce-spring-oauth2.git
执行:
mvn spring-boot:run
然后导航到:
http://localhost:9999/uaa/oauth/authorize?response_type=code&client_id=acme&redirect_uri=http://example.com
通过以下方式进行身份验证:
-user : user
-password : password
问题
现在,我想改变行为:
- 我更喜欢登录/密码表单,而不是普通的旧身份验证对话框。
- 我不想要要求用户授权的第二步:我只想获得一个访问令牌。
到目前为止,我可以通过以下步骤来做到这一点:
- 用户以自定义形式输入其凭据。
- 我发送一个 GET 请求到
http://localhost:9999/uaa/oauth/authorize?response_type=code&client_id=acme&redirect_uri=http://example.com
- 我收到一个HTML 响应(这是确认表单),但并不真正关心它,只要它是 HTTP 200。它还为我提供了一些下一步所需的 cookie。
http://localhost:9999/uaa/oauth/authorize
我使用默认弹簧确认表单发送的相同正文参数 发送 POST 请求:- user_oauth_approval = true
- scope.openid = true
它有效:用户被重定向到我想要的页面(在这种情况下:http ://example.com?code= $authorization_code$)
问题
我真正想要的只是摆脱 Spring 的确认表单。在我的情况下它是无用的:我只想获取当前会话的访问令牌,而不是永久授权客户端访问私有资源。有没有这样的配置?
相关代码是:
@SpringBootApplication
public class AuthserverApplication extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(AuthserverApplication.class, args);
}
@Configuration
@EnableAuthorizationServer
protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager);
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("acme")
.secret("acmesecret")
.authorizedGrantTypes("authorization_code", "refresh_token", "password").scopes("openid");
}
}
}