Skip to content

Commit c78aba4

Browse files
committed
Add lambda DSL method for featurePolicy
Closes: gh-17321 Signed-off-by: Andrey Litvitski <[email protected]>
1 parent 74771a3 commit c78aba4

File tree

4 files changed

+60
-10
lines changed

4 files changed

+60
-10
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/configurers/HeadersConfigurer.java

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
* @author Vedran Pavic
7676
* @author Ankur Pathak
7777
* @author Daniel Garnier-Moiroux
78+
* @author Andrey Litvitski
7879
* @since 3.2
7980
*/
8081
public class HeadersConfigurer<H extends HttpSecurityBuilder<H>>
@@ -355,19 +356,40 @@ public HeadersConfigurer<H> referrerPolicy(Customizer<ReferrerPolicyConfig> refe
355356
* @return the {@link FeaturePolicyConfig} for additional configuration
356357
* @throws IllegalArgumentException if policyDirectives is {@code null} or empty
357358
* @since 5.1
358-
* @deprecated For removal in 7.0. Use {@link #permissionsPolicy(Customizer)} or
359-
* {@code permissionsPolicy(Customizer.withDefaults())} to stick with defaults. See
360-
* the <a href=
361-
* "https://docs.spring.io/spring-security/reference/migration-7/configuration.html#_use_the_lambda_dsl">documentation</a>
362-
* for more details.
363359
* @see ObjectPostProcessorConfiguration FeaturePolicyHeaderWriter
364360
*/
365-
@Deprecated
366361
public FeaturePolicyConfig featurePolicy(String policyDirectives) {
367362
this.featurePolicy.writer = new FeaturePolicyHeaderWriter(policyDirectives);
368363
return this.featurePolicy;
369364
}
370365

366+
/**
367+
* Allows configuration for <a href="https://wicg.github.io/feature-policy/">Feature
368+
* Policy</a> using the lambda-based DSL.
369+
* <p>
370+
* Calling this method automatically enables (includes) the {@code Feature-Policy}
371+
* header in the response using the supplied policy directive(s).
372+
* <p>
373+
* Configuration is provided to the {@link FeaturePolicyHeaderWriter}, which is
374+
* responsible for writing the header.
375+
* <p>
376+
* Even though the Feature-Policy header has been deprecated in favor of the
377+
* Permissions-Policy header, many browsers still support Feature-Policy. As such,
378+
* this method allows applications to continue using Feature-Policy when necessary.
379+
* @param featurePolicyCustomizer the {@link Customizer} to provide feature policy
380+
* configuration
381+
* @return the {@link HeadersConfigurer} for additional configuration
382+
* @since 6.5
383+
* @see FeaturePolicyHeaderWriter
384+
* @see <a href="https://wicg.github.io/feature-policy/">Feature Policy
385+
* specification</a>
386+
*/
387+
public HeadersConfigurer<H> featurePolicy(Customizer<FeaturePolicyConfig> featurePolicyCustomizer) {
388+
this.featurePolicy.writer = new FeaturePolicyHeaderWriter();
389+
featurePolicyCustomizer.customize(this.featurePolicy);
390+
return this;
391+
}
392+
371393
/**
372394
* Allows configuration for
373395
* <a href="https://w3c.github.io/webappsec-permissions-policy/"> Permissions
@@ -990,6 +1012,17 @@ public final class FeaturePolicyConfig {
9901012
private FeaturePolicyConfig() {
9911013
}
9921014

1015+
/**
1016+
* Sets the policy directives to be used in the response header.
1017+
* @param policyDirectives a permissions policy directives
1018+
* @return the {@link FeaturePolicyConfig} for additional configuration
1019+
* @throws IllegalArgumentException if policy is null
1020+
*/
1021+
public FeaturePolicyConfig policyDirectives(String policyDirectives) {
1022+
this.writer.setPolicyDirectives(policyDirectives);
1023+
return this;
1024+
}
1025+
9931026
/**
9941027
* Allows completing configuration of Feature Policy and continuing configuration
9951028
* of headers.

config/src/main/java/org/springframework/security/config/web/server/ServerHttpSecurity.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@
297297
* @author Ankur Pathak
298298
* @author Alexey Nesterov
299299
* @author Yanming Zhou
300+
* @author Andrey Litvitski
300301
* @since 5.0
301302
*/
302303
public class ServerHttpSecurity {
@@ -2584,14 +2585,22 @@ public HeaderSpec contentSecurityPolicy(Customizer<ContentSecurityPolicySpec> co
25842585
* Configures {@code Feature-Policy} response header.
25852586
* @param policyDirectives the policy
25862587
* @return the {@link FeaturePolicySpec} to configure
2587-
* @deprecated For removal in 7.0. Use {@link #permissionsPolicy(Customizer)}
2588-
* instead.
25892588
*/
2590-
@Deprecated
25912589
public FeaturePolicySpec featurePolicy(String policyDirectives) {
25922590
return new FeaturePolicySpec(policyDirectives);
25932591
}
25942592

2593+
/**
2594+
* Configures {@code Feature-Policy} response header.
2595+
* @param featurePolicyCustomizer the {@link Customizer} to provide more options
2596+
* for the {@link FeaturePolicySpec}
2597+
* @return the {@link HeaderSpec} to customize
2598+
*/
2599+
public HeaderSpec featurePolicy(Customizer<FeaturePolicySpec> featurePolicyCustomizer) {
2600+
featurePolicyCustomizer.customize(new FeaturePolicySpec());
2601+
return this;
2602+
}
2603+
25952604
/**
25962605
* Configures {@code Permissions-Policy} response header.
25972606
* @param permissionsPolicyCustomizer the {@link Customizer} to provide more
@@ -2872,6 +2881,9 @@ private ContentSecurityPolicySpec(String policyDirectives) {
28722881
*/
28732882
public final class FeaturePolicySpec {
28742883

2884+
private FeaturePolicySpec() {
2885+
}
2886+
28752887
private FeaturePolicySpec(String policyDirectives) {
28762888
HeaderSpec.this.featurePolicy.setPolicyDirectives(policyDirectives);
28772889
}

config/src/main/kotlin/org/springframework/security/config/annotation/web/HeadersDsl.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ class HeadersDsl {
166166
*
167167
* @param policyDirectives policyDirectives the security policy directive(s)
168168
*/
169-
@Deprecated("Use 'permissionsPolicy { }' instead.")
170169
fun featurePolicy(policyDirectives: String) {
171170
this.featurePolicyDirectives = policyDirectives
172171
}

web/src/main/java/org/springframework/security/web/header/writers/FeaturePolicyHeaderWriter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ public final class FeaturePolicyHeaderWriter implements HeaderWriter {
4242

4343
private String policyDirectives;
4444

45+
/**
46+
* Create a new instance of {@link FeaturePolicyHeaderWriter}
47+
*/
48+
public FeaturePolicyHeaderWriter() {
49+
}
50+
4551
/**
4652
* Create a new instance of {@link FeaturePolicyHeaderWriter} with supplied security
4753
* policy directive(s).

0 commit comments

Comments
 (0)