当前位置: 首页 > 知识库问答 >
问题:

Nginx和Oauth2-proxy:使用Google登录后,重定向回Oauth登录页面

高宸
2023-03-14

我正在尝试使用Oauth2代理作为使用Google身份验证的网站的网关。出现Oauth登录页面,您可以单击“登录”,这将带您进入Google登录页面,但登录后它会直接重定向回Oauth登录页面。我怀疑这与cookie有关,但我不知道我做错了什么。

NGINX配置:

server {
  listen 443;
  server_name my.website.com;

  ssl on;
  ssl_certificate /etc/ssl/my.website.com.pem;
  ssl_certificate_key /etc/ssl/my.website.com.key;

  location /oauth2/ {
    proxy_pass       http://127.0.0.1:4180;
    proxy_set_header Host                    $host;
    proxy_set_header X-Real-IP               $remote_addr;
    proxy_set_header X-Scheme                $scheme;
    proxy_set_header X-Auth-Request-Redirect $request_uri;
  }
  location = /oauth2/auth {
    proxy_pass       http://127.0.0.1:4180;
    proxy_set_header Host             $host;
    proxy_set_header X-Real-IP        $remote_addr;
    proxy_set_header X-Scheme         $scheme;
    proxy_set_header Content-Length   "";
    proxy_pass_request_body           off;
  }

  location / {
    auth_request /oauth2/auth;
    error_page 401 = /oauth2/sign_in;

    # pass information via X-User and X-Email headers to backend,
    # requires running with --set-xauthrequest flag
    auth_request_set $user   $upstream_http_x_auth_request_user;
    auth_request_set $email  $upstream_http_x_auth_request_email;
    proxy_set_header X-User  $user;
    proxy_set_header X-Email $email;

    # if you enabled --pass-access-token, this will pass the token to the backend
    auth_request_set $token  $upstream_http_x_auth_request_access_token;
    proxy_set_header X-Access-Token $token;

    # if you enabled --cookie-refresh, this is needed for it to work with auth_request
    auth_request_set $auth_cookie $upstream_http_set_cookie;
    add_header Set-Cookie $auth_cookie;

    # When using the --set-authorization-header flag, some provider's cookies can exceed the 4kb
    # limit and so the OAuth2 Proxy splits these into multiple parts.
    # Nginx normally only copies the first `Set-Cookie` header from the auth_request to the respon$
    # so if your cookies are larger than 4kb, you will need to extract additional cookies manually.
    auth_request_set $auth_cookie_name_upstream_1 $upstream_cookie_auth_cookie_name_1;

    # Extract the Cookie attributes from the first Set-Cookie header and append them
    # to the second part ($upstream_cookie_* variables only contain the raw cookie content)
    if ($auth_cookie ~* "(; .*)") {
        set $auth_cookie_name_0 $auth_cookie;
        set $auth_cookie_name_1 "auth_cookie_name_1=$auth_cookie_name_upstream_1$1";
    }

    # Send both Set-Cookie headers now if there was a second part
    if ($auth_cookie_name_upstream_1) {
        add_header Set-Cookie $auth_cookie_name_0;
        add_header Set-Cookie $auth_cookie_name_1;
    }
    
    proxy_set_header   X-Forwarded-For $remote_addr;
    proxy_set_header   Host $http_host;
    proxy_pass         "http://127.0.0.1:8888"; # This is where my web server is hosted
  }
}

oauth 2-代理配置:

cookie_secret="" # Filled in with a base64 string
provider="google"
email_domains="*"
client_secret="" # Filled in with Google auth client secret
client_id="" # Filled in with Google auth client ID
cookie_secure="false" # No idea what to set this as, tried both false and true, same result
redirect_url="https://my.website.com/oauth2/auth/"
upstreams="http://127.0.0.1:8888/" # My website server

Oauth2-proxy 启动命令:

~/oauth2-proxy-v6.1.1.linux-amd64/oauth2-proxy --config=/home/username/work/src/github.com/oauth2-proxy/oauth2-proxy/contrib/local-environment/oauth2-proxy.cfg

任何想法将不胜感激!

共有3个答案

黄信厚
2023-03-14

您将返回到 oauth 页面,因为您已设置(在 Oauth2-proxy 配置中)redirect_url=“https://my.website.com/oauth2/auth/”

例如,将其更改为仅 https://my.website.com/,以登陆您的网站主屏幕。

贺靖
2023-03-14

我接受的答案是正确的,但我也不得不做一些cookie更改,并认为我应该发布我的最终工作代码。

我能够通过这些变化使其发挥作用:

位置 /oauth2/下:

< code > proxy _ set _ header X-Auth-Request-Redirect " https://my . website . com ";

在< code>location /下:

auth_request_set$auth_cookie$upstream_http_set_cookie

在 Oauth2-proxy 配置(附加或替换行)中:

redirect_url = "https://my.website.com/oauth2/callback"
set_xauthrequest = true
upstreams = ["file:///dev/null"]
cookie_domains = [".website.com"]
cookie_secure = false
cookie_samesite = "lax"

编辑:配置文件的最终内容:

NGINX配置:

