hsweb-framework

repository·5.0.x·Indexed 27 days ago

https://github.com/hs-web/hsweb-framework

hsweb4 is a fully reactive administrative management framework built on Spring Boot 2, Spring WebFlux, and R2DBC. It provides a modular approach to building enterprise-grade back-office systems with built-in support for RBAC, reactive CRUD, and system management. Key features include the FastBeanCopier for high-performance bean copying, EnumDict for data dictionary definitions, a comprehensive authorization module for RBAC and data-level permission control, dynamic data source switching, and an AOP-based access logging system.

Tokens
5.2K
Snippets
9
Records
31
Agent score
94%

What's inside hsweb-framework

  1. Overview of hsweb4 framework

    5.0.x

    hsweb4 is a fully reactive administrative management framework built on top of Spring Boot 2 and Spring WebFlux. It is designed for building modular, extensible, and decoupled (frontend-backend separation) administrative systems.

    Key Features

    • Reactive CRUD: Uses r2dbc and easy-orm for universal reactive CRUD operations. Supports H2, MySQL, SQL Server, and PostgreSQL.
    • Reactive Transaction Control: Full support for R2DBC transactions.
    • Reactive Permission Control: Includes RBAC (Role-Based Access Control), data-level permission control, and multi-dimensional permission management.
    • Reactive Caching: Built-in reactive caching capabilities.
    • Built-in Business Functions: Includes user management, permission settings/allocation, static file uploads, and data dictionaries.
  2. Overview of the hsweb-authorization module

    5.0.x

    The hsweb-authorization module provides the authorization and authentication management for the entire system. It is divided into two primary components:

    1. hsweb-authorization-api: Defines the APIs for permission control.
    2. hsweb-authorization-basic: Provides the basic implementation of the permission control logic.
  3. Overview of hsweb-system-authorization features

    5.0.x

    The hsweb-system-authorization module provides a comprehensive permission management system with the following capabilities:

    • User and Role Management: Provides basic functions for managing users and roles.
    • Multi-dimensional Permission Allocation: Unlike traditional RBAC where permissions are directly linked to users or roles, this module allows for extensible permission allocation across various dimensions such as Users, Roles, Organizations (机构), Departments (部门), or Positions (岗位).
    • Granular Access Control: Supports standard RBAC as well as custom controls down to the data row and column levels.
    • System Menu Management: Provides tools for managing system menus.
  4. Configure and use Two-Factor Authentication (2FA)

    5.0.x

    Enable Two-Factor Authentication by configuring application.yml and annotating specific controller methods.

    1. Enable in configuration:

    hsweb:
        authorize:
            two-factor:
                enable: true

    2. Apply to controller methods: Use the @TwoFactor annotation on the target endpoint. Provide a unique identifier (e.g., "update-password") for the verification step.

    @PostMapping
    @TwoFactor("update-password")
    public ResponseMessage<Boolean> updatePassword(String password){
        // implementation
    }
    @PostMapping
    @TwoFactor("update-password")
    public ResponseMessage<Boolean> updatePassword(String password){
        
        //
    }
  5. Annotate Controller methods for Access Logging

    5.0.x
    To record access logs for specific controller classes or methods, use the @AccessLogger annotation. Provide a functional description as the argument. If you are using Swagger, you can also use the standard Swagger @Api annotation (e.g., @Api(tags="...")) to provide functional descriptions.
  6. Implement Data Permission control

    5.0.x

    Data permissions work by intercepting AOP method arguments and reconstructing them based on the user's permission scope.

    Requirements:

    • Methods performing dynamic queries must include a QueryParamEntity parameter.
    • Controllers should implement the common controller interfaces provided in hsweb-commons-controller.

    Example Logic: If a user has permission (org) to query only their own organization and subordinates, the framework intercepts the QueryParamEntity and modifies the SQL condition.

    Original client query: where name like ? or full_name like

    Reconstructed query: where u_id in(?,?,?) and (name like ? or full_name like)

  7. Prerequisites and Recommended Usage for hsweb4

    5.0.x

    Prerequisites

    Before using hsweb, you should have a working knowledge of:

    Do not clone and modify the entire repository directly. Instead, use hsweb as a Maven dependency. You should select only the specific modules you need for your project. Once released, all modules will be available in the Maven Central Repository.

  8. Implement custom data access control

    5.0.x

    To extend the data access control system with custom logic, you must implement two components: a configuration converter and a data access handler. This allows you to transform frontend configuration strings into structured configuration objects and then apply authorization logic during API execution.

    1. Create a Configuration Converter

    Implement the DataAccessConfigConvert interface to define how a specific configuration type is parsed. The isSupport method determines if the converter should handle a given type, action, and config string, while convert transforms the raw string into a DataAccessConfig object.

    2. Create a Data Access Handler

    Implement the DataAccessHandler interface to define the actual authorization logic. The isSupport method checks if the provided DataAccessConfig matches your custom type. The handle method is called during the request intercept; it provides a MethodInterceptorParamContext which contains the method arguments (via getNamedArguments()). Return true to allow the request or false to deny it.

    // 1. Implement the converter
    @org.springframework.stereotype.Component
    public class MyDataAccessConfigConvert implements DataAccessConfigConvert {
    
        @Override
        public boolean isSupport(String type, String action, String config) {
            return "custom_type".equals(type);
        }
    
        @Override
        public DataAccessConfig convert(String type, String action, String config) {
            MyDataAccessConfig accessConfig = JSON.parseObject(config, MyDataAccessConfig.class);
            accessConfig.setAction(action);
            accessConfig.setType(type);
            return accessConfig;
        }
    }
    
    // 2. Implement the handler
    @org.springframework.stereotype.Component
    public class MyDataAccessHandler implements org.hswebframework.web.authorization.access.DataAccessHandler {
        
            @Override
            public boolean isSupport(DataAccessConfig access) {
                return "custom_type".equals(access.getType());
            }
        
            @Override
            public boolean handle(DataAccessConfig access, MethodInterceptorParamContext context) {
               // Access method arguments via context
               Map<String, Object> param = context.getNamedArguments();
               // Implement authorization logic here
               return true;
            }
    }
  9. Implement custom authorization logic using UserOnSignIn

    5.0.x

    To handle user authorization events, implement the UserOnSignIn listener provided by hsweb-authorization-api. This listener reacts to the AuthorizationSuccessEvent after a user successfully authenticates.

    Workflow:

    1. Authentication completes and triggers AuthorizationSuccessEvent.
    2. UserOnSignIn receives the event and retrieves the token_type (defaults to sessionId) and authorization info.
    3. Generate a token based on the token_type.
    4. Register the token and the userId from the authorization info into the UserTokenManager.
    5. Return the token to the authorization interface.