0% found this document useful (0 votes)
5 views6 pages

Spring Boot RBAC Mini Project Guide

Create spring boot project complete guide step by step to build spring boot real world project

Uploaded by

Shahid Ahmed
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Spring Boot RBAC Mini Project Guide

Create spring boot project complete guide step by step to build spring boot real world project

Uploaded by

Shahid Ahmed
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Spring Boot RBAC (Users–Roles–Programs) Mini

Project
This mini project includes: - Project structure - Entities (User, Role, Program) - Repositories - Service layer -
Controllers - Security (JWT optional placeholder) - Sample data loader (Users, Roles, Programs)

📁 Project Structure

src/main/java/com/example/rbac/

├── controller/
│ └── [Link]

├── entity/
│ ├── [Link]
│ ├── [Link]
│ └── [Link]

├── repository/
│ ├── [Link]
│ ├── [Link]
│ └── [Link]

├── service/
│ ├── [Link]
│ └── [Link]

├── config/
│ └── [Link]

└── [Link]

🧱 1. Entities

[Link]

@Entity
@Table(name = "users")
public class User {

1
@Id
@GeneratedValue
private Long id;

private String username;


private String password;

@ManyToMany(fetch = [Link])
@JoinTable(
name = "user_roles",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "role_id")
)
private Set<Role> roles;
}

[Link]

@Entity
@Table(name = "roles")
public class Role {
@Id
@GeneratedValue
private Long id;

private String roleName;

@ManyToMany(fetch = [Link])
@JoinTable(
name = "role_programs",
joinColumns = @JoinColumn(name = "role_id"),
inverseJoinColumns = @JoinColumn(name = "program_id")
)
private Set<Program> programs;
}

[Link]

@Entity
@Table(name = "programs")
public class Program {
@Id
@GeneratedValue
private Long id;

2
private String name;
private String path;
private String icon;
private String category;
}

🗂️ 2. Repositories

[Link]

public interface UserRepository extends JpaRepository<User, Long> {


User findByUsername(String username);
}

[Link]

public interface RoleRepository extends JpaRepository<Role, Long> { }

[Link]

public interface ProgramRepository extends JpaRepository<Program, Long> { }

🛠️ 3. Service Layer

[Link]

public interface UserService {


User findByUsername(String username);
}

[Link]

@Service
public class UserServiceImpl implements UserService {

@Autowired
private UserRepository userRepository;

3
@Override
public User findByUsername(String username) {
return [Link](username);
}
}

🌐 4. Controller

[Link]

@RestController
@RequestMapping("/api/user")
public class UserController {

@Autowired
private UserService userService;

@GetMapping("/menu/{username}")
public Set<Program> getMenu(@PathVariable String username) {
User user = [Link](username);

return [Link]().stream()
.flatMap(role -> [Link]().stream())
.collect([Link]());
}
}

▶️ 5. Data Loader (Auto Insert Sample Data)

[Link]

@Component
public class DataLoader implements CommandLineRunner {

@Autowired
private UserRepository userRepository;
@Autowired
private RoleRepository roleRepository;
@Autowired
private ProgramRepository programRepository;

4
@Override
public void run(String... args) {
// Programs
Program p1 = new Program(null, "Dashboard", "/dashboard", "dashboard-
icon", "MAIN");
Program p2 = new Program(null, "Users", "/users", "user-icon", "ADMIN");
Program p3 = new Program(null, "Reports", "/reports", "report-icon",
"ADMIN");
[Link]([Link](p1, p2, p3));

// Roles
Role admin = new Role();
[Link]("ADMIN");
[Link]([Link](p1, p2, p3));
[Link](admin);

Role user = new Role();


[Link]("USER");
[Link]([Link](p1));
[Link](user);

// Users
User u1 = new User();
[Link]("shahid");
[Link]("12345");
[Link]([Link](admin));

User u2 = new User();


[Link]("ahmed");
[Link]("12345");
[Link]([Link](user));

[Link]([Link](u1, u2));
}
}

🔥 6. Output for React


Calling:

GET /api/user/menu/shahid

Returns JSON:

5
[
{ "name": "Dashboard", "path": "/dashboard" },
{ "name": "Users", "path": "/users" },
{ "name": "Reports", "path": "/reports" }
]

React uses this to build dynamic menus.

🎯 Next Steps
I can add: - JWT login - Menu grouping - React sample menu component - SQL scripts for Oracle/MySQL -
Postman collection

Tell me what you want next!

You might also like