簡體   English   中英

在Spring提供安全

[英]Provide security in Spring

我正在 React 和 spring 中開發一個基本的 crud web 應用程序。我正在使用 postman 進行測試,因為前端尚未准備好。 我有這個方法,但我剛剛發現任何人都知道該 id 可以發送 HTTP 請求並獲取所有數據。

@PostMapping("/utente")
public ResponseEntity<Object> getDatiProfiloUtente(@RequestBody final Long idUtente){
        HashMap<String, Object> map = new HashMap<>();

        Paziente paziente = service.findPazienteById(idUtente);
        map.put("nome", paziente.getNome());
        map.put("cognome", paziente.getCognome());
        map.put("email", paziente.getEmail());
        map.put("nTelefono", paziente.getNumeroTelefono());
        map.put("emailCaregiver", paziente.getEmailCaregiver());
        map.put("nomeCaregiver", paziente.getNomeCaregiver());
        map.put("cognomeCaregiver", paziente.getCognomeCaregiver());
            
        return new ResponseEntity<>(map, HttpStatus.OK);
    }

我如何提供安全保障? 我希望只有登錄用戶才能看到他的數據。

您想使用 spring security 提供的@Secured注釋,baeldung 的這篇文章是一個很好的資源,並准確解釋了如何設置您需要的方法安全性。

您必須使用 @EnableWebSecurity 注釋。 Spring 引導為安全提供了強大的支持。 Spring 可以與各種第三方安全應用程序集成,並提供簡單的內存安全性。

在原始文檔中,有一個簡單的 memory 的安全實現。我強烈建議您查看它。 https://docs.spring.io/spring-security/site/docs/4.0.x/apidocs/org/springframework/security/config/annotation/web/configuration/EnableWebSecurity.html

但是,內存管理通常沒有用。 相反,您可能希望將用戶信息保存在數據庫或不同的應用程序中。

除此之外,Rest 的最佳做法是使用 jwt 令牌。 您可能想看看這個示例,了解如何將它與 Spring Boot 一起使用。 https://www.javainuse.com/spring/boot-jwt

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM