Я пытаюсь интегрировать Spring Security 5 OAuth2 / OIDC с GitLab в свой проект и, следуя официальным примерам кодов oauth2login, добавьте мою конфигурацию 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, установив для него URL-адрес обратного вызова авторизации как: http: // локальный: 8080 / логин / oauth2 / код / gitlab.
Когда я запустил приложение через mvn spring-boot:run. И нажмите Gitlab, нажмите кнопку Авторизовать на странице авторизации GitLab.
Затем страница возвращается к http: // localhost: 8080 / логин, и я получил исключение в консоли, подобное следующему:
at position 15931.
at com.nimbusds.jose.util.JSONObjectUtils.parse(JSONObjectUtils.java:75) ~[nimbus-jose-jwt-5.4.jar:5.4]
at com.nimbusds.jose.jwk.JWKSet.parse(JWKSet.java:304) ~[nimbus-jose-jwt-5.4.jar:5.4]
at com.nimbusds.jose.jwk.source.RemoteJWKSet.updateJWKSetFromURL(RemoteJWKSet.java:145) ~[nimbus-jose-jwt-5.4.jar:5.4]
... 64 common frames omitted
Похоже, он не перенаправлял на желаемую страницу, а вместо этого выполнял вход в Gitlab.
Это старая проблема, но я считаю, что здесь есть ошибка:
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
Я боролся с той же проблемой в течение последних двух дней, наконец, нашел рабочее решение:
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:
Конечно, не забудьте изменить localhost:8080 на свой реальный адрес приложения. Этот, однако, отлично подходит для локального развития.
Когда в тот момент я использовал Gitlab, я думаю, что была проблема в том, что Gitlab по умолчанию защищал некоторые URL-адреса конфигурации обнаружения.
также необходимо настроить spring.security.oauth2.client.provider.gitlab.user-name-attr ibute = username