Memo

メモ > 技術 > フレームワーク: SpringBoot > セキュリティ

■セキュリティ
■XSS対策 Thymeleafの標準機能で対応されている 以下のようにすると、HTMLがエスケープして出力される
[[${text}]]
以下のようにすると、HTMLがそのまま出力される
[(${text})]
クロスサイトスクリプティング(XSS)のテスト - Qiita https://qiita.com/oh_yeah_sayryo/items/4e39995e47e371953480 ■CSRF対策 Spring Boot Starter Securityを導入することで対応できる pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
src/main/java/com/example/demo/config/SecurityConfig.java
package com.example.demo.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.web.SecurityFilterChain; @EnableWebSecurity @Configuration public class SecurityConfig { @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http.authorizeRequests().anyRequest().permitAll(); return http.build(); } }
実行すると、コンソールに以下が出力された。これは認証用のパスワードらしいが、詳細は改めて確認しておきたい
Using generated security password: 44294d32-d86a-465c-80a3-7e7405cdd084 This generated password is for development use only. Your security configuration must be updated before running your application in production.
上記は「すべてのページで認証を求めない」という指定だが、この時点でCSRF対策が行われている 投稿画面のformタグに、「_csrf」を送信するためのコードが追加されていることを確認できる
<form action="/dev/task/create" method="post"> ↓ <form action="/dev/task/create" method="post"><input type="hidden" name="_csrf" value="JYbAnNEHGeO4mA8MhnQA_s9CKsESkNtfjsTADk3T2mkBKvq6R7T1-edhL4eVrz404lk0mPl2B_gioeNy6POmb3Tku1ljHMOD"/>
SpringSecurityの導入/CSRF対策|Javaの基礎を学び終えたアナタに贈る, SpringBoot/SpringSecurityによる掲示板開発ハンズオン https://zenn.dev/angelica/books/52be1e365c61ea/viewer/f0683a Basic認証を使用しない場合、以下の方法でパスワードの出力を無くすことができるらしい 【Spring Security】Basic認証いらないけど、良い感じにデフォルトのレスポンスヘッダーを使いたい時に見る記事 - Qiita https://qiita.com/akkino_D-En/items/d506fa24349a0cbd414d 以下は参考になるかと思ったが情報が古いか 【逆引き】Spring Security(随時更新) - Qiita https://qiita.com/mr-hisa-child/items/173289e47b4f21022562

Advertisement