diff options
| author | Kamal Wickramanayake <kamal@inbox.lk> | 2026-06-20 18:25:56 +0530 |
|---|---|---|
| committer | Kamal Wickramanayake <kamal@inbox.lk> | 2026-06-20 18:25:56 +0530 |
| commit | 41b068b6d48f9d82babd1cfce3520ad0b28be861 (patch) | |
| tree | 0a9db394d2f14b7f14c3b1d167f56d4dfecd4563 /spring-boot/10-role-based-security/src/main/java/com/example/spring/security/entity | |
| parent | 8c66a82e0caa71c3bb1c1dc40d91272f51c1c0f5 (diff) | |
Added Spring Boot role based security sample application
Diffstat (limited to 'spring-boot/10-role-based-security/src/main/java/com/example/spring/security/entity')
| -rw-r--r-- | spring-boot/10-role-based-security/src/main/java/com/example/spring/security/entity/User.java | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/spring-boot/10-role-based-security/src/main/java/com/example/spring/security/entity/User.java b/spring-boot/10-role-based-security/src/main/java/com/example/spring/security/entity/User.java new file mode 100644 index 0000000..72c413a --- /dev/null +++ b/spring-boot/10-role-based-security/src/main/java/com/example/spring/security/entity/User.java @@ -0,0 +1,147 @@ +package com.example.spring.security.entity; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Date; +import java.util.HashSet; +import java.util.Set; +import java.util.stream.Collectors; + +import org.hibernate.annotations.CreationTimestamp; +import org.hibernate.annotations.UpdateTimestamp; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.core.userdetails.UserDetails; + +import jakarta.persistence.CollectionTable; +import jakarta.persistence.Column; +import jakarta.persistence.ElementCollection; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.Table; + +/* + @Table - Used to set the database table name explicitely. +*/ +@Entity +@Table(name = "user_account") +public class User implements UserDetails { + + /* + * GenerationType.IDENTITY - Auto generate value based on database's native auto + * increment feature + */ + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(unique = true) + private String username; + + private String password; + + @Column(nullable = false) + private String description; + + @CreationTimestamp + @Column(updatable = false, name = "created_at") + private Date createdAt; + + @UpdateTimestamp + @Column(name = "updated_at") + private Date updatedAt; + + /* + * @ElementCollection: Marks the field as a collection of basic types (Strings, + * Integers, etc.) or embeddable objects. + * + * @CollectionTable: Customizes the junction table details. If omitted, JPA + * generates a default table name combining the entity name and the field name + * (e.g., User_roles). + * + * @Column: Defines the column name for the String values inside the collection + * table. + * + * Fetch Type: By default, element collections use FetchType.LAZY. If you access + * the set outside of an active transaction or Hibernate session, it throws a + * LazyInitializationException. You can switch it to fetch = FetchType.EAGER to + * load the strings immediately alongside the main entity. + * + */ + @ElementCollection(fetch = FetchType.EAGER) + @CollectionTable(name = "user_roles", // Name of the separate collection table + joinColumns = @JoinColumn(name = "user_id") // Foreign key linking back to this entity + ) + @Column(name = "role_name") // Name of the column storing the actual String values + private Set<String> roles = new HashSet<>(); // e.g., ["ADMIN", "USER"] + + @Override + public Collection<? extends GrantedAuthority> getAuthorities() { + return roles.stream() + .map(role -> new SimpleGrantedAuthority("ROLE_" + role)) + .collect(Collectors.toList()); + } + + @Override + public String getPassword() { + return password; + } + + @Override + public String getUsername() { + return username; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public void setUsername(String username) { + this.username = username; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public Date getCreatedAt() { + return createdAt; + } + + public void setCreatedAt(Date createdAt) { + this.createdAt = createdAt; + } + + public Date getUpdatedAt() { + return updatedAt; + } + + public void setUpdatedAt(Date updatedAt) { + this.updatedAt = updatedAt; + } + + public Set<String> getRoles() { + return roles; + } + + public void setRoles(Set<String> roles) { + this.roles = roles; + } + +}
\ No newline at end of file |