server {
  listen 443;
  server_name my.website.com;

  ssl on;
  ssl_certificate /etc/ssl/my.website.com.pem;
  ssl_certificate_key /etc/ssl/my.website.com.key;

  location /oauth2/ {
    proxy_pass       http://127.0.0.1:4180;
    proxy_set_header Host                    $host;
    proxy_set_header X-Real-IP               $remote_addr;
    proxy_set_header X-Scheme                $scheme;
    proxy_set_header X-Auth-Request-Redirect "https://my.website.com";
  }

  location = /oauth2/auth {
    proxy_pass       http://127.0.0.1:4180;
    proxy_set_header Host             $host;
    proxy_set_header X-Real-IP        $remote_addr;
    proxy_set_header X-Scheme         $scheme;
    proxy_set_header Content-Length   "";
    proxy_pass_request_body           off;
  }

  location / {
    auth_request /oauth2/auth;
    error_page 401 = /oauth2/sign_in;

    auth_request_set $user   $upstream_http_x_auth_request_user;
    auth_request_set $email  $upstream_http_x_auth_request_email;
    proxy_set_header X-User  $user;
    proxy_set_header X-Email $email;

    auth_request_set $token  $upstream_http_x_auth_request_access_token;
    proxy_set_header X-Access-Token $token;

    auth_request_set $auth_cookie $upstream_http_set_cookie;
    add_header Set-Cookie $auth_cookie;

    auth_request_set $auth_cookie_name_upstream_1 $upstream_cookie_auth_cookie_name_1;

    if ($auth_cookie ~* "(; .*)") {
        set $auth_cookie_name_0 $auth_cookie;
        set $auth_cookie_name_1 "auth_cookie_name_1=$auth_cookie_name_upstream_1$1";
    }

    if ($auth_cookie_name_upstream_1) {
        add_header Set-Cookie $auth_cookie_name_0;
        add_header Set-Cookie $auth_cookie_name_1;
    }
    
    proxy_set_header   X-Forwarded-For $remote_addr;
    proxy_set_header   Host $http_host;
    proxy_pass         "http://127.0.0.1:8888"; # This is where my web server is hosted
  }
}

oauth 2-代理配置:

cookie_secret="" # Filled in with a base64 string
provider="google"
email_domains="*"
client_secret="" # Filled in with Google auth client secret
client_id="" # Filled in with Google auth client ID
cookie_domains=[".website.com"]
cookie_secure="false"
cookie_samesite="lax"
redirect_url="https://my.website.com/oauth2/callback"
upstreams="http://127.0.0.1:8888/" # My website server
set_xauthrequest=true
upstreams=["file:///dev/null"]

郏瀚
2023-03-14

根据文档--重定向url是OAuth重定向url,应设置为受支持的回调endpoint,例如“https://internalapp.yourcompany.com/oauth2/callback".

/oauth2/callback endpoint - 在 OAuth 周期结束时使用的 URL。oauth 应用将使用此作为回调 url 进行配置。

实际生成的url有一个状态参数,其中包括要返回的原始URL。据我所知,回调从状态中检索原始(受代理保护)url并将浏览器重定向到它。

 类似资料:
  • 问题内容: 我正在做一个简单的论坛,由一系列论坛组成,每个论坛分别代表首页,主题,postitit,登录名和用户列表页面。在某些页面上,当用户未登录时会出现一个链接。 我想要实现的是在登录后触发重定向(在RequestDispatcher上使用forward()),以便浏览器返回到用户在单击登录链接之前所在的页面。为此,我看到了两种解决方案。 第一种解决方案是使用带有登录按钮的HTML 和一个不可

  • 记录器文件中的日志- org.springframework.Security.Access.event.loggerlistener-安全授权失败,原因是:org.springframework.Security.Access.accessdeniedexception:访问被拒绝;通过身份验证的主体:org.springframework.security.authentication.ano

  • 我在Laravel5.2中使用了php artisan函数。 我想重定向的客人登录页面,若客人点击链接,只有用户并没有客人。 我想在登录后将用户重定向到后面的页面。 我怎么能这么做?请详细展示一些文件名示例。 ///////编辑 路线 控制器 Kernel.php

  • 我使用的是Spring Security 4.1.1,但我遇到了一个问题:我试图访问URL,应用程序会重定向到登录页面。到现在为止,一直都还不错。 但是,成功登录后,应用程序会再次将我重定向到登录页面,并且不会创建任何会话,因此即使尝试直接访问URL(在URL栏中键入),应用程序也会重定向到登录页面。 有一些URL我必须要求登录才能访问它们。其他的,我可以访问无需身份验证。这些我不需要验证的URL

  • 这是views.py的内容: 在转到“/”时,我被重定向到“/accounts/login”,它将我带到登录页面。我输入用户名和密码后,在“验证”打印语句printd“登录”到终端。 到目前为止还不错。现在,我不再被重定向到“/”,而是再次被重定向到“/accounts/login”,并再次显示输入用户名和密码的页面。为什么?

  • 我想在登录到我的索引页面(index.jsp)后进行重定向,实际上我的登录可以正常工作,但ajax支持缺少重定向。 她是我的登录名。jsp 这是我的登录 我的struts配置文件: 对于DAO部分是: 公共字符串验证(字符串用户名,字符串userpass)抛出SQLException,Exception{ 谢谢你的帮助。