如何在webflux war应用程序中激活Spring Security

时间:2017-09-20 15:05:46

标签: spring-security spring-webflux

如何在 war packaging 应用程序中激活webflux安全性。我使用的是Spring 5内置AbstractAnnotationConfigDispatcherHandlerInitializer,但它不起作用。

public class AppIntializer extends AbstractAnnotationConfigDispatcherHandlerInitializer {

    @Override
    protected Class<?>[] getConfigClasses() {
        return new Class[]{
            WebConfig.class,
            SecurityConfig.class
        };
    }
}

但似乎没有启用Spring Security WebFilter。 Spring Security 5不包含特定于Reactor的WebApplicationInitializer来激活Spring安全过滤器链。

Stack:Spring Security 5.0.0.M4

源代码:https://github.com/hantsy/spring-reactive-sample/blob/master/war/src/main/java/com/example/demo/AppIntializer.java

1 个答案:

答案 0 :(得分:0)

您应该可以使用AbstractAnnotationConfigDispatcherHandlerInitializer。但是,AbstractDispatcherHandlerInitializer中有bug。您可以使用以下方法解决问题:

public class AppInitializer extends AbstractAnnotationConfigDispatcherHandlerInitializer {

    @Override
    protected Class<?>[] getConfigClasses() {
        return new Class[]{
            WebConfig.class,
            SecurityConfig.class
        };
    }

    protected void registerDispatcherHandler(ServletContext servletContext) {
        String servletName = getServletName();
        ApplicationContext applicationContext = createApplicationContext();

        refreshApplicationContext(applicationContext);
        registerCloseListener(servletContext, applicationContext);

        HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(applicationContext)
            .build();
        ServletHttpHandlerAdapter handlerAdapter = new ServletHttpHandlerAdapter(httpHandler);

        ServletRegistration.Dynamic registration = servletContext.addServlet(servletName, handlerAdapter);

        registration.setLoadOnStartup(1);
        registration.addMapping(getServletMapping());
        registration.setAsyncSupported(true);

        customizeRegistration(registration);
    }
}

在Spring 5.0.2.RELEASE +(计划于2017年11月15日发布)中,您可以扩展AbstractReactiveWebInitializer

public class AppIntializer extends AbstractReactiveWebInitializer {

    @Override
    protected Class<?>[] getConfigClasses() {
        return new Class[]{
            WebConfig.class,
            SecurityConfig.class
        };
    }
}