|
24 | 24 | import jakarta.servlet.http.HttpServletRequest;
|
25 | 25 | import org.apache.commons.logging.Log;
|
26 | 26 | import org.apache.commons.logging.LogFactory;
|
| 27 | +import reactor.util.annotation.NonNull; |
27 | 28 |
|
| 29 | +import org.springframework.beans.BeansException; |
| 30 | +import org.springframework.beans.factory.BeanFactory; |
| 31 | +import org.springframework.beans.factory.BeanFactoryAware; |
| 32 | +import org.springframework.beans.factory.BeanNameAware; |
| 33 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 34 | +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
28 | 35 | import org.springframework.core.log.LogMessage;
|
29 | 36 | import org.springframework.security.web.util.matcher.RequestMatcher;
|
30 | 37 | import org.springframework.util.StringUtils;
|
|
36 | 43 | * @author Jinwoo Bae
|
37 | 44 | * @since 3.1
|
38 | 45 | */
|
39 |
| -public final class DefaultSecurityFilterChain implements SecurityFilterChain { |
| 46 | +public final class DefaultSecurityFilterChain implements SecurityFilterChain, BeanNameAware, BeanFactoryAware { |
40 | 47 |
|
41 | 48 | private static final Log logger = LogFactory.getLog(DefaultSecurityFilterChain.class);
|
42 | 49 |
|
43 | 50 | private final RequestMatcher requestMatcher;
|
44 | 51 |
|
45 | 52 | private final List<Filter> filters;
|
46 | 53 |
|
| 54 | + private String beanName; |
| 55 | + |
| 56 | + private ConfigurableListableBeanFactory beanFactory; |
| 57 | + |
47 | 58 | public DefaultSecurityFilterChain(RequestMatcher requestMatcher, Filter... filters) {
|
48 | 59 | this(requestMatcher, Arrays.asList(filters));
|
49 | 60 | }
|
@@ -80,8 +91,38 @@ public boolean matches(HttpServletRequest request) {
|
80 | 91 |
|
81 | 92 | @Override
|
82 | 93 | public String toString() {
|
83 |
| - return this.getClass().getSimpleName() + " [RequestMatcher=" + this.requestMatcher + ", Filters=" + this.filters |
84 |
| - + "]"; |
| 94 | + List<String> filterNames = new ArrayList<>(); |
| 95 | + for (Filter filter : this.filters) { |
| 96 | + String name = filter.getClass().getSimpleName(); |
| 97 | + if (name.endsWith("Filter")) { |
| 98 | + name = name.substring(0, name.length() - "Filter".length()); |
| 99 | + } |
| 100 | + filterNames.add(name); |
| 101 | + } |
| 102 | + String declaration = this.getClass().getSimpleName(); |
| 103 | + if (this.beanName != null) { |
| 104 | + declaration += " defined as '" + this.beanName + "'"; |
| 105 | + if (this.beanFactory != null) { |
| 106 | + BeanDefinition bd = this.beanFactory.getBeanDefinition(this.beanName); |
| 107 | + String description = bd.getResourceDescription(); |
| 108 | + if (description != null) { |
| 109 | + declaration += " in [" + description + "]"; |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + return declaration + " matching [" + this.requestMatcher + "] and having filters " + filterNames; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public void setBeanName(@NonNull String name) { |
| 118 | + this.beanName = name; |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public void setBeanFactory(BeanFactory beanFactory) throws BeansException { |
| 123 | + if (beanFactory instanceof ConfigurableListableBeanFactory listable) { |
| 124 | + this.beanFactory = listable; |
| 125 | + } |
85 | 126 | }
|
86 | 127 |
|
87 | 128 | }
|
0 commit comments