【问题标题】:Spring boot + oauth2 + HttpClientErrorException 401 UnauthorizedSpring boot + oauth2 + HttpClientErrorException 401 Unauthorized
【发布时间】:2018-06-20 18:49:52
【问题描述】:

我以 Spring Boot 教程为基础 (https://spring.io/guides/tutorials/spring-boot-oauth2/) 测试 Oauth2。

但是,我的身份验证服务器不是 facebook,而是 Netiq Access Manager (NAM)。 我设法被重定向到 NAM 登录页面,但登录后,我收到以下错误:

日志显示:

o.s.b.a.s.o.r.UserInfoTokenServices      : Could not fetch user details: class org.springframework.web.client.HttpClientErrorException, 401 Unauthorized

这是项目:

应用代码:

package com.example.springoauthdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;

@SpringBootApplication
@EnableOAuth2Sso
public class SocialApplication {

    public static void main(String[] args) {
        SpringApplication.run(SocialApplication.class, args);
    }
}

application.yml

security:
  oauth2:
    client:
      clientId: 55bb61f1-4384-4939-9cd0-fa7d76af9a0c
      clientSecret: fdUjegFlCJjnD778RUSuS4SqMRey4IKVDOkadi4hjN6YbhC1xCInxoxobf-a-p-po8rt1wfZM2BPqJHpcZ-FGs
      accessTokenUri: https://nam.example.com/nidp/oauth/nam/token
      userAuthorizationUri: https://nam.example.com/nidp/oauth/nam/authz
      tokenName: oauth_token
      authenticationScheme: query
      clientAuthenticationScheme: form
    resource:
      userInfoUri: https://localhost:8443/index.html
      #userInfoUri: https://nam.example.com/nidp/oauth/nam/userinfo

server:
  port: 8443
  ssl:
    enabled: true
    key-alias: tomcat-localhost
    key-password: changeit
    key-store: classpath:keystore.jks
    key-store-provider: SUN
    key-store-type: JKS
    key-store-password: changeit

据我所知,以this Oauth2 流程为例,步骤 1、2 和 3 似乎没问题,所以问题是试图获取访问令牌?

有什么想法吗?

提前致谢!

【问题讨论】:

  • 我也面临同样的问题。问题解决了吗,
  • 很遗憾还没有。
  • 您解决了这个问题吗?我也一样

标签: spring-boot oauth netiq


【解决方案1】:

当您通过身份验证并拥有用户后,您可以根据 userInfoUri 对其进行验证,该用户信息返回 oauth 的 Principal 对象。

您正在针对 html 设置此值:

resource:
      userInfoUri: https://localhost:8443/index.html

应该是这样的:

resource:
      userInfoUri: https://localhost:8443/userinfo

并且该服务响应必须返回如下内容:

@RequestMapping("/userinfo")
    Principal getUser(Principal user) {
        return user;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-31
    • 1970-01-01
    • 2018-09-07
    • 1970-01-01
    • 2019-07-12
    • 1970-01-01
    • 2019-02-21
    • 2020-07-29
    相关资源
    最近更新 更多