【问题标题】:OAuth2 retrieve client_id in CustomUserDetailsServiceOAuth2 在 CustomUserDetailsS​​ervice 中检索 client_id
【发布时间】:2018-01-05 13:54:07
【问题描述】:

我创建了一个配置为我的授权服务器 oauth2 的 Spring Boot 应用程序,还有其他配置为客户端 oauth2 的 Spring Boot 应用程序。

在我的服务器中,我实现了 CustomUserDetailsS​​ervice,我希望在 loadUserByUsername 方法中检索正在验证的项目的 client_id。

我不知道这是否可能,但我想恢复它以验证 client_id,因为我有几个使用相同授权方的应用程序,并且通过 client_id 我将检查用户是否有权访问该应用程序。

有人知道怎么做吗?

application.yml 客户端

security:
  basic:
    enabled: false
  oauth2:
    sso:
      loginPath: /login
    client:
      clientId: web_app
      clientSecret: secret
      accessTokenUri: https://localhost/uaa/oauth/token
      userAuthorizationUri: https://localhost/uaa/oauth/authorize

CustomUserDetailsS​​ervice

public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        try {

            //I would like to recover the client_id in this location

            User domainUser = userRepository.findByUsername(username);

必须验证任何服务器,因为我对所有应用程序都有一个登录:

Auth 服务器身份验证由用户和密码以及应用程序完成。

假设您正在登录“https://accounts.google.com/”,谷歌会检查用户名和密码,并显示您的帐户以及“产品”的快捷方式。

现在,如果您访问“https://mail.google.com”,谷歌会检查用户名和密码并显示 gmail 界面。就我而言,它可以来自 URL https://application.domain.comhttps://application2.domain.comhttps://application3.domain.com 或“https://accounts.domain.com”。

它使用用户名和密码的所有 URL,但它可以访问 application1 或全部或无。

我想做服务器端,因为它只为具有权限(信用)的应用程序返回访问令牌。如果用户和密码正确,并不意味着用户可以访问所有应用程序。

【问题讨论】:

    标签: spring-boot oauth-2.0 spring-security-oauth2


    【解决方案1】:

    为了访问 client_id,您需要实现不同的接口。 CustomUserDetailsService 只会让您访问用户对象。

    我不知道在Spring Boot 中执行此操作的确切方法,但是在基于 XML 的配置中,您必须执行以下操作

    public class CustomJdbcClientDetailsService implements 
           ClientDetailsService, ClientRegistrationService {
    
           @Override
           public CustomClientDetails loadClientByClientId(String clientId)
                  throws InvalidClientException {
               //Do something with clientId 
           }
    }
    

    简而言之,loadClientByClientId 方法让您可以访问客户端 ID。还有其他方法可以拦截不同的 Spring OAuth2 类并访问client_id,但这是最简洁和推荐的方法。

    您还可以通过添加Filter 来访问client_id 以拦截请求并检查正文。

    您需要研究如何在Spring Boot 中实现ClientDetailsServiceClientRegistrationService。一定有很多关于它的教程。

    最好先用谷歌搜索如何设置ClientDetailsServiceConfigurerSpring Boot

    1. 有关ClientDetailsServiceConfigurer 的更多信息,请参阅Spring OAuth Java Doc
    2. A good tutorialthis one

    【讨论】:

      【解决方案2】:
      final SavedRequest savedRequest = new HttpSessionRequestCache().getRequest(request, response); if (null != savedRequest) { 最终字符串 redirectUrl = savedRequest.getRedirectUrl(); 最终 MultiValueMap 参数 = UriComponentsBuilder.fromUriString(redirectUrl).build().getQueryParams(); if (parameters.containsKey("client_id")) { final String clientId = parameters.get("client_id").get(0); } }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-10
        • 1970-01-01
        • 2013-12-19
        • 1970-01-01
        • 1970-01-01
        • 2012-04-22
        • 2017-01-31
        • 2013-10-06
        相关资源
        最近更新 更多