001/*
002 * Copyright 2012-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.autoconfigure.security.servlet;
018
019import java.util.function.Supplier;
020
021import javax.servlet.http.HttpServletRequest;
022
023import org.springframework.boot.autoconfigure.h2.H2ConsoleProperties;
024import org.springframework.boot.autoconfigure.security.StaticResourceLocation;
025import org.springframework.boot.security.servlet.ApplicationContextRequestMatcher;
026import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
027import org.springframework.security.web.util.matcher.RequestMatcher;
028
029/**
030 * Factory that can be used to create a {@link RequestMatcher} for commonly used paths.
031 *
032 * @author Madhura Bhave
033 * @author Phillip Webb
034 * @since 2.0.0
035 */
036public final class PathRequest {
037
038        private PathRequest() {
039        }
040
041        /**
042         * Returns a {@link StaticResourceRequest} that can be used to create a matcher for
043         * {@link StaticResourceLocation locations}.
044         * @return a {@link StaticResourceRequest}
045         */
046        public static StaticResourceRequest toStaticResources() {
047                return StaticResourceRequest.INSTANCE;
048        }
049
050        /**
051         * Returns a matcher that includes the H2 console location. For example:
052         * <pre class="code">
053         * PathRequest.toH2Console()
054         * </pre>
055         * @return the configured {@link RequestMatcher}
056         */
057        public static H2ConsoleRequestMatcher toH2Console() {
058                return new H2ConsoleRequestMatcher();
059        }
060
061        /**
062         * The request matcher used to match against h2 console path.
063         */
064        public static final class H2ConsoleRequestMatcher
065                        extends ApplicationContextRequestMatcher<H2ConsoleProperties> {
066
067                private volatile RequestMatcher delegate;
068
069                private H2ConsoleRequestMatcher() {
070                        super(H2ConsoleProperties.class);
071                }
072
073                @Override
074                protected void initialized(Supplier<H2ConsoleProperties> h2ConsoleProperties) {
075                        this.delegate = new AntPathRequestMatcher(
076                                        h2ConsoleProperties.get().getPath() + "/**");
077                }
078
079                @Override
080                protected boolean matches(HttpServletRequest request,
081                                Supplier<H2ConsoleProperties> context) {
082                        return this.delegate.matches(request);
083                }
084
085        }
086
087}