Angularjs Spring Security OAUTH2使用用户名/密码获取令牌

Angularjs Spring Security OAUTH2使用用户名/密码获取令牌,angularjs,spring,security,oauth,spring-security,Angularjs,Spring,Security,Oauth,Spring Security,我的问题很“简单”。我不知道如何设置OAUTH2身份验证服务器以接受用户名/密码并返回令牌 如果我使用: 卷曲:password@localhost:8081/oauth/token\?授权类型=客户端凭据 它返回令牌,但问题是它在数据库中注册用户“curl”,所以。。。不太好 如果我使用: 它会提示“用户名和密码”对话框,我输入用户名和密码,然后它会问我“您是否授权“web”访问您受保护的资源?” scope.read:Approve Deny“ 我可以将这两者结合起来,创建一个简单的请求,

我的问题很“简单”。我不知道如何设置OAUTH2身份验证服务器以接受用户名/密码并返回令牌

如果我使用:

卷曲:password@localhost:8081/oauth/token\?授权类型=客户端凭据 它返回令牌,但问题是它在数据库中注册用户“curl”,所以。。。不太好

如果我使用:

它会提示“用户名和密码”对话框,我输入用户名和密码,然后它会问我“您是否授权“web”访问您受保护的资源?”

scope.read:Approve Deny“

我可以将这两者结合起来,创建一个简单的请求,返回令牌吗?我想将它用于angularjs前端,在Spring Boot和Jersey中使用RESTful WS

我应该使用这个方案吗

使用这个配置->clients.jdbc(数据源)

如何为该方案设置一个用户?只需使用用户名和密码进行基本登录

OauthConfiguration

@Configuration
@EnableAuthorizationServer
public class OAuthConfiguration extends AuthorizationServerConfigurerAdapter
{

@Autowired
private DataSource dataSource;

@Bean
public TokenStore tokenStore()
{
    return new JdbcTokenStore(dataSource);
}

@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception
{
    endpoints.tokenStore(tokenStore());
}

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception
{
    // @formatter:off
    clients.inMemory()
            .withClient("curl")
            .authorities("USER")
            .resourceIds("reee")
            .scopes("read", "write")
            .authorizedGrantTypes("client_credentials")
            .secret("password")
            .and()
            .withClient("web")
            .redirectUris("http://github.com/techdev-solutions/")
            .resourceIds("reee")
            .scopes("read")
            .authorizedGrantTypes("implicit");
    // @formatter:on
}
}
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter
{
@Autowired
private DataSource dataSource;

@Override
protected void configure(HttpSecurity http) throws Exception
{
    http.authorizeRequests().antMatchers("/**").authenticated()
            .and().httpBasic().realmName("OAuth Server");
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception
{
    PasswordEncoder encoder = new BCryptPasswordEncoder();

    auth.userDetailsService(userDetailsService()).passwordEncoder(encoder);
    auth.jdbcAuthentication().dataSource(dataSource);
}

@Bean
public UserDetailsService userDetailsService()
{
    return new CustomUserDetailsService(dataSource);
}
}
证券配置

@Configuration
@EnableAuthorizationServer
public class OAuthConfiguration extends AuthorizationServerConfigurerAdapter
{

@Autowired
private DataSource dataSource;

@Bean
public TokenStore tokenStore()
{
    return new JdbcTokenStore(dataSource);
}

@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception
{
    endpoints.tokenStore(tokenStore());
}

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception
{
    // @formatter:off
    clients.inMemory()
            .withClient("curl")
            .authorities("USER")
            .resourceIds("reee")
            .scopes("read", "write")
            .authorizedGrantTypes("client_credentials")
            .secret("password")
            .and()
            .withClient("web")
            .redirectUris("http://github.com/techdev-solutions/")
            .resourceIds("reee")
            .scopes("read")
            .authorizedGrantTypes("implicit");
    // @formatter:on
}
}
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter
{
@Autowired
private DataSource dataSource;

@Override
protected void configure(HttpSecurity http) throws Exception
{
    http.authorizeRequests().antMatchers("/**").authenticated()
            .and().httpBasic().realmName("OAuth Server");
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception
{
    PasswordEncoder encoder = new BCryptPasswordEncoder();

    auth.userDetailsService(userDetailsService()).passwordEncoder(encoder);
    auth.jdbcAuthentication().dataSource(dataSource);
}

@Bean
public UserDetailsService userDetailsService()
{
    return new CustomUserDetailsService(dataSource);
}
}

我认为我们有类似的问题,因为我已经打开了一个问答


您可以通过在请求标头和web add client.inMemory()的客户端配置中添加用户名和密码来避免用户名和密码框。但是,服务器将使用重定向url进行响应。我的问题是,我不希望它重定向,我只希望在响应中使用令牌。

您所说的“它在数据库中注册用户”curl“是什么意思…”在OAUTH_ACCESS_令牌表中,创建了新令牌,客户端id为“curl”,用户名为空。但通过“web”客户端,它会注册我的用户名'bla@gmail.com'但如果我想授权该用户访问受保护的资源,则会有一个确认对话框。如何避免这种对话?