Java Spring boot oauth2:No userInfo端点-如何直接在客户端从JWT访问令牌加载身份验证(主体)
我正在设置一个oauth 2.0客户端应用程序,它将用户重定向到外部IDP(授权服务器)进行登录。我的应用程序将执行常规的oauth 2授权码授权流-1)重定向用户以登录。2) 先获取访问码3)使用访问码获取令牌。由于外部IDP仅使用oauth 2进行身份验证,因此他们不会提供用户信息端点url(OIDC提供程序需要)来获取用户详细信息。相反,他们希望我们直接从JWT令牌获得索赔,并在我们的应用程序中进行任何授权。 我无法找到正确的代码/配置,它不需要用户信息端点,而是直接解码jwt以加载身份验证。 在下面的演示代码中,如果我要解码OKTA发布的JWT令牌中的用户详细信息,而不调用其userInfo端点,我该怎么做 我使用的是SpringBoot2.x版本,使用的是SpringReference示例SocialOAuth2项目中提供的标准oauth客户端配置 如果有人能指引我走上正确的道路,我将不胜感激。谢谢大家! 梯度配置Java Spring boot oauth2:No userInfo端点-如何直接在客户端从JWT访问令牌加载身份验证(主体),java,spring-boot,oauth-2.0,Java,Spring Boot,Oauth 2.0,我正在设置一个oauth 2.0客户端应用程序,它将用户重定向到外部IDP(授权服务器)进行登录。我的应用程序将执行常规的oauth 2授权码授权流-1)重定向用户以登录。2) 先获取访问码3)使用访问码获取令牌。由于外部IDP仅使用oauth 2进行身份验证,因此他们不会提供用户信息端点url(OIDC提供程序需要)来获取用户详细信息。相反,他们希望我们直接从JWT令牌获得索赔,并在我们的应用程序中进行任何授权。 我无法找到正确的代码/配置,它不需要用户信息端点,而是直接解码jwt以加载身份验
buildscript {
ext {
springBootVersion = '2.2.0.RELEASE'
}
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenLocal()
mavenCentral()
}
configurations {
compile.exclude module: 'spring-boot-starter-logging'
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web:${springBootVersion}")
compile("org.springframework.boot:spring-boot-starter-log4j2:${springBootVersion}")
compile("org.springframework.boot:spring-boot-starter-security:${springBootVersion}")
compile("org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:${springBootVersion}")
compile("org.webjars:jquery:2.1.1")
compile("org.webjars:bootstrap:3.2.0")
compile("org.webjars:webjars-locator-core:0.42")
}
application.yml
github:
client:
clientId: <clientId>
clientSecret: <clientSecret>
accessTokenUri: https://github.com/login/oauth/access_token
userAuthorizationUri: https://github.com/login/oauth/authorize
tokenName: oauth_token
authenticationScheme: query
clientAuthenticationScheme: form
resource:
userInfoUri: https://api.github.com/user
okta:
client:
clientId: <clientId>
clientSecret: <clientSecret>
accessTokenUri: https://<okta-sub-domain>/oauth2/default/v1/token
userAuthorizationUri: https://<okta-sub-domain>/oauth2/default/v1/authorize
scope: openid profile email
resource:
userInfoUri: https://<okta-sub-domain>/oauth2/default/v1/userinfo
}
}
DefaultReactiveOAuth2UserService
查找用户信息。我们可以简单地引入一个新的ReactiveOAuth2UserService
实现,从令牌中获取值,例如:
@Service
public class GttOAuth2UserService implements ReactiveOAuth2UserService<OAuth2UserRequest, OAuth2User> {
@Override
public Mono<OAuth2User> loadUser(OAuth2UserRequest oAuth2UserRequest) throws OAuth2AuthenticationException {
final List<GrantedAuthority> authorities = Arrays.asList(new SimpleGrantedAuthority("authority"));
final Map<String, Object> attributes = oAuth2UserRequest.getAdditionalParameters();
final OAuth2User user = new DefaultOAuth2User(authorities, attributes, "email");
return Mono.just(user);
}
}
@服务
公共类GttOAuth2UserService实现了ReactiveOAuth2UserService{
@凌驾
公共Mono loadUser(OAuth2UserRequest OAuth2UserRequest)引发OAuth2AuthenticationException{
最终列表权限=Arrays.asList(新的SimpleGrantedAuthority(“权限”);
最终映射属性=oAuth2UserRequest.getAdditionalParameters();
最终OAuth2User=新的默认OAuth2User(权限、属性、“电子邮件”);
返回Mono.just(用户);
}
}
(在您的情况下,可能是非反应性等效物)
@RestController
public class UserController {
@GetMapping("/user")
public Principal user(Principal principal) {
return principal;
}
@SpringBootApplication
public class Oauth2Application {
public static void main(String[] args) {
SpringApplication.run(Oauth2Application.class, args);
}
@Service
public class GttOAuth2UserService implements ReactiveOAuth2UserService<OAuth2UserRequest, OAuth2User> {
@Override
public Mono<OAuth2User> loadUser(OAuth2UserRequest oAuth2UserRequest) throws OAuth2AuthenticationException {
final List<GrantedAuthority> authorities = Arrays.asList(new SimpleGrantedAuthority("authority"));
final Map<String, Object> attributes = oAuth2UserRequest.getAdditionalParameters();
final OAuth2User user = new DefaultOAuth2User(authorities, attributes, "email");
return Mono.just(user);
}
}