Google OAuth2身份验证
要启用Google OAuth2,您必须向Google注册您的应用程序。Google将生成客户端ID和密钥供您使用。
创建Google OAuth密钥
首先,您需要创建Google OAuth客户端:
- 转到https://console.developers.google.com/apis/credentials
- 单击“创建凭据”按钮,然后在下拉菜单中单击“OAuth Client ID”
- 输入以下内容:
- Application Type:WebApplication
- Name:Grafana
- Authorized Javascript Origins:https://grafana.mycompany.com/
- Authorized Redirect URLs:https://grafana.mycompany.com/login/google
- 将https://grafana.mycompany.com替换为Grafana实例的URL。
- 单击“创建”
- 从“OAuth客户端”模式复制客户端ID和客户端密钥
在Grafana中启用Google OAuth
在Grafana配置文件中指定客户端ID和密钥。例如:
[auth.google]
enabled = true
client_id = CLIENT_ID
client_secret = CLIENT_SECRET
scopes = https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email
auth_url = https://accounts.google.com/o/oauth2/auth
token_url = https://accounts.google.com/o/oauth2/token
allowed_domains = mycompany.com mycompany.org
allow_sign_up = true
您可能必须为回调URL设置正确的root_url
选项[server]
。例如,如果您在代理服务器后面提供Grafana服务。
重启Grafana后端。您现在应该在登录页面上看到Google登录按钮。您现在可以登录或注册Google帐户。该allowed_domains
选项是可选的,域由空格分隔。
您可以通过将allow_sign_up
选项设置为允许用户通过Google身份验证进行注册true
。当此选项设置为时true
,任何通过Google身份验证成功进行身份验证的用户都将自动注册。
其他问题:
1. 由于墙的问题,grafana auth.google配置中google的几个网址都给墙掉了。
解决此问题我们使用代理方式:我们公司内部已经有一条vpn线路可以访问所有国外互联网,所有我们在公司内部启动一个nginx代理服务器,grafana所有访问google的网址都改为通过nginx代理访问。
1.1 更改grafana配置文件中的google域名,改为代理的地址:
[auth.google]
enabled = true
client_id = CLIENT_ID
client_secret = CLIENT_SECRET
scopes = https://www.a.com/auth/userinfo.profile https://www.a.com/auth/userinfo.email
auth_url = https://www.b.com/o/oauth2/auth
token_url = https://www.b.com/o/oauth2/token
allowed_domains = mycompany.com mycompany.org
allow_sign_up = true
1.2 先在公司内网使用nginx搭建一个代理:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name www.b.com;
location /auth/ {
proxy_pass https://www.googleapis.com/auth/;
}
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name www.b.com;
location /o/ {
proxy_pass https://accounts.google.com/o/oauth2/token;
}
}