Polishing the ReferrerPolicyHeaderWriter gh-4110
This commit is contained in:
parent
14a656186d
commit
d2c28c58e2
|
@ -48,11 +48,12 @@ import org.springframework.util.Assert;
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @author Eddú Meléndez
|
* @author Eddú Meléndez
|
||||||
|
* @author Kazuki Shimizu
|
||||||
* @since 4.2
|
* @since 4.2
|
||||||
*/
|
*/
|
||||||
public class ReferrerPolicyHeaderWriter implements HeaderWriter {
|
public class ReferrerPolicyHeaderWriter implements HeaderWriter {
|
||||||
|
|
||||||
private final String REFERER_POLICY_HEADER = "Referrer-Policy";
|
private static final String REFERRER_POLICY_HEADER = "Referrer-Policy";
|
||||||
|
|
||||||
private ReferrerPolicy policy;
|
private ReferrerPolicy policy;
|
||||||
|
|
||||||
|
@ -66,8 +67,8 @@ public class ReferrerPolicyHeaderWriter implements HeaderWriter {
|
||||||
/**
|
/**
|
||||||
* Creates a new instance.
|
* Creates a new instance.
|
||||||
*
|
*
|
||||||
* @param policy
|
* @param policy a referrer policy
|
||||||
* @throws IllegalArgumentException if policyDirectives is null or empty
|
* @throws IllegalArgumentException if policy is null
|
||||||
*/
|
*/
|
||||||
public ReferrerPolicyHeaderWriter(ReferrerPolicy policy) {
|
public ReferrerPolicyHeaderWriter(ReferrerPolicy policy) {
|
||||||
setPolicy(policy);
|
setPolicy(policy);
|
||||||
|
@ -75,7 +76,8 @@ public class ReferrerPolicyHeaderWriter implements HeaderWriter {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the policy to be used in the response header.
|
* Sets the policy to be used in the response header.
|
||||||
* @param policy
|
* @param policy a referrer policy
|
||||||
|
* @throws IllegalArgumentException if policy is null
|
||||||
*/
|
*/
|
||||||
public void setPolicy(ReferrerPolicy policy) {
|
public void setPolicy(ReferrerPolicy policy) {
|
||||||
Assert.notNull(policy, "policy can not be null");
|
Assert.notNull(policy, "policy can not be null");
|
||||||
|
@ -87,7 +89,7 @@ public class ReferrerPolicyHeaderWriter implements HeaderWriter {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void writeHeaders(HttpServletRequest request, HttpServletResponse response) {
|
public void writeHeaders(HttpServletRequest request, HttpServletResponse response) {
|
||||||
response.setHeader(REFERER_POLICY_HEADER, this.policy.getPolicy());
|
response.setHeader(REFERRER_POLICY_HEADER, this.policy.getPolicy());
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum ReferrerPolicy {
|
public enum ReferrerPolicy {
|
||||||
|
@ -101,7 +103,7 @@ public class ReferrerPolicyHeaderWriter implements HeaderWriter {
|
||||||
STRICT_ORIGIN_WHEN_CROSS_ORIGIN("strict-origin-when-cross-origin"),
|
STRICT_ORIGIN_WHEN_CROSS_ORIGIN("strict-origin-when-cross-origin"),
|
||||||
UNSAFE_URL("unsafe-url");
|
UNSAFE_URL("unsafe-url");
|
||||||
|
|
||||||
private static Map<String, ReferrerPolicy> REFERRER_POLICIES;
|
private static final Map<String, ReferrerPolicy> REFERRER_POLICIES;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
Map<String, ReferrerPolicy> referrerPolicies = new HashMap<String, ReferrerPolicy>();
|
Map<String, ReferrerPolicy> referrerPolicies = new HashMap<String, ReferrerPolicy>();
|
||||||
|
@ -113,7 +115,7 @@ public class ReferrerPolicyHeaderWriter implements HeaderWriter {
|
||||||
|
|
||||||
private String policy;
|
private String policy;
|
||||||
|
|
||||||
private ReferrerPolicy(String policy) {
|
ReferrerPolicy(String policy) {
|
||||||
this.policy = policy;
|
this.policy = policy;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue