【问题标题】:Minimum spring boot configuration for OAuth2 server and clientOAuth2 服务器和客户端的最小 Spring Boot 配置
【发布时间】:2017-04-01 00:55:04
【问题描述】:

我尝试编写一个“Hello world”类型的练习来学习 Spring Boot 对 OAuth2 的支持以及所需的最低配置。

组件:

  • 授权服务器
  • webapp,它将调用身份验证服务器对用户进行身份验证,并向他打招呼

预期流量:

  • 我打开 webapp
  • 我被重定向到身份验证服务器
  • 我登录到身份验证服务器
  • 我批准请求的范围
  • 我被重定向回 webapp
  • 我看到一个问候语(webapp 应该从身份验证服务器获取我的用户名)

最后一点失败,出现 401:Could not get access token。 最后一个重定向链接是http://localhost:9001/ui/login?code=wcXMG4&state=JEEYqC

我是否太天真地认为以下代码和配置应该足以满足我的预期流程?

认证服务器:

@SpringBootApplication
@EnableAuthorizationServer
@EnableResourceServer
@RestController
public class AuthServer {

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

@GetMapping("/whois")
Principal whois(Principal principal) {
    return principal;
}
}

验证服务器属性:

server.port=9000
server.contextPath=/sso

security.user.name=joe
security.user.password=password

security.oauth2.client.clientId=SOMEAPP
security.oauth2.client.clientSecret=SECRET
security.oauth2.client.authorized-grant-types=authorization_code,refresh_token,password
security.oauth2.client.scope=read
security.oauth2.resource.userInfoUri=http://localhost:9000/sso/whois

网页应用:

@SpringBootApplication
@EnableOAuth2Sso
@RestController
public class UiServer {

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

@GetMapping("/")
String helloWorld(Principal principal) {
    return "Yay, auth server provided identity, you are " + principal;
}
}

Webapp 属性:

server.port=9001
server.contextPath=/ui

security.oauth2.client.client-id=SOMEAPP
security.oauth2.client.client-secret=SECRET
security.oauth2.client.accessTokenUri=http://localhost:9000/sso/oauth/access_token
security.oauth2.client.userAuthorizationUri=http://localhost:9000/sso/oauth/authorize
security.oauth2.resource.user-info-uri=http://localhost:9000/sso/whois

【问题讨论】:

    标签: spring-oauth2


    【解决方案1】:

    启动调试后,发现 security.oauth2.client.accessTokenUri 不正确。

    正确的端点不是.../oauth/access_token,而是.../oauth/token。 可能我正在查看的教程使用过时的 uri。

    有了这个修复,这个最低配置就可以满足它的预期,所以我将结束这个问题。

    真正的乐趣始于您尝试自定义它,绕过默认设置;在我看来,spring oauth 似乎仍然存在重大错误,并且需要在少数用例中使用 hacky/意外的方法来解决它们。

    【讨论】:

      猜你喜欢
      • 2018-06-23
      • 1970-01-01
      • 2018-08-22
      • 2016-06-27
      • 1970-01-01
      • 2016-09-28
      • 2016-04-15
      • 2021-05-07
      • 2019-02-26
      相关资源
      最近更新 更多