GDPR Compliant Spring Boot Apps | Data Privacy, Consent & Deletion | Sinhala Guide
ඉතින් කොහොමද යාලුවනේ? අද ලෝකේ අපි හැමෝම අන්තර්ජාලය හරහා විශාල දත්ත ප්රමාණයක් හුවමාරු කරනවා. Facebook, Google, online shopping sites වගේම අපි හදන customized applications වලටත් අපේ user data, personal information ලබා දෙනවා. මේ දත්ත වල ආරක්ෂාව කියන්නේ අද කාලේ හරිම වැදගත් දෙයක්, මොකද කිසිම කෙනෙක් කැමති නැහැ තමන්ගේ private data අවභාවිතා කරනවට. ඒ වගේම, companies වලටත් මේ user data safe කරන්න එක එක නීති රීති වලට අනුකූලව වැඩ කරන්න සිද්ධ වෙනවා.
අද අපි කතා කරන්න යන්නේ මේ GDPR (General Data Protection Regulation) කියන එක ගැන. ඒ වගේම Spring Boot භාවිතා කරලා කොහොමද GDPR compliant applications හදන්නේ කියන එක ගැනත් අපි discuss කරමු. විශේෂයෙන්ම, user consent manage කරන විදිය සහ user data delete කරන විදිය (data deletion) implement කරන හැටි අපි practical විදියට බලමු.
මේ article එක අවසානෙදි ඔයාට GDPR ගැන හොඳ අවබෝධයක් ලැබිලා, ඔයාගේ Spring Boot projects වලට මේ concepts implement කරන්න පුළුවන් වෙයි!
1. GDPR කියන්නේ මොකක්ද? (What is GDPR?)
GDPR කියන්නේ European Union (EU) රටවල් වල පුද්ගලයන්ගේ දත්ත ආරක්ෂා කිරීම සඳහා නිර්මාණය කරපු නීති මාලාවක්. සරලව කිව්වොත්, EU පුරවැසියන්ගේ personal data කොහොමද collect කරන්නේ, process කරන්නේ, store කරන්නේ සහ destroy කරන්නේ කියන එකට අදාළ නීති රීති මෙයින් නියම වෙනවා. EU එකට අයිති නැති වුණත්, EU පුරවැසියන්ට අදාළ services ලබා දෙන ඕනෑම company එකකට මේ GDPR නීති අදාළ වෙනවා. මේකෙදි ප්රධාන කරුණු කීපයක් තියෙනවා:
- Data Subject Rights: User කෙනෙක්ට තමන්ගේ data වලට access කරන්න, edit කරන්න, delete කරන්න සහ process කරන එකට විරුද්ධ වෙන්න වගේ අයිතිවාසිකම් තියෙනවා.
- Consent: User data collect කරන්න කලින්, ඒ user ගෙන් clear සහ unambiguous consent එකක් ලබා ගන්න ඕනේ.
- Data Minimization: Application එකකට අත්යවශ්යම data විතරක් collect කරන්න ඕනේ.
- Security: Collect කරන data ආරක්ෂා කරන්න හොඳ security measures apply කරන්න ඕනේ.
- Accountability: Data controller (company) එකට data protection regulations වලට අනුකූලව වැඩ කරන බව පෙන්වන්න පුළුවන් වෙන්න ඕනේ.
ඉතින් අපි Spring Boot developers විදියට මේ නීති ගැන දැනගෙන ඉන්න එක සහ ඒවා අපේ applications වලට implement කරන එක අත්යවශ්යයි. එහෙම නැත්නම්, company එකට විශාල penalties වගේම user trust එකත් නැති වෙන්න පුළුවන්.
2. User Consent එක Manage කරමු (Managing User Consent)
GDPR වලදී user consent කියන්නේ හරිම critical point එකක්. User කෙනෙක්ගේ personal data එකක් collect කරන්න කලින්, ඒ user ගෙන් ඒ සඳහා clear consent එකක් ලබාගෙන ඒක record කරලා තියාගන්න ඕනේ. මේක හරියට බැංකුවක සල්ලි වගේ පරිස්සමට maintain කරන්න ඕනේ දෙයක්.
Consent ලබා ගන්නා ආකාරය (Obtaining Consent)
Consent එකක් ලබා ගන්නකොට ඒක explicit වෙන්න ඕනේ. ඒ කියන්නේ, user කෙනෙක් checkbox එකක් tick කරනවා වගේ action එකකින් තමන්ගේ කැමැත්ත පැහැදිලිව ප්රකාශ කරන්න ඕනේ. Auto-ticked checkboxes හෝ pre-filled forms වගේ දේවල් GDPR compliant නැහැ. ඒ වගේම, user ගෙන් ලබා ගන්න data මොකටද භාවිතා කරන්නේ කියලත් පැහැදිලිව සඳහන් කරන්න ඕනේ.
Spring Boot වල Consent Store කරන හැටි (Storing Consent in Spring Boot)
අපි user consent එක database එකේ store කරමු. ඒ සඳහා අපේ User entity එකට අදාළ fields කීපයක් එකතු කරන්න පුළුවන්:
consentGiven: User ට data collection සඳහා කැමැත්ත දීලාද නැද්ද කියලා පෙන්නන්නbooleanඑකක්.consentTimestamp: Consent එක ලබා දුන්න වෙලාවLocalDateTimeඑකක් විදියට.consentDetails: User agreement එකේ version එක වගේ දේවල් save කරන්නStringඑකක්.
මේ තියෙන්නේ සරල User entity එකක් සහ UserRepository එකක්:
// User.java
package com.example.gdpr.model;
import javax.persistence.*;
import java.time.LocalDateTime;
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
private String email;
// GDPR Specific fields
private boolean consentGiven;
private LocalDateTime consentTimestamp;
private String consentVersion;
private boolean isDeleted; // For soft delete
private LocalDateTime deletedAt; // For soft delete
// Getters and Setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public boolean isConsentGiven() {
return consentGiven;
}
public void setConsentGiven(boolean consentGiven) {
this.consentGiven = consentGiven;
}
public LocalDateTime getConsentTimestamp() {
return consentTimestamp;
}
public void setConsentTimestamp(LocalDateTime consentTimestamp) {
this.consentTimestamp = consentTimestamp;
}
public String getConsentVersion() {
return consentVersion;
}
public void setConsentVersion(String consentVersion) {
this.consentVersion = consentVersion;
}
public boolean isDeleted() {
return isDeleted;
}
public void setDeleted(boolean deleted) {
isDeleted = deleted;
}
public LocalDateTime getDeletedAt() {
return deletedAt;
}
public void setDeletedAt(LocalDateTime deletedAt) {
this.deletedAt = deletedAt;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", email='" + email + '\'' +
", consentGiven=" + consentGiven +
", consentTimestamp=" + consentTimestamp +
", consentVersion='" + consentVersion + '\'' +
", isDeleted=" + isDeleted +
", deletedAt=" + deletedAt +
'}';
}
}
// UserRepository.java
package com.example.gdpr.repository;
import com.example.gdpr.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Optional;
public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByEmail(String email);
}
Consent Update කරන Controller එකක් (A Controller to Update Consent)
දැන් අපි බලමු user කෙනෙක් consent එකක් දෙනකොට හෝ withdraw කරනකොට ඒක update කරන්න Controller එකක් ලියන හැටි. මේකෙදි අපි UserService එකක් හරහා UserRepository එක use කරනවා.
// UserService.java
package com.example.gdpr.service;
import com.example.gdpr.model.User;
import com.example.gdpr.repository.UserRepository;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.Optional;
@Service
public class UserService {
private final UserRepository userRepository;
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public User registerUser(User user) {
// Default consent status for new users
user.setConsentGiven(false);
user.setConsentVersion("1.0"); // Default version
return userRepository.save(user);
}
public Optional<User> getUserById(Long id) {
return userRepository.findById(id);
}
public User updateConsent(Long userId, boolean consentGiven, String consentVersion) {
return userRepository.findById(userId).map(user -> {
user.setConsentGiven(consentGiven);
user.setConsentTimestamp(LocalDateTime.now());
user.setConsentVersion(consentVersion);
return userRepository.save(user);
}).orElseThrow(() -> new RuntimeException("User not found with id " + userId));
}
// Other user related methods (e.g., update profile, get all users)
}
// UserController.java
package com.example.gdpr.controller;
import com.example.gdpr.model.User;
import com.example.gdpr.service.UserService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/users")
public class UserController {
private final UserService userService;
public UserController(UserService userService) {
this.userService = userService;
}
@PostMapping
public ResponseEntity<User> registerUser(@RequestBody User user) {
User registeredUser = userService.registerUser(user);
return ResponseEntity.ok(registeredUser);
}
@GetMapping("/{id}")
public ResponseEntity<User> getUser(@PathVariable Long id) {
return userService.getUserById(id)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
@PutMapping("/{id}/consent")
public ResponseEntity<User> updateConsent(@PathVariable Long id, @RequestParam boolean consentGiven, @RequestParam(required = false, defaultValue = "1.0") String consentVersion) {
try {
User updatedUser = userService.updateConsent(id, consentGiven, consentVersion);
return ResponseEntity.ok(updatedUser);
} catch (RuntimeException e) {
return ResponseEntity.notFound().build();
}
}
}
මේ updateConsent endpoint එකට request එකක් යැවීමෙන් user කෙනෙක්ගේ consent status එක update කරන්න පුළුවන්. උදාහරණයක් විදියට:
PUT /api/users/1/consent?consentGiven=true&consentVersion=1.1
මේකෙන් user ID 1 හි consent එක true ලෙස, version 1.1 සමග update වෙනවා. Consent එක withdraw කරනවා නම් consentGiven=false කියලා දෙන්න පුළුවන්.
3. Data Subject Rights Implement කරමු: Right to Erasure (Right to be Forgotten)
GDPR යටතේ user කෙනෙක්ට තමන්ගේ personal data delete කරන්න (right to erasure) අයිතියක් තියෙනවා. මේක හරිම වැදගත් සහ සංකීර්ණ වෙන්න පුළුවන් task එකක්. Data delete කරනකොට අපි “soft delete” වගේ approach එකක් භාවිතා කරන එක වඩාත් සුදුසුයි, මොකද සමහර වෙලාවට audit purposes හෝ legal obligations නිසා data සම්පූර්ණයෙන්ම delete කරන්න බැරි වෙන්න පුළුවන්.
Soft Delete එකක් Implement කරමු (Implementing Soft Delete)
Soft delete කියන්නේ, අපි database එකෙන් record එක delete කරන්නේ නැහැ, ඒ වෙනුවට ඒ record එක delete කරලා කියලා mark කරනවා. අපේ User entity එකට අපි isDeleted (boolean) සහ deletedAt (LocalDateTime) කියන fields දෙක එකතු කළේ මේකට තමයි.
User කෙනෙක් තමන්ගේ data delete කරන්න request කරනකොට, අපි ඒ user ගේ isDeleted field එක true කරලා deletedAt එකට timestamp එකක් දානවා. ඊට පස්සේ, data retrieve කරන හැම තැනකදීම isDeleted=false users ලා විතරක් retrieve කරන්න අපි logic එක modify කරන්න ඕනේ.
Data Deletion Controller සහ Service (Data Deletion Controller and Service)
දැන් අපි බලමු user කෙනෙක්ගේ data delete කරන්න service method එකක් සහ controller endpoint එකක් හදන හැටි:
// UserService.java (add this method)
public User deleteUser(Long userId) {
return userRepository.findById(userId).map(user -> {
user.setDeleted(true);
user.setDeletedAt(LocalDateTime.now());
// Optionally, you might want to anonymize sensitive data here
// user.setEmail("anonymized_" + userId + "@example.com");
// user.setUsername("deleted_user_" + userId);
return userRepository.save(user);
}).orElseThrow(() -> new RuntimeException("User not found with id " + userId));
}
// Modify existing findAll to exclude soft-deleted users
public List<User> getAllActiveUsers() {
return userRepository.findAll().stream()
.filter(user -> !user.isDeleted())
.collect(Collectors.toList());
}
// Modify getUserById to ensure it doesn't return deleted users by default
public Optional<User> getActiveUserById(Long id) {
return userRepository.findById(id)
.filter(user -> !user.isDeleted());
}
// UserController.java (add this endpoint)
@DeleteMapping("/{id}")
public ResponseEntity<String> deleteUser(@PathVariable Long id) {
try {
userService.deleteUser(id);
return ResponseEntity.ok("User with ID " + id + " has been soft-deleted.");
} catch (RuntimeException e) {
return ResponseEntity.notFound().build();
}
}
@GetMapping // Modify this to only return active users
public ResponseEntity<List<User>> getAllUsers() {
List<User> users = userService.getAllActiveUsers();
return ResponseEntity.ok(users);
}
මේ deleteUser endpoint එකට request එකක් යැවීමෙන් user කෙනෙක්ගේ data soft-delete කරන්න පුළුවන්. GET /api/users endpoint එකෙන් දැන් delete කරපු users ලා පෙන්වන්නේ නැහැ.
සැලකිය යුතුයි: GDPR හි Right to Erasure එකට අනුව, සමහර අවස්ථාවලදී data සම්පූර්ණයෙන්ම delete කරන්න (hard delete) සිද්ධ වෙනවා. මේකෙදි database එකෙන් record එක සම්පූර්ණයෙන්ම අයින් කරන්න පුළුවන්. හැබැයි මේක හරිම පරිස්සමෙන් කරන්න ඕනේ, මොකද data recovery කරන්න බැරි වෙන නිසා. Soft delete කියන්නේ ආරම්භක පියවරක්, ඒ වගේම system auditability එකටත් උදව් වෙනවා. Sensitive data (email, name) වගේ දේවල් soft delete කරනකොටම anonymize කරන එකත් හොඳ practice එකක්.
4. Privacy-by-Design සහ Best Practices (Privacy-by-Design & Best Practices)
GDPR compliant application එකක් හදන එක කියන්නේ user consent එකයි, data deletion එකයි විතරක් නෙවෙයි. ඒක application එකේ design phase එකේ ඉඳන්ම privacy ගැන හිතලා වැඩ කරන එකක් (Privacy-by-Design). අපි බලමු මේකට අදාළ තවත් best practices කීපයක්.
Regular Data Audits
ඔබේ system එකේ තියෙන data නිවැරදිද, අත්යවශ්යම ද, security measures නිවැරදිව ක්රියාත්මක වෙනවද කියලා නිතරම audit කරන්න. අනවශ්ය data identify කරලා ඒවා delete කිරීම හෝ anonymize කිරීම මේකෙන් කරන්න පුළුවන්.
Logging සහ Auditing
GDPR වලට අදාළ වැදගත් events (උදා: user data delete කිරීම, consent එක වෙනස් කිරීම) log කරලා තියාගන්න එක හරිම වැදගත්. කවුද, කවදා, මොන data වලට මොකක්ද කළේ කියන එක track කරන්න මේක උදව් වෙනවා. Spring Boot Actuator වගේ tools මේකට උදව් වෙන්න පුළුවන්.
Data Protection Impact Assessments (DPIAs)
අලුත් project එකක් පටන් ගන්නකොට හෝ existing system එකකට විශාල වෙනස්කම් කරනකොට DPIA එකක් කරන්න. මේකෙදි, ඔයාගේ system එකෙන් user privacy වලට ඇති වෙන්න පුළුවන් බලපෑම් සහ ඒ අවදානම් අඩු කරගන්න පුළුවන් ක්රම ගැන analyse කරනවා.
Security Measures (ආරක්ෂක ක්රමවේද)
Collect කරන data ආරක්ෂා කරන්න හොඳ security measures භාවිතා කරන්න. Data encryption (at rest and in transit), access controls (කවුද මොන data වලට access කරන්නේ), strong authentication mechanisms (MFA) වගේ දේවල් අත්යවශ්යයි. Spring Security වගේ frameworks මේකට විශාල සහයක් දෙනවා.
Pseudonymization සහ Anonymization
හැකි සෑම විටම user data pseudonymize (දත්ත වලට අන්වර්ථ නාම යෙදීම) හෝ anonymize (දත්ත නිර්නාමික කිරීම) කරන්න. Pseudonymization වලදී data එක direct user කෙනෙක්ට associate කරන්න බැරි වෙන්න වෙනස් කරනවා, හැබැයි වෙනම key එකක් භාවිතා කරලා ආයෙත් original data එකට හරවන්න පුළුවන්. Anonymization වලදී නම් data එක ආයෙත් user කෙනෙක්ට associate කරන්න බැරි වෙන විදියට සම්පූර්ණයෙන්ම වෙනස් කරනවා. මේකෙන් data breach එකක් වුණත් personal data අවභාවිතා කිරීමේ අවදානම අඩු වෙනවා.
Data Minimization (අවශ්යම දත්ත පමණක් එකතු කිරීම)
ඔයාගේ application එකට අත්යවශ්යම data විතරක් collect කරන්න. අනාගතේට ප්රයෝජනවත් වෙයි කියලා හිතලා අනවශ්ය data collect කිරීමෙන් වළකින්න. වැඩිපුර data කියන්නේ වැඩිපුර responsibility එකක්. උදාහරණයක් විදියට, user ගේ birthday එක අවශ්ය නැත්නම් ඒක අහන්න එපා.
අවසන් වශයෙන් (Conclusion)
අද අපි Spring Boot applications වල GDPR compliance ගැන වැදගත් කරුණු කීපයක් ඉගෙන ගත්තා. User data කියන්නේ බැංකු ගිණුමක සල්ලි වගේ වටිනා දෙයක්. ඒ නිසා, developers විදියට අපිට user trust එක රැකගෙන, නීතිමය ප්රශ්න වලින් තොරව applications හදන්න පුළුවන් වෙන්න ඕනේ.
අපි මේ article එකේදී user consent එක manage කරන හැටි, user data soft delete කරන හැටි, ඒ වගේම Privacy-by-Design වගේ concepts ගැන කතා කළා. මේවා අපේ projects වලදී implement කරන එකෙන් අපිට වගේම අපේ users ලාටත් ලොකු සහනයක් ලැබෙනවා. GDPR කියන්නේ EU නීතියක් වුණත්, data privacy කියන concept එක අද ලෝකේ හැමතැනටම අදාළයි. ඉතින්, ඔබේ ඊලඟ Spring Boot project එක හදනකොට මේ GDPR principles ගැන හොඳට හිතලා බලන්න.
මේ tutorial එකෙන් ඔයාට අලුත් දෙයක් ඉගෙන ගන්න ලැබුණා නම්, ඔබේ අත්දැකීම්, ප්රශ්න හෝ අදහස් comment section එකේ share කරන්න. ඊලඟ project එකට මේ concepts add කරලා බලන්න! Data privacy ගැන තවදුරටත් ඉගෙන ගන්න ඔබට තව මොනවාද දැනගන්න ඕනේ?