内容安全策略Jhipster(春季启动)

本文介绍了在JHipster(基于Spring Boot)应用中使用Content Security Policy(CSP)来增强安全性,防止跨站脚本攻击和数据注入。CSP是一种向后兼容的安全层,允许开发者指定资源的有效来源。文章详细讨论了CSP的常见指令,如default-src、font-src等,并指出在JHipster 5.0.x版本后,CSP头已添加到安全配置中。此外,还推荐了一个CSP评估工具,帮助开发者在实施前测试策略的有效性。
摘要由CSDN通过智能技术生成

大家好,今天我要在JHipster中写有关CSP的内容,或者在JHipster中说内容安全策略。 从现在开始,我将写更多关于Jhipster的文章,因此这是该系列的第一篇文章。 因此,让我们开始吧。 将我们的一个应用程序更新到最新版本时出现问题。 所以我试图一次又一次地查看我的代码。 所以我终于在控制台中出错了;脚本由于安全权限而被阻止。 我再次看了一下,才知道我需要在HTML索引中添加一些标头以完成相同的工作,但是确实如此。 不行。 因此,尝试许多资源没有用。 因此,请查看Github存储库上的对话; 我发现了一些我需要修复的代码。 但是在开始之前,让我们知道什么是CSP或内容安全策略。

Content Security Policy

So according to Mozilla Developers Site, CSP is is an added layer of security that helps to detect and mitigate certain types of attacks, including Cross-Site Scripting (X小号S) and data injection attacks. These attacks are used for everything; from data, theft to site defacement to the distribution of malware.

CSP has a fully backward compatible design. Browsers that don’t support it still work with servers that implement it, and vice-versa: browsers that don’t support CSP simply ignore it, functioning, as usual, defaulting to the standard same-origin policy for web content. If the site doesn’t offer the CSP header, browsers likewise use the standard same-origin policy.

要启用CSP,您需要配置您的Web服务器以返回Content-Security-Policy HTTP标头(有时您会看到X-Content-Security-Policy标头的提法,但这是一个较旧的版本,不需要指定 它)。

或者,该元素可用于配置策略,例如:

我们在网页上使用了各种资源,例如媒体,脚本,图像,因此为了允许这些资源,我们需要使用指令。

Some common directives are listed below (Check others here) :

  1. default-src – Serves as a fallback for the other fetch directives.

  2. font-src – Specifies valid sources for fonts loaded using @font-face.

  3. frame-src – Specifies valid sources for nested browsing contexts loading using elements such as and .

  4. img-src – Specifies valid sources of images and favicons.

  5. media-src Specifies valid sources for loading media using the <audio> , <video> and <track> elements.

  6. object-src Specifies valid sources for the <object>, <embed>, and <applet> elements.

因此,让我们回到主要问题。

CSP Headers in JHipster (Spring Boot)

因此,从Jhipster 5.0.x左右开始,属性CSP头添加到了安全配置中。 您可以在Project folder > src > main > java > package > config > SecurityConfiguration.java。

/**/************
Content Security policy Jhipster
**********/

/* Use directives as per your requirement like image-src and default-src for defaults of all*/
// Single line CSP  
.headers()
  .contentSecurityPolicy("default-src 'self';")

// Multi Line CSP joined by and
.headers()
  .contentSecurityPolicy("default-src 'self';")
  .and()
  .contentSecurityPolicy("script-src 'self' 'unsafe-eval' 'unsafe-inline' https://www.gstatic.com https://www.google.com http://www.google-analytics.com https://maps.googleapis.com https://storage.googleapis.com;")


 * Insert your code here
 */

因此,在上面您可以看到示例。 这样,您可以基于JHipster中的多个指令(如image-src)添加CSP,以用于来自URL或数据的图像:src,script-src(如内联脚本,外部脚本)。

Bonus

您将思考剩下的内容,所以让我告诉您这是奖金,CSP评估员。 因此,在直接申请之前,您可以使用该工具测试CSP是否有效。 您还可以在同一位置获得警告和信息,并附带示例(如果您不知道从何处开始)。 链接在这里 :

https://csp-evaluator.withgoogle.com/

希望您喜欢这个职位,继续检查更多消息。 另外,请随时在下面的评论中分享您的观点。 在分享时,也要与您的朋友分享。 继续检查。

Originally published at Tekraze.com

from: https://dev.to//balvinder294/content-security-policy-jhipster-spring-boot-1c6i

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值