How to activate Spring Security in a webflux war application

前端 未结 1 2030
遥遥无期
遥遥无期 2021-01-15 14:44

How to activate a webflux security in a war packaging application. I am using the Spring 5 built-in AbstractAnnotationConfigDispatcherHan

相关标签:
1条回答
  • 2021-01-15 15:10

    You should be able to just use AbstractAnnotationConfigDispatcherHandlerInitializer. However, there is a bug in AbstractDispatcherHandlerInitializer. You can work around the problem using:

    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);
        }
    }
    

    In Spring 5.0.2.RELEASE+ (scheduled for release Nov 15, 2017) you can extend AbstractReactiveWebInitializer instead.

    public class AppIntializer extends AbstractReactiveWebInitializer {
    
        @Override
        protected Class<?>[] getConfigClasses() {
            return new Class[]{
                WebConfig.class,
                SecurityConfig.class
            };
        }
    }
    
    0 讨论(0)
提交回复
热议问题