如何安全授权服务器内的API端点

时间:2019-02-02 19:40:17

标签: spring-boot spring-security oauth-2.0

在我的应用程序中,我正在使用Spring Security OAuth2提供基于JWT令牌的身份验证。默认情况下,/oauth/token端点使用client-idclient-secret的基本HTTP身份验证进行保护。该端点已按预期完全正常工作。

但是,当我将新端点添加到授权服务器时,我发现它根本没有受到保护。换句话说,我可以在不提供基本HTTP身份验证或不提供任何JWT Bearer令牌的情况下调用端点。

我尝试在与以下项目相同的扩展@EnableResourceServer的新类上添加ResourceServerConfigurerAdapter,但是没有用。我仍然可以打电话给我新的端点,而无需先进行身份验证。

@EnableResourceServer
@Configuration
public class ResourcesServerConfiguration extends ResourceServerConfigurerAdapter {
    @Autowired
    private JwtAccessTokenConverter jwtAccessTokenConverter;

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception{
        resources.tokenStore(new JwtTokenStore(jwtAccessTokenConverter));
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .anyRequest().authenticated();
    }
}

我还尝试在扩展WebSecurityConfigurerAdapter的类上添加@EnableWebSecurity,如下所示,但效果不佳。在这种情况下,我根本无法访问新端点。无论我提供基本HTTP身份验证还是有效的JWT承载令牌,我都将始终获得401 Unauthorized

@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().authenticated();
    }
}

如果您能指出正确的方向,我将不胜感激。

0 个答案:

没有答案
相关问题