简体   繁体   English

带有其他HTTP标头的WordPress反向代理身份验证

[英]WordPress reverse proxy authentication with additional http headers

I want to log in to WordPress dashboard with http X-User header provided by reverse proxy server. 我想使用反向代理服务器提供的http X-User标头登录WordPress仪表板。

I'm planning to use WordPress as a CMS where our organization members can freely write articles. 我计划使用WordPress作为CMS,我们的组织成员可以在其中自由撰写文章。 To maximize user experience, I want to allow users to login to the dashboard with our organization account. 为了最大化用户体验,我希望允许用户使用我们的组织帐户登录到仪表板。 Our servers are constructed on Docker containers behind one nginx reverse proxy server container, which checks the request by questioning to the authentication server, and notify the application server by adding X-User header. 我们的服务器构建在一个nginx反向代理服务器容器后面的Docker容器上,该容器通过向身份验证服务器提问来检查请求,并通过添加X-User标头通知应用程序服务器。

Here's my nginx.conf 这是我的nginx.conf

location /_login {
  internal;
  proxy_pass http://auth-server/path/to/api;
}
location @unauthorized {
  internal;
  return 302 http://auth-client.example.com/path/to/login-form
}
location / {
  auth_request /_login;
  auth_request_set $user $upstream_http_x_user; 
  proxy_set_header X-User $user;
  proxy_pass http://wordpress/;
  error_page 403 @unauthorized;
}

Is there any appropriate plugin to do that? 有没有合适的插件可以做到这一点? Or I have to create a new plugin? 还是我必须创建一个新插件?

Finally I found a solution. 终于我找到了解决方案。

Create wp-content/plugins/some-plugin.php : 创建wp-content/plugins/some-plugin.php

<?php
/*
Plugin Name: Some Plugin
*/

add_action('login_init', function() {
    if ($_SERVER['HTTP_X_USER']) {
        $user = get_user_by('login', $_SERVER['HTTP_X_USER']);
        if ($user) {
            wp_clear_auth_cookie();
            wp_set_auth_cookie($user->ID);
            do_action('wp_login', $user->login, $user);
            wp_safe_redirect(isset($_GET['redirect_to']) ? $_GET['redirect_to'] : admin_url());
            exit;
        }
    }
}, 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM