Java 带REST请求的Spring OAuth2身份验证
背景 为了使用spring-security-OAuth2配置OAuth2服务器,我遵循了以下步骤 到目前为止,它是这样工作的:Java 带REST请求的Spring OAuth2身份验证,java,spring-security,spring-oauth2,Java,Spring Security,Spring Oauth2,背景 为了使用spring-security-OAuth2配置OAuth2服务器,我遵循了以下步骤 到目前为止,它是这样工作的: 当用户尝试连接到服务器时,将显示一个普通的旧身份验证对话框 如果提供了正确的凭据,则会将用户重定向到默认的Spring确认表单 如果用户选择“允许”,则会使用可用于获取访问令牌的授权码将用户重定向到我想要的任何页面 SSCCE 我放置了这个准备运行的示例(这或多或少是教程给出的代码) 克隆到: git clone https://github.com/Arnaud
- 当用户尝试连接到服务器时,将显示一个普通的旧身份验证对话框
- 如果提供了正确的凭据,则会将用户重定向到默认的Spring确认表单李>
- 如果用户选择“允许”,则会使用可用于获取访问令牌的授权码将用户重定向到我想要的任何页面
git clone https://github.com/ArnaudDenoyelle/sscce-spring-oauth2.git
执行时:
mvn spring-boot:run
-user : user
-password : password
然后导航到:
http://localhost:9999/uaa/oauth/authorize?response_type=code&client_id=acme&redirect_uri=http://example.com
通过以下方式验证:
mvn spring-boot:run
-user : user
-password : password
问题
现在,我想改变行为:
- 我更喜欢登录/密码表单,而不是普通的旧身份验证对话框
- 我不想让第二步要求用户授权:我只想获得一个访问令牌
- 用户以自定义形式输入其凭据
- 我向
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
- 用户\u oauth\u批准=真
- scope.openid=true
@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");
}
}
}