Java 如何使用Azure AD B2C保护Spring Boot REST API?
我将SpringBoot 2.2.0与azure-active-directory-b2c-Spring-Boot-starter一起使用2.2.0。我设法用它来保护一个Thymeleaf网页(遵循他们的教程)。现在,我想要一个RESTAPI,它以同样的方式受到保护,因为实际的应用程序将是一个移动应用程序,它对我的Spring Boot后端进行REST调用 我已经了解了如何使用密码授予流获取令牌,方法如下:Java 如何使用Azure AD B2C保护Spring Boot REST API?,java,spring-boot,spring-security,azure-ad-b2c,Java,Spring Boot,Spring Security,Azure Ad B2c,我将SpringBoot 2.2.0与azure-active-directory-b2c-Spring-Boot-starter一起使用2.2.0。我设法用它来保护一个Thymeleaf网页(遵循他们的教程)。现在,我想要一个RESTAPI,它以同样的方式受到保护,因为实际的应用程序将是一个移动应用程序,它对我的Spring Boot后端进行REST调用 我已经了解了如何使用密码授予流获取令牌,方法如下: POST https://<my-tenant-id>.b2clogin.c
POST https://<my-tenant-id>.b2clogin.com/<my-tenant-id.onmicrosoft.com/oauth2/v2.0/token?p=B2C_1_<my-custom-policy>
但当我在我的Spring Boot应用程序上执行请求时,我得到一个带有“无效令牌”错误的401。一旦知道,解决方案似乎非常简单 首先,添加以下依赖项:
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-resource-server</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-jose</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
如果您现在有这样的控制器:
@RestController
public class ApiController {
@GetMapping("/api/test")
public String apiTest(@AuthenticationPrincipal Principal principal) {
return "test " + principal;
}
}
如果使用正确的Authorization
头(类型为org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken
)进行GET-onapi/test
,您将看到主体不是空的
唯一不幸的是,委托人没有权限,我仍然需要找出原因。您尝试过什么吗?您必须检查Spring Boot应用程序中传入请求标头中包含的JWT令牌(对于每个传入请求).@ieggel你能详细解释一下应该怎么做吗?@Michael我用我所掌握的更新了这个问题tried@WimDeblauwe我没有Azure方面的经验,但我之前提到的是它通常是如何实现的。在我看来,您正在使用的库已经可以为您处理JWT验证,并将其集成到spring安全上下文中。这里似乎有一些例子:
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-resource-server</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-jose</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configurers.oauth2.server.resource.OAuth2ResourceServerConfigurer;
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/api/**")
.authenticated()
.and()
.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt);
}
}
@RestController
public class ApiController {
@GetMapping("/api/test")
public String apiTest(@AuthenticationPrincipal Principal principal) {
return "test " + principal;
}
}