官术网_书友最值得收藏!

How it works...

The magic behind this functionality is actually very simple. Let's start from the separate configuration class and work our way to the filter bean detection.

If we look in our main class, BookPubApplication, we will see that it is annotated with @SpringBootApplication, which in turn is a convenience meta-annotation that declares @ComponentScan among others. We discussed this in detail in one of our earlier recipes. The presence of @ComponentScan instructs Spring Boot to detect WebConfiguration as a @Configuration class and add its definitions to the context. So, anything that we will declare in WebConfiguration is as good as if we were to put it right in BookPubApplication itself.

The @BeanpublicRemoteIpFilterremoteIpFilter() {...} declaration simply creates a Spring bean for the RemoteIpFilter class. When Spring Boot detects all the beans of javax.servlet.Filter, it will add them to the filter chain automatically. So, all we have to do, if we want to add more filters, is to just declare them as @Bean configurations. For example, for a more advanced filter configuration, if we want a particular filter to apply only to specific URL patterns, we can create a @Bean configuration of a FilterRegistrationBean type and use it to configure the precise settings.

To make supporting this use-case easier Spring Boot provides us with configuration properties that can be used instead of manually configuring the  RemoteIpFilter bean for occasions when Tomcat servlet container is being used. Use server.use-forward-headers=true to indicate to Spring Boot that it needs to automatically configure support for proxy headers, to provide proper request obfuscation. Specifically for Tomcat, one can also use server.tomcat.remote_ip_header=x-forwarded-for and server.tomcat.protocol_header=x-forwarded-proto properties to configure what specific header names should be used to retrieve the values.
主站蜘蛛池模板: 格尔木市| 白银市| 调兵山市| 台山市| 天门市| 敖汉旗| 大关县| 平顶山市| 聊城市| 年辖:市辖区| 于田县| 屯昌县| 岳西县| 桃园县| 高州市| 泊头市| 大邑县| 南汇区| 聂拉木县| 武清区| 大港区| 泰宁县| 柯坪县| 长垣县| 凤庆县| 五台县| 二连浩特市| 肃宁县| 龙陵县| 南昌县| 平乡县| 民勤县| 北碚区| 德州市| 化隆| 玉田县| 仁寿县| 长丰县| 怀来县| 饶平县| 四子王旗|