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;
}
}