Grafana - Configure generic OAuth2 authentication

Configure generic OAuth2 authentication

There are numerous authentication methods available in Grafana to verify user identity. The authentication configuration dictates which users can access Grafana and the methods they can use for logging in. You can also configure Grafana to automatically update users’ roles and team memberships in Grafana based on the information returned by the auth provider integration.

When deciding on an authentication method, it’s important to take into account your current identity and access management system as well as the specific authentication and authorization features you require. For a complete list of the available authentication options and the features they support, refer to Configure authentication.

Grafana provides OAuth2 integrations for the following auth providers:

If your OAuth2 provider is not listed, you can use generic OAuth2 authentication.

This topic describes how to configure generic OAuth2 authentication using different methods and includes examples of setting up generic OAuth2 with specific OAuth2 providers.

Before you begin

To follow this guide:

  • Ensure you know how to create an OAuth2 application with your OAuth2 provider. Consult the documentation of your OAuth2 provider for more information.
  • Ensure your identity provider returns OpenID UserInfo compatible information such as the sub claim.
  • If you are using refresh tokens, ensure you know how to set them up with your OAuth2 provider. Consult the documentation of your OAuth2 provider for more information.

Configure generic OAuth authentication client using the Grafana UI

Note

Available in Public Preview in Grafana 10.4 behind the ssoSettingsApi feature toggle.

As a Grafana Admin, you can configure Generic OAuth2 client from within Grafana using the Generic OAuth UI. To do this, navigate to Administration > Authentication > Generic OAuth page and fill in the form. If you have a current configuration in the Grafana configuration file then the form will be pre-populated with those values otherwise the form will contain default values.

After you have filled in the form, click Save to save the configuration. If the save was successful, Grafana will apply the new configurations.

If you need to reset changes you made in the UI back to the default values, click Reset. After you have reset the changes, Grafana will apply the configuration from the Grafana configuration file (if there is any configuration) or the default values.

Note

If you run Grafana in high availability mode, configuration changes may not get applied to all Grafana instances immediately. You may need to wait a few minutes for the configuration to propagate to all Grafana instances.

Refer to configuration options for more information.

Configure generic OAuth authentication client using the Terraform provider

Note

Available in Public Preview in Grafana 10.4 behind the ssoSettingsApi feature toggle. Supported in the Terraform provider since v2.12.0.

terraform

resource "grafana_sso_settings" "generic_sso_settings" {
  provider_name = "generic_oauth"
  oauth2_settings {
    name              = "Auth0"
    auth_url          = "https://<domain>/authorize"
    token_url         = "https://<domain>/oauth/token"
    api_url           = "https://<domain>/userinfo"
    client_id         = "<client id>"
    client_secret     = "<client secret>"
    allow_sign_up     = true
    auto_login        = false
    scopes            = "openid profile email offline_access"
    use_pkce          = true
    use_refresh_token = true
  }
}

Refer to Terraform Registry for a complete reference on using the grafana_sso_settings resource.

Configure generic OAuth authentication client using the Grafana configuration file

Ensure that you have access to the Grafana configuration file.

Steps

To integrate your OAuth2 provider with Grafana using our generic OAuth2 authentication, follow these steps:

  1. Create an OAuth2 application in your chosen OAuth2 provider.

  2. Set the callback URL for your OAuth2 app to http://<my_grafana_server_name_or_ip>:<grafana_server_port>/login/generic_oauth.

    Ensure that the callback URL is the complete HTTP address that you use to access Grafana via your browser, but with the appended path of /login/generic_oauth.

    For the callback URL to be correct, it might be necessary to set the root_url option in the [server]section of the Grafana configuration file. For example, if yo

### Spring Security OAuth2 配置指南 #### 1. 授权服务器 (Authorization Server) 为了配置授权服务器,可以使用 `spring-security-oauth2-authorization-server` 提供的功能。以下是具体的步骤: 引入必要的依赖项: ```xml <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-oauth2-authorization-server</artifactId> <version>1.0.0</version> </dependency> ``` 创建一个类来启用授权服务器功能并定义令牌端点和其他必要设置: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.oauth2.server.authorization.config.ProviderSettings; @Configuration(proxyBeanMethods = false) public class AuthorizationServerConfig { @Bean public ProviderSettings providerSettings() { return ProviderSettings.builder() .issuer("https://example.com") // 设置发行者URL .build(); } protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests(authorize -> authorize.anyRequest().authenticated()) .oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt); } } ``` 上述代码片段展示了如何通过自定义 `ProviderSettings` 来指定授权服务器的相关参数[^1]。 --- #### 2. 客户端 (Client Configuration) 要使应用程序作为OAuth2客户端运行,需添加以下依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-client</artifactId> </dependency> ``` 在 `application.yml` 或 `application.properties` 文件中提供客户端的具体配置信息: ```yaml spring: security: oauth2: client: registration: google: clientId: your-google-client-id clientSecret: your-google-client-secret redirectUri: "{baseUrl}/login/oauth2/code/{registrationId}" scope: - email - profile provider: google: authorization-uri: https://accounts.google.com/o/oauth2/v2/auth token-uri: https://www.googleapis.com/oauth2/v4/token user-info-uri: https://www.googleapis.com/oauth2/v3/userinfo jwk-set-uri: https://www.googleapis.com/oauth2/v3/certs ``` 此部分描述了如何注册外部身份提供商(如Google),以及如何获取用户的电子邮件和个人资料数据[^2]。 --- #### 3. 资源服务器 (Resource Server) 资源服务器负责验证传入请求中的访问令牌,并保护受控API免遭未经授权的访问。为此,应加入如下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-resource-server</artifactId> </dependency> ``` 接着,在安全配置文件中声明资源服务器的行为模式: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.web.SecurityFilterChain; @Configuration public class ResourceServerConfig { @Bean public SecurityFilterChain resourceServerSecurityFilterChain(HttpSecurity http) throws Exception { http.authorizeHttpRequests(authz -> authz .anyRequest().authenticated()) .oauth2ResourceServer(oauth2 -> oauth2.jwt()); return http.build(); } } ``` 这里说明的是如何利用JWT解析器对进入系统的每一个HTTP请求执行认证操作[^3]。 --- #### 总结 以上分别介绍了基于Spring Boot框架下构建OAuth2架构所需的三个核心模块——授权服务器、客户端和资源服务器的基础搭建方法及其相互协作方式。每一步都紧密关联着实际项目开发过程中的需求场景和技术选型考量因素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

临水逸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值