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

时间:2015-01-11 16:40:28

标签: angularjs spring security oauth spring-security

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

如果我使用:

curl curl:password @ localhost:8081 / oauth / token \?grant_type = client_credentials 它返回我的令牌,但问题是它在DB中注册用户“curl”所以...不太好......

如果我使用:

http://www.example.com:8081/oauth/authorize?client_id=web&response_type=token 它会提示用户名和密码对话框,我输入它然后它会问我“您是否授权'web'访问受保护的资源?

scope.read:批准拒绝“

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

我应该使用这个方案吗? https://github.com/spring-projects/spring-security-oauth/blob/master/spring-security-oauth2/src/test/resources/schema.sql

使用此配置 - > 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
}
}

SecurityConfiguration

@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);
}
}

1 个答案:

答案 0 :(得分:1)

我认为我们有类似的问题,因为我已经打开了q&一个what are the java* configuration for oauth2 to return token after authentication

您可以通过在请求http://localhost:8081/oauth/authorize?client_id=web&response_type=token的标头中添加用户名和密码以及在web add client.inMemory()。autoApprove(true)的客户端配置中添加用户名和密码来避免用户名和密码,但是服务器将响应重定向网址。我的问题是我不希望它重定向我只想在响应中使用令牌..

相关问题