跳转至

配置#

功能可用性

Embed 需要嵌入许可证。有关何时使用 Embed 以及成本和许可流程的更多信息,请参阅 n8n 网站上的 Embed

身份验证#

您可以通过设置用户管理来保护n8n的安全,这是n8n的内置身份验证功能。

n8n支持LDAPSAML

凭证覆写#

为了向用户提供OAuth登录,可以在全局基础上覆写凭证。这些凭证数据对用户不可见,但后端会自动使用它们。

在编辑器UI中,n8n默认隐藏所有被覆写的字段。这意味着用户可以通过按下凭证上的“连接”按钮使用OAuth进行身份验证。

n8n提供了两种应用凭证覆写的方式:使用环境变量和使用REST API。

使用环境变量#

您可以通过将CREDENTIALS_OVERWRITE_DATA设置为{ CREDENTIAL_NAME: { PARAMETER: VALUE }}来使用环境变量设置凭证覆写。

Warning

尽管这是可能的,但不推荐这样做。环境变量在n8n中没有受到保护,因此数据可能会泄露给用户。

使用REST API#

推荐的方式是使用自定义REST端点来加载数据。将CREDENTIALS_OVERWRITE_ENDPOINT设置为一个路径,在该路径下可以提供此端点。

Note

出于安全原因,端点一次只能调用一个。

例如:

  1. 通过在n8n运行的环境中设置环境变量来激活端点:

    1
    export CREDENTIALS_OVERWRITE_ENDPOINT=send-credentials
    
  2. 然后需要一个包含要覆写的凭证的JSON文件。例如,用于覆写Asana和GitHub凭证的oauth-credentials.json文件可以如下所示:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    {
        "asanaOAuth2Api": {
            "clientId": "<id>",
            "clientSecret": "<secret>"
        },
        "githubOAuth2Api": {
            "clientId": "<id>",
            "clientSecret": "<secret>"
        }
    }
    
  3. 然后通过使用curl发送将其应用到实例:

    1
    curl -H "Content-Type: application/json" --data @oauth-credentials.json http://localhost:5678/send-credentials
    

Note

有些情况下凭证是基于其他凭证的。例如,googleSheetsOAuth2Api继承了googleOAuth2Api。 在这种情况下,您可以在父凭证(googleOAuth2Api)上设置参数,供所有子凭证(googleSheetsOAuth2Api)使用。

环境变量#

n8n有许多您可以配置的环境变量。以下是与您的托管解决方案最相关的环境变量:

变量 类型 默认值 描述
EXECUTIONS_TIMEOUT Number -1 为所有工作流设置默认超时时间(秒),超时后n8n停止其执行。用户可以为单个工作流覆写此设置,最高不超过EXECUTIONS_TIMEOUT_MAX中设置的持续时间。将EXECUTIONS_TIMEOUT设置为-1可禁用。
EXECUTIONS_DATA_PRUNE Boolean true 是否按滚动基础删除过去执行的数据。
EXECUTIONS_DATA_MAX_AGE Number 336 删除前执行的年龄(小时)。
EXECUTIONS_DATA_PRUNE_MAX_COUNT Number 10000 数据库中保存的最大执行数。0 = 无限制
NODES_EXCLUDE Array of strings - 指定不加载哪些节点。例如,如果用户不可信赖,可以阻止可能存在安全风险的节点:NODES_EXCLUDE: "[\"n8n-nodes-base.executeCommand\", \"n8n-nodes-base.readWriteFile\"]"
NODES_INCLUDE Array of strings - 指定要加载哪些节点。
N8N_TEMPLATES_ENABLED Boolean true 启用工作流模板 (true) 或禁用 (false)。
N8N_TEMPLATES_HOST String https://api.n8n.io 如果创建您自己的工作流模板库,请更改此项。注意,要使用您自己的工作流模板库,您的API必须提供与n8n相同的端点和响应结构。有关更多信息,请参阅工作流模板

后端钩子#

可以定义外部钩子,n8n在特定操作运行时执行这些钩子。您可以使用这些钩子,例如记录数据、更改数据或通过抛出错误来禁止操作。

可用钩子#

Hook Arguments Description
credentials.create [credentialData: ICredentialsDb] Called before new credentials get created. Use to restrict the number of credentials.
credentials.delete [id: credentialId] Called before credentials get deleted.
credentials.update [credentialData: ICredentialsDb] Called before existing credentials are saved.
frontend.settings [frontendSettings: IN8nUISettings] Gets called on n8n startup. Allows you to, for example, overwrite frontend data like the displayed OAuth URL.
n8n.ready [app: App] Called once n8n is ready. Use to, for example, register custom API endpoints.
n8n.stop Called when an n8n process gets stopped. Allows you to save some process data.
oauth1.authenticate [oAuthOptions: clientOAuth1.Options, oauthRequestData: {oauth_callback: string}] Called before an OAuth1 authentication. Use to overwrite an OAuth callback URL.
oauth2.callback [oAuth2Parameters: {clientId: string, clientSecret: string \| undefined, accessTokenUri: string, authorizationUri: string, redirectUri: string, scopes: string[]}] Called in an OAuth2 callback. Use to overwrite an OAuth callback URL.
workflow.activate [workflowData: IWorkflowDb] Called before a workflow gets activated. Use to restrict the number of active workflows.
workflow.afterDelete [workflowId: string] Called after a workflow gets deleted.
workflow.afterUpdate [workflowData: IWorkflowBase] Called after an existing workflow gets saved.
workflow.create [workflowData: IWorkflowBase] Called before a workflow gets created. Use to restrict the number of saved workflows.
workflow.delete [workflowId: string] Called before a workflow gets delete.
workflow.postExecute [run: IRun, workflowData: IWorkflowBase] Called after a workflow gets executed.
workflow.preExecute [workflow: Workflow: mode: WorkflowExecuteMode] Called before a workflow gets executed. Allows you to count or limit the number of workflow executions.
workflow.update [workflowData: IWorkflowBase] Called before an existing workflow gets saved.

