当前位置: 首页 > 知识库问答 >
问题:

Spring Boot+Oauth2客户端凭据

云京
2023-03-14

顺便说一句,这些微服务只会通过中间件层互相交谈,我的意思是不需要用户凭据来允许授权(用户登录过程如Facebook)。

我在Internet上寻找了一些示例,展示了如何创建一个授权和资源服务器来管理这种通信。然而,我只是找到了一些例子,解释了如何使用用户凭据(三条腿)来实现它。

有没有人有在Spring Boot和Oauth2中如何做的样例?如果有可能提供更多关于所用范围的详细信息,令牌交换将不胜感激。

共有1个答案

阮梓
2023-03-14

我们使用Oauth2客户端凭据方案保护REST服务。资源和授权服务运行在同一应用程序中,但可以拆分到不同的应用程序中。

@Configuration
public class SecurityConfig {

@Configuration
@EnableResourceServer
protected static class ResourceServer extends ResourceServerConfigurerAdapter {

    // Identifies this resource server. Usefull if the AuthorisationServer authorises multiple Resource servers
    private static final String RESOURCE_ID = "*****";

    @Resource(name = "OAuth")
    @Autowired
    DataSource dataSource;

    @Override
    public void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        http    
                .authorizeRequests().anyRequest().authenticated();
        // @formatter:on
    }

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        resources.resourceId(RESOURCE_ID);
        resources.tokenStore(tokenStore());
    }

    @Bean
    public TokenStore tokenStore() {
        return new JdbcTokenStore(dataSource);
    }
}

@Configuration
@EnableAuthorizationServer
protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {

    @Resource(name = "OAuth")
    @Autowired
    DataSource dataSource;

    @Bean
    public TokenStore tokenStore() {
        return new JdbcTokenStore(dataSource);
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.tokenStore(tokenStore());
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.jdbc(dataSource);
    }
}
}

Oauth2表的数据源配置:

@Bean(name = "OAuth")
@ConfigurationProperties(prefix="datasource.oauth")
public DataSource secondaryDataSource() {
    return DataSourceBuilder.create().build();
}

与身份验证和资源服务器的通信如下所示

curl -H "Accept: application/json" user:password@localhost:8080/oauth/token -d grant_type=client_credentials
curl -H "Authorization: Bearer token" localhost:8080/...

Oauth2数据库中存在以下记录:

client_id  resource_ids  client_secret  scope  authorized_grant_types   web_server_redirect_uri  authorities  access_token_validity refresh_token_validity  additional_information  autoapprove
user  ****  password  NULL  client_credentials  NULL  X  NULL  NULL  NULL  NULL

客户端应用程序中的Resttemplate配置

@Configuration
@EnableOAuth2Client
public class OAuthConfig {

@Value("${OAuth2ClientId}")
private String oAuth2ClientId;

@Value("${OAuth2ClientSecret}")
private String oAuth2ClientSecret;

@Value("${Oauth2AccesTokenUri}")
private String accessTokenUri;

@Bean
public RestTemplate oAuthRestTemplate() {
    ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();
    resourceDetails.setId("1");
    resourceDetails.setClientId(oAuth2ClientId);
    resourceDetails.setClientSecret(oAuth2ClientSecret);
    resourceDetails.setAccessTokenUri(accessTokenUri);

    /*

    When using @EnableOAuth2Client spring creates a OAuth2ClientContext for us:

    "The OAuth2ClientContext is placed (for you) in session scope to keep the state for different users separate.
    Without that you would have to manage the equivalent data structure yourself on the server,
    mapping incoming requests to users, and associating each user with a separate instance of the OAuth2ClientContext."
    (http://projects.spring.io/spring-security-oauth/docs/oauth2.html#client-configuration)

    Internally the SessionScope works with a threadlocal to store variables, hence a new thread cannot access those.
    Therefore we can not use @Async

    Solution: create a new OAuth2ClientContext that has no scope.
    *Note: this is only safe when using client_credentials as OAuth grant type!

     */

//        OAuth2RestTemplate restTemplate = new      OAuth2RestTemplate(resourceDetails, oauth2ClientContext);
    OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resourceDetails, new DefaultOAuth2ClientContext());

    return restTemplate;
}
}

您可以注入restTemplate以(异步)与Oauth2安全服务对话。我们目前不使用scope。

 类似资料:
  • 问题内容: 我正在尝试了解和实现新的REST服务器与现有的客户端应用程序之间的客户端凭证流。我已经像这样设置了spring-security OAuth2 。从到目前为止的理解来看,我的服务器现在应该支持以下请求: 但我明白了 由引起的是这里(弹簧安全码): 看来,我需要首先 针对服务器 进行 身份验证 。但这 不是我想做的 。我希望我的两个服务器使用共享密钥相互通信。OAuth提供者服务器应应请

  • 当授权范围限于客户端控制下的受保护资源或事先与授权服务器商定的受保护资源时客户端凭据可以被用作为一种授权许可。典型的当客户端代表自己表演(客户端也是资源所有者)或者基于与授权服务器事先商定的授权请求对受保护资源的访问权限时,客户端凭据被用作为授权许可。

  • 我试图使用spring OAuth2在Spring Boot服务中实现服务到服务的安全性。我希望一个服务访问另一个服务的安全资源,而不涉及任何用户操作。 我可以设置auth服务器并使用curl请求获取令牌。我发现的测试使用Http对象来检查状态代码。 如何在具有RestTemplate和spring OAuth2的java客户机中使用客户机凭据授权类型? 我想它一定像添加一个依赖项、一个注释和一个

  • 我有一个相当基本的Spring Boot设置,我已经安装了Spring Security,我已经成功地设置了OAuth2来保护我的API。 几天前,我遇到了一些麻烦,问(并回答)了一个关于达到endpoint的问题。我很快发现问题在于,我试图在请求的正文中发送客户机凭据,但令牌endpoint在Spring Security中被配置为通过HTTP Basic Auth接受客户机凭据(和)。 我使用

  • 我开发Spring Cloud(使用Netflix OSS堆栈)微服务架构已经有一段时间了。正如您所料,我已经将授权服务器分离为一个独立的微服务。我的前端应用程序使用“密码”授权类型用于用户登录目的。但是,对于从前端服务到其他后端服务的rest调用,我使用的是“Client-Credentials”授权类型。客户机凭据授予类型也在其他后端服务中使用。通过这样做,我无法确定谁是请求的实际调用者(当前

  • 我在我的客户端应用程序(我的Spring网关)中配置客户端凭据流时遇到了一些问题。 我的授权服务器功能正常,与邮递员一起测试,没有任何问题。 但在我的客户机应用程序中,oauth2配置似乎没有编译错误。 当我调用服务器资源上的受保护资源时,我的客户端应用程序似乎试图调用其基础中的URL,而不是授权服务器。 请参阅我的配置文件中的代码: 我的客户依赖: 我的网络客户端的配置: 我在资源上的呼叫者: