我正在使用 Spring Boot 2.2.0azure-active-directory-b2c-spring-boot-starter
2.2.0。我设法用它来保护 Thymeleaf 网页(按照他们的教程)。现在,我想要一个以相同方式保护的 REST API,因为实际应用程序将是一个对我的 Spring Boot 后端进行 REST 调用的移动应用程序。
我已经弄清楚如何使用密码授予流程获取令牌:
POST https://<my-tenant-id>.b2clogin.com/<my-tenant-id.onmicrosoft.com/oauth2/v2.0/token?p=B2C_1_<my-custom-policy>
(以用户名和密码作为参数)
因此移动应用程序可以使用该调用。但是我应该如何配置我的 Spring Boot 应用程序以便使用Authorization: Bearer <access-token>
API 调用有效吗?我需要哪些依赖项/启动器以及应该如何配置?
UPDATE:
我尝试添加:
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.3.7.RELEASE</version>
</dependency>
With:
@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。
一旦你知道了,解决方案似乎很简单。
首先,添加以下依赖项:
<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>
接下来指定spring.security.oauth2.resourceserver.jwt.jwk-set-uri
您的财产application.properties
file.
要知道这个值,请执行GET
(使用 cURL 或其他工具)https://<my-tenant-id>.b2clogin.com/<my-tenant-id>.onmicrosoft.com/v2.0/.well-known/openid-configuration?p=B2C_1_<my-custom-policy>
这将返回一个 JSON 正文jwks_uri
价值。获取该值并将其放入您的application.properties
file.
现在在项目中创建这个 Java 类:
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;
}
}
如果您执行 GET 操作,您将看到主体不为空api/test
与一个正确的Authorization
标头(它的类型是org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken
)
唯一遗憾的是校长没有权力,我还需要弄清楚为什么。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)