Spring oauth2 - 无法登录/ oauth / authorization

时间:2017-04-03 11:13:58

标签: spring spring-security oauth

我正在尝试使用OAUTH2保护我的REST应用程序,在这种情况下使用授权代码授予。所以基本上我认为在尝试访问/ oauth / authorize端点时,我应该获得一个登录页面(来自我的应用程序),用户应该使用我的应用程序进行身份验证,以使客户端应用程序获取redirect_uri中的令牌。这是对的吗?

我的客户端在数据库中,我创建了Spring Security所需的所有表。在这种情况下,我有client_id = testauthorized_grant_types "access_token,refresh_token,implicit,authorization_code" 这是我的AuthorizationServer配置

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    DataSource dataSource;

    @Autowired
    @Qualifier("authenticationManagerBean")
    private AuthenticationManager authManager;

   @Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
    oauthServer.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()");
}

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.jdbc(dataSource);
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {

        endpoints.tokenStore(tokenStore())
        .authenticationManager(authManager);

    }

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

}

资源服务器(在同一个应用程序中)是

@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class ResourceServerConfig extends ResourceServerConfigurerAdapter{

    @Autowired
    DataSource dataSource;

     @Override
        public void configure(HttpSecurity http) throws Exception {

            http    
                    .authorizeRequests()
                    .antMatchers("/api/**").access("hasRole('USER')");

        }

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

     @Override
        public void configure(ResourceServerSecurityConfigurer resources) throws Exception {

            resources.tokenStore(tokenStore());
        }
}

最后,我的一般SecurityConfiguration设置应用程序的Web应用程序部分的安全级别,而不是“/ api / **”部分。我正在尝试使用PostMan向http://localhost:8080/oauth/authorize发送带有以下参数的帖子请求:client_id = test redirect_uri = http://test.com response_type = code但是我收到此错误

User must be authenticated with Spring Security before authorization can be completed.

我想我应该得到一个用户应该进行身份验证的登录窗口 我做错了什么?

0 个答案:

没有答案