![](/img/trans.png)
[英]How to pass the value @Body MultipartBody from curl or postman or swagger Micronaut
[英]How to pass the value to the AuthenticationProvider from HttpRequest in Micronaut java with Jnunit 5
使用 Micronaut HttpClient 执行 Junit 5 测试中的 HTTP 调用。
我正在尝试使用 HttpRequest 将值传递给 AuthenticationProvider,如下所示
@MicronautTest
public class ProductCreateTest extends TestContainerFixture {
@Inject
@Client("/")
HttpClient client;
@Test
@DisplayName("Should create the product")
void shouldCreateTheProduct() {
HttpRequest request = HttpRequest.POST("/product", new ProductModel())
.bearerAuth(bearerAccessRefreshToken.getAccessToken());
request.setAttribute("fb_product", "owner");
HttpResponse < ProductModel > rsp = client.toBlocking().exchange(request, ProductModel.class);
var item = rsp.body();
}
}
这里我将属性设置为request.setAttribute("fb_product", "owner");
在身份验证提供程序中,我正在尝试访问如下属性
@Singleton
@Requires(env = Environment.TEST)
public record AuthenticationProviderFixture(Configuration configuration) implements AuthenticationProvider {
@Override
public Publisher<AuthenticationResponse> authenticate(HttpRequest<?> httpRequest, AuthenticationRequest<?, ?> authenticationRequest) {
return Flux.create(emitter -> {
if (authenticationRequest.getIdentity().equals(configuration.Username()) && authenticationRequest.getSecret().equals(configuration.Password())) {
var attributeValue = httpRequest.getAttribute("fb_product");
HashMap<String, Object> attributes = new HashMap<>();
emitter.next(AuthenticationResponse.success((String) authenticationRequest.getIdentity(), attributes));
emitter.complete();
} else {
emitter.error(AuthenticationResponse.exception());
}
}, FluxSink.OverflowStrategy.ERROR);
}
}
该属性未映射,这提供了一个 null 值var attributeValue = httpRequest.getAttribute("fb_product");
将数据从 HttpRequest 传递到 AuthenticationProvider 的最佳方法是什么
有一个token generator的概念,但是在security rule上有token generator,认证是null。
注入TokenGenerator
并创建令牌
Map<String, Object> claims = new HashMap<>();
claims.put("fb_product","owner");
var claimGenerator = tokenGenerator.generateToken(claims);
要将 header 添加到请求中:
HttpRequest request = HttpRequest.POST("/product", new ProductModel())
.bearerAuth(bearerAccessRefreshToken.getAccessToken())
.header("fb_product", "owner");
要检索 header:
Optional<String> fbOwner = request.getHeaders().findFirst("fb_owner");
您没有在 HTTP 客户端请求中设置属性。
你想达到什么目的? 模拟正在登录的用户?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.