我正在使用Spring Boot 2.2.0版本和azure-active-directory-b2c-spring-boot-starter
2.2.0版本。 我已经按照其教程保护了一个Thymeleaf网页。 现在,我想要一个REST API以相同的方式进行安全保护,因为实际应用程序将是一个调用我Spring Boot后端的移动应用程序。
我已经弄清楚如何使用密码授权流获得令牌:
POST https://<my-tenant-id>.b2clogin.com/<my-tenant-id.onmicrosoft.com/oauth2/v2.0/token?p=B2C_1_<my-custom-policy>
(使用用户名和密码作为参数)
因此,移动应用程序可以使用该调用。但是,我应该如何配置我的Spring Boot应用程序,以便在API调用上使用Authorization:Bearer&lt;access-token>
工作?我需要什么依赖项/启动器,并且应如何配置这些内容?
更新:
我尝试添加:
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.3.7.RELEASE</version>
</dependency>
使用:
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class OAuth2ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
resources.resourceId("my-azure-b2c-test");
}
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/api/**")
.authenticated();
}
}
但是,当我在我的Spring Boot应用程序上发出请求时,我遇到了带有“invalid_token”错误的401。