【问题标题】:Spring OAuth access to UserDetailsSpring OAuth 访问 UserDetails
【发布时间】:2015-10-16 07:21:10
【问题描述】:

有没有办法访问资源服务器中的 OAuth UserDetails?

在 OAuth 服务器上,我正在返回自定义用户详细信息

http://localhost:9999/uaa/user

@RestController
public class UserInfoResource {

    @RequestMapping("/user")
    public ResponseEntity<UserInfo> account(@AuthenticationPrincipal UserInfo userInfo) {

        return Optional.ofNullable(userInfo)
                .map((usr) -> new ResponseEntity<UserInfo>(usr, HttpStatus.OK))
                .orElse(new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR));
    }
}

有没有办法访问资源服务器中的 UserInfo 对象? Spring Security 定义了方便的@AuthenticationPrincipal 注解,可用于将身份验证对象注入 Spring MVC 控制器/rest 端点,OAuth 身份验证也可以这样做,还是有任何其他方式来填充处理请求的 UserInfo 实例?

【问题讨论】:

    标签: spring spring-mvc oauth spring-security spring-security-oauth2


    【解决方案1】:

    我不确定,但由于Oauth2 是描述不同服务器/服务之间身份验证机制的标准,因此无法提供自定义属性。

    假设您的资源服务器支持 oauth2 授权,并且用户使用 FB 的身份验证服务器。正如我们所说,这是一个标准,但 FB 对您的自定义字段一无所知...

    【讨论】:

      【解决方案2】:

      试试这个:

        @RestController
              public class UserInfoResource {
      
                  @RequestMapping("/user")
                  public ResponseEntity<UserInfo> account(Authentication authentication) {
                      UserDetails userDetails = (UserDetails) authentication.getPrincipal();
                      return Optional.ofNullable(userInfo)
                              .map((usr) -> new ResponseEntity<UserInfo>(usr, HttpStatus.OK))
                              .orElse(new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR));
                  }
              }
      

      【讨论】:

      • 也许我还不够清楚,我想做的是访问从授权服务器检索到的填充 UserDetails 对象实例,以便我能够访问它的自定义属性 instacne do就像是。 {code} @RequestMapping("/user") public ResponseEntity account() { // 以某种方式获取 UserInfo userInfo.getId(); // 访问自定义属性 } {code}
      猜你喜欢
      • 2018-11-09
      • 2014-11-30
      • 1970-01-01
      • 2015-11-13
      • 2018-06-04
      • 2015-01-17
      • 2016-12-22
      • 2018-12-16
      • 2014-11-17
      相关资源
      最近更新 更多