使用Gitlab的Spring Security 5 OAuth2客户端失败

时间:2018-03-17 02:41:46

标签: spring spring-boot spring-security oauth-2.0 gitlab

我正在尝试在我的项目中将Spring Security 5 OAuth2 / OIDC与GitLab集成,并按照官方示例代码oauth2login添加我的Gitlab配置,如下所示。

setTimeout(fn, 0)

我在Gitlab设置帐户中创建了一个应用程序,将其设置为授权回调网址: http://localhost:8080/login/oauth2/code/gitlab

当我通过security: oauth2: client: registration: gitlab: client-id: 0cef9527091bb2faec01610a0fb330e3a915672110cf3298ff3aadceaa8ab11f client-secret: fd84439d06f7a2dabb5d5a64ac478211ab4009aa0fa62d478661a52f4234de72 authorization-grant-type: authorization_code redirectUriTemplate: '{baseUrl}/login/oauth2/code/{registrationId}' scope: - openid - api clientName: GitLab provider: gitlab: authorization-uri: https://gitlab.com/oauth/authorize token-uri: https://gitlab.com/oauth/token user-info-uri: https://gitlab.com/oauth/userinfo jwk-set-uri: https://gitlab.com/oauth/discovery/keys 运行应用程序时。然后点击 Gitlab ,点击GitLab授权页面中的授权按钮。

然后页面返回 http://localhost:8080/login ,我在控制台中遇到异常,如下所示:

mvn spring-boot:run

它似乎没有重定向到所需的页面,而是改为Gitlab登录。

2 个答案:

答案 0 :(得分:1)

这是一个老问题,但我认为这里有一个错误:

user-info-uri: https://gitlab.com/oauth/userinfo

应该是:

user-info-uri: https://gitlab.com/api/v4/user

在这里:https://docs.gitlab.com/ce/api/oauth2.html

答案 1 :(得分:1)

最近两天我一直在努力解决同一问题,终于找到了可行的解决方案:

pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

application.properties

spring.security.oauth2.client.provider.gitlab.authorization-uri=https://gitlab.com/oauth/authorize
spring.security.oauth2.client.provider.gitlab.token-uri=https://gitlab.com/oauth/token
spring.security.oauth2.client.provider.gitlab.user-info-uri=https://gitlab.com/api/v4/user
spring.security.oauth2.client.provider.gitlab.user-name-attribute=username
spring.security.oauth2.client.provider.gitlab.jwk-set-uri=https://gitlab.com/oauth/discovery/keys

spring.security.oauth2.client.registration.gitlab.client-id=YOUR_CLIENT_ID
spring.security.oauth2.client.registration.gitlab.client-secret=YOUR_SECRET
spring.security.oauth2.client.registration.gitlab.authorization-grant-type=authorization_code
spring.security.oauth2.client.registration.gitlab.redirect-uri=http://localhost:8080/login/oauth2/code/gitlab
spring.security.oauth2.client.registration.gitlab.scope=read_user
spring.security.oauth2.client.registration.gitlab.client-name=GitLab

Gitlab应用程序配置:

gitlab config

当然,请记住将localhost:8080更改为您的真实应用地址。但是,这对于本地开发来说效果很好。