|
26 | 26 | import javax.servlet.http.HttpServletRequest; |
27 | 27 |
|
28 | 28 | import org.springframework.beans.factory.ObjectProvider; |
29 | | -import org.springframework.beans.factory.annotation.Autowired; |
30 | 29 | import org.springframework.boot.actuate.endpoint.Endpoint; |
31 | 30 | import org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping; |
32 | 31 | import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint; |
|
42 | 41 | import org.springframework.boot.autoconfigure.condition.SpringBootCondition; |
43 | 42 | import org.springframework.boot.autoconfigure.security.AuthenticationManagerConfiguration; |
44 | 43 | import org.springframework.boot.autoconfigure.security.FallbackWebSecurityAutoConfiguration; |
| 44 | +import org.springframework.boot.autoconfigure.security.IgnoredRequestCustomizer; |
45 | 45 | import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration; |
46 | 46 | import org.springframework.boot.autoconfigure.security.SecurityPrerequisite; |
47 | 47 | import org.springframework.boot.autoconfigure.security.SecurityProperties; |
48 | 48 | import org.springframework.boot.autoconfigure.security.SpringBootWebSecurityConfiguration; |
49 | | -import org.springframework.boot.autoconfigure.web.ErrorController; |
50 | 49 | import org.springframework.boot.autoconfigure.web.ServerProperties; |
51 | 50 | import org.springframework.boot.context.properties.EnableConfigurationProperties; |
52 | 51 | import org.springframework.context.ApplicationContext; |
|
56 | 55 | import org.springframework.context.annotation.Configuration; |
57 | 56 | import org.springframework.core.annotation.Order; |
58 | 57 | import org.springframework.core.type.AnnotatedTypeMetadata; |
59 | | -import org.springframework.security.config.annotation.web.WebSecurityConfigurer; |
60 | 58 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
61 | | -import org.springframework.security.config.annotation.web.builders.WebSecurity; |
62 | 59 | import org.springframework.security.config.annotation.web.builders.WebSecurity.IgnoredRequestConfigurer; |
63 | 60 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
64 | 61 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration; |
|
72 | 69 | import org.springframework.security.web.util.matcher.NegatedRequestMatcher; |
73 | 70 | import org.springframework.security.web.util.matcher.OrRequestMatcher; |
74 | 71 | import org.springframework.security.web.util.matcher.RequestMatcher; |
75 | | -import org.springframework.util.ObjectUtils; |
76 | 72 | import org.springframework.util.StringUtils; |
77 | 73 |
|
78 | 74 | /** |
@@ -102,9 +98,34 @@ public class ManagementWebSecurityAutoConfiguration { |
102 | 98 | AnyRequestMatcher.INSTANCE); |
103 | 99 |
|
104 | 100 | @Bean |
105 | | - @ConditionalOnMissingBean({ IgnoredPathsWebSecurityConfigurerAdapter.class }) |
106 | | - public IgnoredPathsWebSecurityConfigurerAdapter ignoredPathsWebSecurityConfigurerAdapter() { |
107 | | - return new IgnoredPathsWebSecurityConfigurerAdapter(); |
| 101 | + public IgnoredRequestCustomizer managementIgnoredRequestCustomizer( |
| 102 | + ManagementServerProperties management, |
| 103 | + ObjectProvider<ManagementContextResolver> contextResolverProvider) { |
| 104 | + return new ManagementIgnoredRequestCustomizer(management, |
| 105 | + contextResolverProvider.getIfAvailable()); |
| 106 | + } |
| 107 | + |
| 108 | + private class ManagementIgnoredRequestCustomizer implements IgnoredRequestCustomizer { |
| 109 | + |
| 110 | + private final ManagementServerProperties management; |
| 111 | + |
| 112 | + private final ManagementContextResolver contextResolver; |
| 113 | + |
| 114 | + ManagementIgnoredRequestCustomizer(ManagementServerProperties management, |
| 115 | + ManagementContextResolver contextResolver) { |
| 116 | + this.management = management; |
| 117 | + this.contextResolver = contextResolver; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void customize(IgnoredRequestConfigurer configurer) { |
| 122 | + if (!this.management.getSecurity().isEnabled()) { |
| 123 | + RequestMatcher requestMatcher = LazyEndpointPathRequestMatcher |
| 124 | + .getRequestMatcher(this.contextResolver); |
| 125 | + configurer.requestMatchers(requestMatcher); |
| 126 | + } |
| 127 | + |
| 128 | + } |
108 | 129 | } |
109 | 130 |
|
110 | 131 | @Configuration |
@@ -132,80 +153,6 @@ public void init() { |
132 | 153 |
|
133 | 154 | } |
134 | 155 |
|
135 | | - // Get the ignored paths in early |
136 | | - @Order(SecurityProperties.IGNORED_ORDER + 1) |
137 | | - private static class IgnoredPathsWebSecurityConfigurerAdapter |
138 | | - implements WebSecurityConfigurer<WebSecurity> { |
139 | | - |
140 | | - @Autowired(required = false) |
141 | | - private ErrorController errorController; |
142 | | - |
143 | | - @Autowired |
144 | | - private SecurityProperties security; |
145 | | - |
146 | | - @Autowired |
147 | | - private ManagementServerProperties management; |
148 | | - |
149 | | - @Autowired(required = false) |
150 | | - private ManagementContextResolver contextResolver; |
151 | | - |
152 | | - @Autowired(required = false) |
153 | | - private ServerProperties server; |
154 | | - |
155 | | - @Override |
156 | | - public void configure(WebSecurity builder) throws Exception { |
157 | | - } |
158 | | - |
159 | | - @Override |
160 | | - public void init(WebSecurity builder) throws Exception { |
161 | | - if (this.server == null) { |
162 | | - return; |
163 | | - } |
164 | | - IgnoredRequestConfigurer ignoring = builder.ignoring(); |
165 | | - // The ignores are not cumulative, so to prevent overwriting the defaults |
166 | | - // we add them back. |
167 | | - Set<String> ignored = new LinkedHashSet<String>( |
168 | | - SpringBootWebSecurityConfiguration.getIgnored(this.security)); |
169 | | - if (ignored.contains("none")) { |
170 | | - ignored.remove("none"); |
171 | | - } |
172 | | - if (this.errorController != null) { |
173 | | - ignored.add(normalizePath(this.errorController.getErrorPath())); |
174 | | - } |
175 | | - RequestMatcher requestMatcher = getRequestMatcher(); |
176 | | - String[] paths = this.server.getPathsArray(ignored); |
177 | | - if (!ObjectUtils.isEmpty(paths)) { |
178 | | - List<RequestMatcher> matchers = new ArrayList<RequestMatcher>(); |
179 | | - for (String pattern : paths) { |
180 | | - matchers.add(new AntPathRequestMatcher(pattern, null)); |
181 | | - } |
182 | | - if (requestMatcher != null) { |
183 | | - matchers.add(requestMatcher); |
184 | | - } |
185 | | - requestMatcher = new OrRequestMatcher(matchers); |
186 | | - } |
187 | | - if (requestMatcher != null) { |
188 | | - ignoring.requestMatchers(requestMatcher); |
189 | | - } |
190 | | - } |
191 | | - |
192 | | - private RequestMatcher getRequestMatcher() { |
193 | | - if (this.management.getSecurity().isEnabled()) { |
194 | | - return null; |
195 | | - } |
196 | | - return LazyEndpointPathRequestMatcher.getRequestMatcher(this.contextResolver); |
197 | | - } |
198 | | - |
199 | | - private String normalizePath(String errorPath) { |
200 | | - String result = StringUtils.cleanPath(errorPath); |
201 | | - if (!result.startsWith("/")) { |
202 | | - result = "/" + result; |
203 | | - } |
204 | | - return result; |
205 | | - } |
206 | | - |
207 | | - } |
208 | | - |
209 | 156 | @Configuration |
210 | 157 | @ConditionalOnMissingBean(WebSecurityConfiguration.class) |
211 | 158 | @Conditional(WebSecurityEnablerCondition.class) |
@@ -310,9 +257,7 @@ private void configurePermittedRequests( |
310 | 257 | // Permit access to the non-sensitive endpoints |
311 | 258 | requests.requestMatchers(new LazyEndpointPathRequestMatcher( |
312 | 259 | this.contextResolver, EndpointPaths.NON_SENSITIVE)).permitAll(); |
313 | | - // Restrict the rest to the configured roles |
314 | | - List<String> roles = this.management.getSecurity().getRoles(); |
315 | | - requests.anyRequest().hasAnyRole(roles.toArray(new String[roles.size()])); |
| 260 | + requests.anyRequest().authenticated(); |
316 | 261 | } |
317 | 262 |
|
318 | 263 | } |
|
0 commit comments