Registering hooks#

Set hooks by registering a hook file that contains the hook functions. To register a hook, set the environment variable EXTERNAL_HOOK_FILES.

You can set the variable to a single file:

EXTERNAL_HOOK_FILES=/data/hook.js

Or to contain multiple files separated by a semicolon:

EXTERNAL_HOOK_FILES=/data/hook1.js;/data/hook2.js

Backend hook files#

Hook files are regular JavaScript files that have the following format:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
module.exports = {
    "frontend": {
        "settings": [
            async function (settings) {
                settings.oauthCallbackUrls.oauth1 = 'https://n8n.example.com/oauth1/callback';
                settings.oauthCallbackUrls.oauth2 = 'https://n8n.example.com/oauth2/callback';
            }
        ]
    },
    "workflow": {
        "activate": [
            async function (workflowData) {
                const activeWorkflows = await this.dbCollections.Workflow.count({ active: true });

                if (activeWorkflows > 1) {
                    throw new Error(
                        'Active workflow limit reached.'
                    );
                }
            }
        ]
    }
}

Backend hook functions#

A hook or a hook file can contain multiple hook functions, with all functions executed one after another.

If the parameters of the hook function are objects, it's possible to change the data of that parameter to change the behavior of n8n.

You can also access the database in any hook function using this.dbCollections (refer to the code sample in Backend hook files.

Frontend external hooks#

Like backend external hooks, it's possible to define external hooks in the frontend code that get executed by n8n whenever a user performs a specific operation. You can use them, for example, to log data and change data.

Available hooks#

Hook Description
credentialsEdit.credentialTypeChanged Called when an existing credential's type changes.
credentials.create Called when someone creates a new credential.
credentialsList.dialogVisibleChanged
dataDisplay.nodeTypeChanged
dataDisplay.onDocumentationUrlClick Called when someone selects the help documentation link.
execution.open Called when an existing execution opens.
executionsList.openDialog Called when someone selects an execution from existing Workflow Executions.
expressionEdit.itemSelected
expressionEdit.dialogVisibleChanged
nodeCreateList.filteredNodeTypesComputed
nodeCreateList.nodeFilterChanged Called when someone makes any changes to the node panel filter.
nodeCreateList.selectedTypeChanged
nodeCreateList.mounted
nodeCreateList.destroyed
nodeSettings.credentialSelected
nodeSettings.valueChanged
nodeView.createNodeActiveChanged
nodeView.addNodeButton
nodeView.createNodeActiveChanged
nodeView.mount
pushConnection.executionFinished
showMessage.showError
runData.displayModeChanged
workflow.activeChange
workflow.activeChangeCurrent
workflow.afterUpdate Called when someone updates an existing workflow.
workflow.open
workflowRun.runError
workflowRun.runWorkflow Called when a workflow executes.
workflowSettings.dialogVisibleChanged
workflowSettings.saveSettings Called when someone saves the settings of a workflow.

Registering hooks#

You can set hooks by loading the hooks script on the page. One way to do this is by creating a hooks file in the project and adding a script tag in your editor-ui/public/index.html file:

1
<script src="frontend-hooks.js"></script>

Frontend hook files#

Frontend external hook files are regular JavaScript files which have the following format:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
window.n8nExternalHooks = {
  nodeView: {
    mount: [
      function (store, meta) {
        // do something
      },
    ],
    createNodeActiveChanged: [
      function (store, meta) {
        // do something
      },
      function (store, meta) {
        // do something else
      },
    ],
    addNodeButton: [
      function (store, meta) {
        // do something
      },
    ],
  },
};

Frontend hook functions#

You can define multiple hook functions per hook. Each hook function is invoked with the following arguments arguments:

  • store: The Vuex store object. You can use this to change or get data from the store.
  • metadata: The object that contains any data provided by the hook. To see what's passed, search for the hook in the editor-ui package.
此页面是否
💬 微信

🚀 与作者交流

关注公众号
n8n实战笔记公众号
n8n实战笔记
📚 教程 💡 案例 🔧 技巧
添加微信
添加作者微信
1对1 专业指导
⚡ 快答 🎯 定制 🚀 支持