代码之家  ›  专栏  ›  技术社区  ›  Factor Three

Micronaut security无法“保护”

  •  0
  • Factor Three  · 技术社区  · 8 年前

    我有一个简单的基于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服务的访问。我不知道怎样才能使访问失败。

    有人能在这件事上帮忙吗?非常感谢。

    1 回复  |  直到 8 年前
        1
  •  2
  •   cementovoz    8 年前

    当您不能像往常一样访问资源或处理请求时,您需要更改筛选器中的请求。您的HelloFilter如下所示:

    @Override
    public Publisher<MutableHttpResponse<?>> doFilter(HttpRequest<?> theReq, ServerFilterChain theChain) {
        System.out.println("Filtering!");
        Publisher<MutableHttpResponse<?>> resp = secService.checkAuthorization(theReq)
                .switchMap((authResult) -> { // authResult - is you result from SecurityService
                    if (!authResult) {
                        return Publishers.just(HttpResponse.status(HttpStatus.FORBIDDEN)); // reject request
                    } else {
                        return theChain.proceed(theReq); // process request as usual
                    }
                })
                .doOnNext(res -> {
                    System.out.println("Responding!");
                });
    
        return (resp);
    }
    

    最后,micronaut有一个带有SecurityFilter的安全模块,您可以使用@Secured注解或在配置文件中写入访问规则 more examples in the doc