1
1
/*
2
- * Copyright 2002-2024 the original author or authors.
2
+ * Copyright 2002-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -292,6 +292,23 @@ void getCorsConfigWithBeanNameHandler() throws Exception {
292
292
HandlerMethod handlerMethod = this .mapping .getHandlerInternal (new MockHttpServletRequest ("GET" , key ));
293
293
}
294
294
295
+ @ Test
296
+ void registerCustomHandlerMethod () throws Exception {
297
+ this .mapping .setCustomerHandlerMethod (true );
298
+ this .mapping .registerMapping ("/foo" , this .handler , this .handler .getClass ().getMethod ("corsHandlerMethod" ));
299
+
300
+ MockHttpServletRequest request = new MockHttpServletRequest ("OPTIONS" , "/foo" );
301
+ request .addParameter ("abort" , "true" );
302
+ request .addHeader (HttpHeaders .ORIGIN , "https://domain.com" );
303
+ request .addHeader (HttpHeaders .ACCESS_CONTROL_REQUEST_METHOD , "GET" );
304
+
305
+ MockHttpServletResponse response = new MockHttpServletResponse ();
306
+
307
+ HandlerExecutionChain chain = this .mapping .getHandler (request );
308
+
309
+ assertThat (chain ).isNotNull ();
310
+ assertThat (response .getStatus ()).isEqualTo (200 );
311
+ }
295
312
296
313
297
314
private static class MyHandlerMethodMapping extends AbstractHandlerMethodMapping <String > {
@@ -302,6 +319,8 @@ private static class MyHandlerMethodMapping extends AbstractHandlerMethodMapping
302
319
303
320
private final List <String > matches = new ArrayList <>();
304
321
322
+ private boolean customerHandlerMethod ;
323
+
305
324
public MyHandlerMethodMapping () {
306
325
setHandlerMethodMappingNamingStrategy (new SimpleMappingNamingStrategy ());
307
326
}
@@ -326,6 +345,16 @@ protected String getMappingForMethod(Method method, Class<?> handlerType) {
326
345
return methodName .startsWith ("handler" ) ? methodName : null ;
327
346
}
328
347
348
+ public void setCustomerHandlerMethod (boolean customerHandlerMethod ) {
349
+ this .customerHandlerMethod = customerHandlerMethod ;
350
+ }
351
+
352
+ @ Override
353
+ protected HandlerMethod createHandlerMethod (Object handler , Method method ) {
354
+ return (this .customerHandlerMethod ?
355
+ new CustomHandlerMethod (handler , method ) : super .createHandlerMethod (handler , method ));
356
+ }
357
+
329
358
@ Override
330
359
protected CorsConfiguration initCorsConfiguration (Object handler , Method method , String mapping ) {
331
360
CrossOrigin crossOrigin = AnnotatedElementUtils .findMergedAnnotation (method , CrossOrigin .class );
@@ -355,6 +384,7 @@ protected Comparator<String> getMappingComparator(HttpServletRequest request) {
355
384
356
385
}
357
386
387
+
358
388
private static class SimpleMappingNamingStrategy implements HandlerMethodMappingNamingStrategy <String > {
359
389
360
390
@ Override
@@ -363,6 +393,16 @@ public String getName(HandlerMethod handlerMethod, String mapping) {
363
393
}
364
394
}
365
395
396
+
397
+ private static class CustomHandlerMethod extends HandlerMethod {
398
+
399
+ public CustomHandlerMethod (Object bean , Method method ) {
400
+ super (bean , method );
401
+ }
402
+
403
+ }
404
+
405
+
366
406
@ Controller
367
407
static class MyHandler {
368
408
0 commit comments