Spring boot + oauth2 + HttpClientErrorException 401未经授权

时间:2018-01-11 12:27:59

标签: spring-boot oauth netiq

我使用spring boot教程作为基础(https://spring.io/guides/tutorials/spring-boot-oauth2/) 测试Oauth2。

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

Whitelabel Error

日志显示:

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

这是项目:

Project structure

应用代码:

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似乎没问题,所以问题是尝试获取访问令牌?

有什么想法吗?

提前致谢!

1 个答案:

答案 0 :(得分:0)

通过身份验证并拥有用户后,可以根据userInfoUri对其进行验证,该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;
    }