First, let's look at the X-Frame-Options response header.
This header can be used to indicate whether or not a browser should be allowed to render a page in a <frame>
or <iframe>
.
Sites can use this to avoid Clickjacking attacks, by ensuring that their content is not embedded into other sites.
Spring Security sets the X-Frame-Options response header to DENY
by default.
This tells the browser that the page cannot be displayed in a frame, regardless of the site attempting to do so.
Since the H2 console UI is using <frame>
elements, these will not be rendered and you will see the error screen that you shared in your question.
Spring Security allows you to customise this behaviour using .headers().frameOptions()
in the Security DSL.
If you choose to disable the X-Frame-Options header (not recommended) by setting .headers().frameOptions().disable()
, then Spring Security will not add the X-Frame-Options header to the response.
This means your application could be rendered in a frame, and also could be vulnerable to Clickjacking attacks.
Instead of disabling it, it is sufficient to set X-Frame-Options to SAMEORIGIN
, for this use case.
http
.headers(headers -> headers
.frameOptions(frameOptions -> frameOptions
.sameOrigin()
)
)
This tells the browser that the page can only be displayed in a frame on the same origin as the page itself.
Since the frames in the H2 console UI (such as http://localhost:8080/h2-console/tables.do
) are on the same origin as the the H2 console (http://localhost:8080/h2-console
), the browser will allow them to be displayed.
However, if a different (potentially malicious) website tried to embed one the pages, the browser would not allow it.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…