Spring Boot Vue Admin

repository·master·Indexed 43 days ago

https://github.com/zoctan/spring-boot-vue-admin

A permission management template featuring a separated frontend and backend architecture for role-based access control (RBAC). It utilizes Spring Security and JSON Web Tokens (JWT) for authentication and authorization, providing a system for managing users, roles, and permissions via a Vue.js frontend and Spring Boot backend.

Tokens
5.5K
Snippets
24
Records
27
Agent score
56%

What's inside Spring Boot Vue Admin

  1. Understand the Database Schema for User and Permissions

    master

    The backend uses five primary tables to manage users, roles, and permissions:

    • user: Stores user information.
    • role: Stores role information.
    • user_role: Maps users to roles (One-to-One relationship).
    • permission: Defines the resources and operations (e.g., user:list) that can be accessed.
    • role_permission: Maps roles to permissions (One-to-Many relationship).

    Note on Super Admins: The ROLE_ADMIN role may not have explicit permission entries in the role_permission table. Instead, the system is designed to return all permissions automatically when this role is identified, granting full access without needing exhaustive database records.

  2. Implement Role and Permission Control with Spring Security and JWT

    master

    The project uses Spring Security combined with JSON Web Tokens (JWT) for authentication and authorization.

    Workflow:

    1. User logs in.
    2. Server generates a JWT containing the user's roles and permissions.
    3. Client stores the token and includes it in the header of every subsequent request.
    4. Server parses and validates the token to authorize the request.

    Implementation Details:

    When generating the token, both roles and permissions are added to the authorities claim. For example, a user might have authorities like [ROLE_TEST, role:list, user:list].

    Controller Authorization: You can protect API endpoints using @PreAuthorize annotations. You can check for specific permissions using hasAuthority or specific roles using hasRole.

    // Protect an endpoint by specific permission
    @PreAuthorize("hasAuthority('user:list')")
    
    // Protect an endpoint by role OR permission
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasAuthority('user:list')")
  3. Quick start Spring Boot Vue Admin

    master

    Follow these steps to clone, set up the database, and run both the backend and frontend components of the project.

    1. Clone and Navigate

    git clone https://github.com/Zoctan/spring-boot-vue-admin.git
    cd spring-boot-vue-admin

    2. Backend Setup

    Navigate to the api directory and run the database reset script. Note: You must modify the database information in the script before running it.

    cd api
    sudo chmod a+x resetDB.sh && ./resetDB.sh
    # Start the backend service after database import

    3. Frontend Setup

    Navigate to the app directory, install dependencies, and start the development server.

    cd app
    npm install
    npm run dev
    git clone https://github.com/Zoctan/spring-boot-vue-admin.git
    cd spring-boot-vue-admin
    
    # Backend
    cd api
    sudo chmod a+x resetDB.sh && ./resetDB.sh
    
    # Frontend
    cd app
    npm install
    npm run dev
  4. Quick Start Guide for Spring Boot Vue Admin

    master

    Follow these steps to clone, set up the backend, and run the frontend of the project.

    1. Clone the repository

    git clone https://github.com/Zoctan/spring-boot-vue-admin.git
    cd spring-boot-vue-admin

    2. Backend Setup

    Navigate to the api directory and initialize the database using the provided script. Note: Ensure you modify the database connection information before running the script.

    cd api
    sudo chmod a+x resetDB.sh && ./resetDB.sh
    # Start the backend service as per your local environment configuration

    3. Frontend Setup

    Navigate to the app directory, install dependencies, and start the development server.

    cd app
    npm install
    npm run dev
    git clone https://github.com/Zoctan/spring-boot-vue-admin.git
    cd spring-boot-vue-admin
    
    # Backend
    cd api
    sudo chmod a+x resetDB.sh && ./resetDB.sh
    
    # Frontend
    cd app
    npm install
    npm run dev
  5. Handle CORS and Preflight Requests in JWT Filter

    master

    Because the project uses a decoupled frontend and backend, you must configure Cross-Origin Resource Sharing (CORS) in the JwtAuthenticationFilter.

    Specifically, you must:

    1. Set appropriate Access-Control-* headers on the response.
    2. Handle OPTIONS (preflight) requests by returning immediately with a 200 status code. If the preflight request does not return a 200, the browser will treat the actual request as a failure.
    // core/jwt/JwtAuthenticationFilter.java
    
    // Configure CORS headers
    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Credentials", "true");
    response.setHeader("Access-Control-Allow-Headers", "Content-Type, Content-Length, Authorization, Accept, X-Requested-With");
    response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
    response.setHeader("Access-Control-Max-Age", "3600");
    response.setHeader("Access-Control-Expose-Headers", "*");
    
    // Handle preflight OPTIONS request
    if (request.getMethod().equals("OPTIONS")) {
        return;
    }
  6. Troubleshoot css-loader installation error

    master

    If you encounter an ENOENT error related to ansi-styles or css-loader during npm install (e.g., no such file/ansi-styles/css-loader), follow these steps:

    1. Install css-loader explicitly:
      npm install css-loader
    2. Re-run the project dependency installation:
      npm install
    npm install css-loader
    npm install