0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Web 開発再入門 #19 ― セキュリティ対策(Not Found Page 処理)

Last updated at Posted at 2024-04-28

Web 開発再入門 #19 ― セキュリティ設定(Not Found Page 処理)

fmockup

★ 本ページは工事中 ★

はじめに

SpringBoot のデフォルトでは、誤った URL をアクセスすると、いろいろな情報を返却してしまいます。ハッカーは、これらの情報を見て、Web アプリケーションの作りを想像して、攻撃を仕掛けてきます。
よって、最低限の情報しか返却しないように修正します。

フォルダー・ファイル構成

D:\
└ Developments\
    └ Workspace\
         └ fmockup\
             ├ build\
             ├ sql\
             ├ src\
             │ └ main\
             │     ├ java\
             │     │ └ cn\
             │     │     └ com\
             │     │         └ xxxx\
             │     │             └ fmockup\
             │     │                  ├ action\
             │     │                  ├ controller\
             │     │                  ├ customizer\
             │     │                  │ └ ErrorIntercepter.java ← コレ
             │     │                  ├ entity\
             │     │                  ├ mapper\
             │     │                  ├ response\
             │     │                  ├ service\
             │     │                  ├ util\
             │     │                  ├ validator\
             │     │                  └ validator_order\
             │     └ resources\
             │          ├ mapper\
             │          │ └ error.html ← コレ
             │          ├ templates\
             │          └ static\
             ├ vue-vite\
             └ WinSW.NET-nnn\

ファイルの文字コード

基本的に Eclipse でファイルを作成するので、あまり意識したことがありません。
多分、Unix 改行(LF)なのだと思います。

ファイルの作成

  1. ファイル “ErrorIntercepter.java”、“error.html” を作成する。
    ErrorIntercepter.java
    ・・・
    
    package cn.com.xxxx.fmockup.customizer;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.springframework.boot.web.servlet.error.ErrorController;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.MediaType;
    import org.springframework.http.ResponseEntity;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    import jakarta.servlet.RequestDispatcher;
    import jakarta.servlet.http.HttpServletRequest;
    
    /**
     * Error Interceptor
     *   It Cuts off Some Information from Error Information.
     */
    
    /*
     * If a URL is invalid, the Spring Boot does not become an error.
     * 
     * If this is not specified. Spring boot outputs a "Not Found" page.
     * It gives a hint to attackers, so it is not secured...
     */
    
    @Controller
    public class ErrorInterceptor implements ErrorController {
    
    	/**
    	 * Error Interceptor (for HTML request)
    	 *   It Cuts off Some Information from Error Information.
    	 */
    	@RequestMapping(produces = {MediaType.TEXT_HTML_VALUE})
    	public ModelAndView errorHtml(HttpServletRequest request, ModelAndView mav) {
    
    		HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
    		Object statusCode = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
    		if (statusCode != null && statusCode.toString().equals("404")) {
    		  status = HttpStatus.NOT_FOUND;
    		}
    		mav.setStatus(status);
    
    		return mav;
    	}
    
    	/**
    	 * Error Interceptor (for JSON request)
    	 *   It Cuts off Some Information from Error Information.
    	 */
    	@RequestMapping(produces = {MediaType.APPLICATION_JSON_VALUE})
    	public ResponseEntity<Map<String, Object>> errorJson(HttpServletRequest req) {
    
    		HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
    		// No Convert
    		// Object statusCode = req.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
    		// if (statusCode != null && statusCode.toString().equals("404")) {
    		//     status = HttpStatus.NOT_FOUND;
    		// }
    
    		Map<String, Object> body = new HashMap<String, Object>();
    		body.put("status", status.value());
    
    		return new ResponseEntity<>(body, status);
    	}
    }
    
    error.html
    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="UTF-8">
    		<title>Error</title>
    	</head>
    	<body>
    		Error<br>
    	</body>
    </html>
    

参考

Not Found Page 処理

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?