我有一个简单的基于Micronaut的“hello world”服务,它内置了一个简单的安全性(为了测试和说明Micronaut的安全性)。下面提供了实现hello服务的服务中的控制器代码:
@Controller("/hello")
public class HelloController
{
public HelloController()
{
// Might put some stuff in in the future
}
@Get("/")
@Produces(MediaType.TEXT_PLAIN)
public String index()
{
return("Hello to the World of Micronaut!!!");
}
}
为了测试安全机制,我按照Micronaut教程的说明创建了一个安全服务类:
@Singleton
public class SecurityService
{
public SecurityService()
{
// Might put in some stuff in the future
}
Flowable<Boolean> checkAuthorization(HttpRequest<?> theReq)
{
Flowable<Boolean> flow = Flowable.fromCallable(()->{
System.out.println("Security Engaged!");
return(false); <== The tutorial says return true
}).subscribeOn(Schedulers.io());
return(flow);
}
}
应该注意的是,与本教程不同的是flowable.fromCallable可调用()lambda返回false。在教程中,它返回true。我假设如果返回false,安全检查将失败,并且失败将导致hello服务无法响应。
@Filter("/**")
public class HelloFilter implements HttpServerFilter
{
private final SecurityService secService;
public HelloFilter(SecurityService aSec)
{
System.out.println("Filter Created!");
secService = aSec;
}
@Override
public Publisher<MutableHttpResponse<?>> doFilter(HttpRequest<?> theReq, ServerFilterChain theChain)
{
System.out.println("Filtering!");
Publisher<MutableHttpResponse<?>> resp = secService.checkAuthorization(theReq)
.doOnNext(res->{
System.out.println("Responding!");
});
return(resp);
}
}
当我运行microservice并访问Helo world URL时,问题就出现了。(
http://localhost:8080/hello
)我不能导致对服务的访问失败。过滤器捕获所有请求,并且安全对象被占用,但是它似乎并不阻止对hello服务的访问。我不知道怎样才能使访问失败。
有人能在这件事上帮忙吗?非常感谢。