public final class ContentSecurityPolicyHeaderWriter extends Object implements HeaderWriter
Provides support for Content Security Policy (CSP) Level 2.
CSP provides a mechanism for web applications to mitigate content injection vulnerabilities, such as cross-site scripting (XSS). CSP is a declarative policy that allows web application authors to inform the client (user-agent) about the sources from which the application expects to load resources.
For example, a web application can declare that it only expects to load script from specific, trusted sources. This declaration allows the client to detect and block malicious scripts injected into the application by an attacker.
A declaration of a security policy contains a set of security policy directives (for example, script-src and object-src), each responsible for declaring the restrictions for a particular resource type. The list of directives defined can be found at Directives.
Each directive has a name and value. For detailed syntax on writing security policies, see Syntax and Algorithms.
This implementation of
HeaderWriter writes one of the following headers:
By default, the Content-Security-Policy header is included in the response.
true will include the
Content-Security-Policy-Report-Only header in the response.
NOTE: The supplied security policy directive(s) will be used for whichever header is enabled (included).
CSP is not intended as a first line of defense against content injection vulnerabilities. Instead, CSP is used to reduce the harm caused by content injection attacks. As a first line of defense against content injection, web application authors should validate their input and encode their output.
|Constructor and Description|
Creates a new instance
|Modifier and Type||Method and Description|
Sets the security policy directive(s) to be used in the response header.
If true, includes the Content-Security-Policy-Report-Only header in the response, otherwise, defaults to the Content-Security-Policy header.
public void writeHeaders(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
request- the request
response- the response
public void setPolicyDirectives(String policyDirectives)
policyDirectives- the security policy directive(s)
IllegalArgumentException- if policyDirectives is null or empty
public void setReportOnly(boolean reportOnly)
reportOnly- set to true for reporting policy violations only