/** * Calls the {@link #registerHandlers} method in addition to the * superclass's initialization. */ @Override public void initApplicationContext() throws BeansException { super.initApplicationContext(); registerHandlers(this.urlMap); }
/** * Register the specified handler for the given URL paths. * @param urlPaths the URLs that the bean should be mapped to * @param beanName the name of the handler bean * @throws BeansException if the handler couldn't be registered * @throws IllegalStateException if there is a conflicting handler registered */ protected void registerHandler(String[] urlPaths, String beanName) throws BeansException, IllegalStateException { Assert.notNull(urlPaths, "URL path array must not be null"); for (String urlPath : urlPaths) { registerHandler(urlPath, beanName); } }
@Bean @Order(0) public WebExceptionHandler responseStatusExceptionHandler() { return new WebFluxResponseStatusExceptionHandler(); }
@Bean public SimpleUrlHandlerMapping simpleUrlHandlerMapping() { ResourceWebHandler handler = new ResourceWebHandler(); HashMap<String, ResourceWebHandler> handlerMap = new HashMap<>(); handlerMap.put("/resources/**", handler); SimpleUrlHandlerMapping hm = new SimpleUrlHandlerMapping(); hm.setUrlMap(handlerMap); return hm; }
@Bean @SuppressWarnings("unused") public SimpleUrlHandlerMapping handlerMapping() { SimpleUrlHandlerMapping hm = new SimpleUrlHandlerMapping(); hm.registerHandler("/welcome*", otherController()); hm.registerHandler("/welcome.html", mainController()); hm.registerHandler("/show.html", mainController()); hm.registerHandler("/bookseats.html", mainController()); return hm; }
@Test public void actualRequestWithCorsConfigurationSource() throws Exception { this.handlerMapping.setCorsConfigurationSource(new CustomCorsConfigurationSource()); String origin = "http://domain2.com"; ServerWebExchange exchange = createExchange(HttpMethod.GET, "/welcome.html", origin); Object actual = this.handlerMapping.getHandler(exchange).block(); assertNotNull(actual); assertSame(this.welcomeController, actual); assertEquals("http://domain2.com", exchange.getResponse().getHeaders() .getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)); assertEquals("true", exchange.getResponse().getHeaders() .getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS)); }
@Override protected CorsConfiguration getCorsConfiguration(Object handler, ServerWebExchange exchange) { CorsConfiguration corsConfig = super.getCorsConfiguration(handler, exchange); if (handler instanceof HandlerMethod) { HandlerMethod handlerMethod = (HandlerMethod) handler; if (handlerMethod.equals(PREFLIGHT_AMBIGUOUS_MATCH)) { return ALLOW_CORS_CONFIG; } CorsConfiguration methodConfig = this.mappingRegistry.getCorsConfiguration(handlerMethod); corsConfig = (corsConfig != null ? corsConfig.combine(methodConfig) : methodConfig); } return corsConfig; }
@Test public void actualRequestWithoutCorsConfigurationProvider() throws Exception { String origin = "http://domain2.com"; ServerWebExchange exchange = createExchange(HttpMethod.GET, "/welcome.html", origin); Object actual = this.handlerMapping.getHandler(exchange).block(); assertNotNull(actual); assertSame(this.welcomeController, actual); }
private Object handleMatch(Object handler, PathPattern bestMatch, PathContainer pathWithinMapping, ServerWebExchange exchange) { // Bean name or resolved handler? if (handler instanceof String) { String handlerName = (String) handler; handler = obtainApplicationContext().getBean(handlerName); } validateHandler(handler, exchange); exchange.getAttributes().put(BEST_MATCHING_HANDLER_ATTRIBUTE, handler); exchange.getAttributes().put(BEST_MATCHING_PATTERN_ATTRIBUTE, bestMatch); exchange.getAttributes().put(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, pathWithinMapping); return handler; }
@Test public void resourceHandler() throws Exception { ApplicationContext context = loadConfig(CustomResourceHandlingConfig.class); String name = "resourceHandlerMapping"; AbstractUrlHandlerMapping handlerMapping = context.getBean(name, AbstractUrlHandlerMapping.class); assertNotNull(handlerMapping); assertEquals(Ordered.LOWEST_PRECEDENCE - 1, handlerMapping.getOrder()); SimpleUrlHandlerMapping urlHandlerMapping = (SimpleUrlHandlerMapping) handlerMapping; WebHandler webHandler = (WebHandler) urlHandlerMapping.getUrlMap().get("/images/**"); assertNotNull(webHandler); }
@Test @SuppressWarnings("resource") public void handlerMappingJavaConfig() throws Exception { AnnotationConfigApplicationContext wac = new AnnotationConfigApplicationContext(); wac.register(WebConfig.class); wac.refresh(); HandlerMapping handlerMapping = (HandlerMapping) wac.getBean("handlerMapping"); Object mainController = wac.getBean("mainController"); Object otherController = wac.getBean("otherController"); testUrl("/welcome.html", mainController, handlerMapping, ""); testUrl("/welcome.x", otherController, handlerMapping, "welcome.x"); testUrl("/welcome/", otherController, handlerMapping, "welcome"); testUrl("/show.html", mainController, handlerMapping, ""); testUrl("/bookseats.html", mainController, handlerMapping, ""); }
private ResourceWebHandler getHandler(String pathPattern) { SimpleUrlHandlerMapping mapping = (SimpleUrlHandlerMapping) this.registry.getHandlerMapping(); return (ResourceWebHandler) mapping.getUrlMap().get(pathPattern); }
@Test public void preFlightRequestWithCorsConfigurationSource() throws Exception { this.handlerMapping.setCorsConfigurationSource(new CustomCorsConfigurationSource()); String origin = "http://domain2.com"; ServerWebExchange exchange = createExchange(HttpMethod.OPTIONS, "/welcome.html", origin); Object actual = this.handlerMapping.getHandler(exchange).block(); assertNotNull(actual); assertNotSame(this.welcomeController, actual); assertEquals("http://domain2.com", exchange.getResponse().getHeaders() .getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)); assertEquals("true", exchange.getResponse().getHeaders() .getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS)); }
@Before public void setup() { this.handlerMapping = new AbstractUrlHandlerMapping() {}; this.handlerMapping.registerHandler("/welcome.html", this.welcomeController); this.handlerMapping.registerHandler("/cors.html", this.corsController); }
@Test public void preflightRequestWithoutCorsConfigurationProvider() throws Exception { String origin = "http://domain2.com"; ServerWebExchange exchange = createExchange(HttpMethod.OPTIONS, "/welcome.html", origin); Object actual = this.handlerMapping.getHandler(exchange).block(); assertNotNull(actual); assertNotSame(this.welcomeController, actual); assertNull(exchange.getResponse().getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)); }
@Override protected ResponseStatusExceptionHandler createResponseStatusExceptionHandler() { return new WebFluxResponseStatusExceptionHandler(); }
@Test public void actualRequestWithCorsAwareHandler() throws Exception { String origin = "http://domain2.com"; ServerWebExchange exchange = createExchange(HttpMethod.GET, "/cors.html", origin); Object actual = this.handlerMapping.getHandler(exchange).block(); assertNotNull(actual); assertSame(this.corsController, actual); assertEquals("*", exchange.getResponse().getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)); }
public void defaultConfiguration() { this.codecConfigurer.registerDefaults(true); this.exceptionHandlers.add(new WebFluxResponseStatusExceptionHandler()); this.localeContextResolver = new AcceptHeaderLocaleContextResolver(); }
@Test public void preFlightWithCorsAwareHandler() throws Exception { String origin = "http://domain2.com"; ServerWebExchange exchange = createExchange(HttpMethod.OPTIONS, "/cors.html", origin); Object actual = this.handlerMapping.getHandler(exchange).block(); assertNotNull(actual); assertNotSame(this.corsController, actual); assertEquals("*", exchange.getResponse().getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)); }