如何使用OAuth2RestTemplate? [英] How to use OAuth2RestTemplate?

查看:10934
本文介绍了如何使用OAuth2RestTemplate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解如何使用OAuth2RestTemplate对象来消耗我的OAuth2安全REST服务(这是一个不同的项目下运行,让我们还假设在不同的服务器等等上)

f.e。我的休息服务是:

 的http://本地主机:8082 /应用/的HelloWorld

- >访问这个URL,因为我未通过身份验证生成错误

要请求令牌我会去:

<$p$p><$c$c>http://localhost:8082/app/oauth/token?grant_type=password&client_id=restapp&client_secret=restapp&username=**USERNAME**&password=**PASSWORD**

我收到令牌后,我就可以使用下列URL连接到REST API(例如令牌插入)

<$p$p><$c$c>http://localhost:8082/app/helloworld/?access_token=**4855f557-c6ee-43b7-8617-c24591965206**

现在我的问题是如何实现它可能消耗的OAuth2这个安全REST API第二个应用程序?我真的没有发现任何工作例子,您提供的用户名和密码(例如,从登录表单来的),然后产生一个令牌,该令牌可重新用于从REST API获取数据。

目前,我试着用以下对象的内容:

  BaseOAuth2ProtectedResourceDetails baseOAuth2ProtectedResourceDetails =新BaseOAuth2ProtectedResourceDetails();
baseOAuth2ProtectedResourceDetails.setClientId(restapp);
baseOAuth2ProtectedResourceDetails.setClientSecret(restapp);
baseOAuth2ProtectedResourceDetails.setGrantType(密码);
//如何设置的用户名和密码???DefaultAccessTokenRequest accessTokenRequest =新DefaultAccessTokenRequest();
OAuth2ClientContext oAuth2ClientContext =新DefaultOAuth2ClientContext(accessTokenRequest());OAuth2RestTemplate restTemplate =新OAuth2RestTemplate(baseOAuth2ProtectedResourceDetails,oAuth2ClientContext);

但是,这只是不工作:(

任何想法是极大的AP preciated或链接到工作示例和教程大大AP preciated。


解决方案

您可以找到exampels这里<一个编写OAuth客户href=\"https://github.com/spring-projects/spring-security-oauth\">https://github.com/spring-projects/spring-security-oauth

在你的情况,你不能只使用默认设置或基类的一切,你有多个类实现OAuth2ProtectedResourceDetails。配置取决于你如何配置您的OAuth服务,但是从你的卷曲的连接假设我会建议:

  @ EnableOAuth2Client
@组态
一流的myconfig {
    @Value($ {oauth.resource:HTTP://本地主机:8082})
    私人字符串的baseUrl;
    @Value($ {oauth.authorize:HTTP://本地主机:8082 /的OAuth /授权})
    私人字符串authorizeUrl;
    @Value($ {oauth.token:HTTP://本地主机:8082 /的OAuth /令牌})
    私人字符串tokenUrl
    @豆
    保护OAuth2ProtectedResourceDetails资源(){        ResourceOwnerPasswordResourceDetails资源=新ResourceOwnerPasswordResourceDetails();        清单范围=新的ArrayList&LT;串GT;(2);
        scopes.add(写);
        scopes.add(读);
        resource.setAccessTokenUri(tokenUrl);
        resource.setClientId(restapp);
        resource.setClientSecret(restapp);
        resource.setGrantType(密码);
        resource.setScope(范围);        resource.setUsername(** USERNAME **);
        resource.setPassword(**密码**);        返回的资源;
    }    @豆
    公共OAuth2RestOperations restTemplate(){
        AccessTokenRequest ATR =新DefaultAccessTokenRequest();        返回新OAuth2RestTemplate(资源(),新DefaultOAuth2ClientContext(ATR));
    }}@服务
@燮pressWarnings(未登记)
类为MyService {
    @Autowired
    私人OAuth2RestOperations restTemplate;    公共AcesToken(){        restTemplate.getAccessToken();
    }
}

不要忘了@ EnableOAuth2Client你的配置类,我也建议尝试您所使用的网址是卷曲第一份工作,也尽量与调试跟踪它,因为很多例外的都只是消耗和从来没有印刷由于进行安全方面的原因,所以它得到一点很难找到那里的问题。与调试启用设置你应该使用记录。
祝你好运

我上传样本springboot应用在github <一个href=\"https://github.com/mariubog/oauth-client-sample\">https://github.com/mariubog/oauth-client-sample
来描述你的情况,因为您的方案我找不到任何样品。

I'm trying to understand how to use a OAuth2RestTemplate object to consume my OAuth2 secured REST service (which is running under a different project and let's assume also on a different server etc...)

f.e. my rest service is:

http://localhost:8082/app/helloworld

-> Accessing this URL generates an error as I am not authenticated

To request a token I would go to:

http://localhost:8082/app/oauth/token?grant_type=password&client_id=restapp&client_secret=restapp&username=**USERNAME**&password=**PASSWORD**

After I receive the token I can then connect to the REST API by using the following URL (example token inserted)

http://localhost:8082/app/helloworld/?access_token=**4855f557-c6ee-43b7-8617-c24591965206**

Now my question is how do I implement a second application which can consume this OAuth2 secured REST API? I really haven't found any working examples where you provide the user name and password (e.g. coming from a login form) and then a token is generated which can be re-used to get data from the REST API.

I currently tried something with the following objects:

BaseOAuth2ProtectedResourceDetails baseOAuth2ProtectedResourceDetails =  new BaseOAuth2ProtectedResourceDetails();
baseOAuth2ProtectedResourceDetails.setClientId("restapp");
baseOAuth2ProtectedResourceDetails.setClientSecret("restapp");
baseOAuth2ProtectedResourceDetails.setGrantType("password");
// how to set user name and password ???

DefaultAccessTokenRequest accessTokenRequest = new DefaultAccessTokenRequest();
OAuth2ClientContext oAuth2ClientContext = new DefaultOAuth2ClientContext(accessTokenRequest());

OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(baseOAuth2ProtectedResourceDetails,oAuth2ClientContext);

But this just isn't working :(

Any ideas is greatly appreciated or links to working examples and tutorials is greatly appreciated.

解决方案

You can find exampels for writing Oauth clients here https://github.com/spring-projects/spring-security-oauth

In your case you can't just use default or base classes for everything, you have a multiple classes Implementing OAuth2ProtectedResourceDetails. The configuration depends of how you configured your Oauth service but assuming from your curl connections I would recomend:

@EnableOAuth2Client
@Configuration
class MyConfig{




    @Value("${oauth.resource:http://localhost:8082}")
    private String baseUrl;
    @Value("${oauth.authorize:http://localhost:8082/oauth/authorize}")
    private String authorizeUrl;
    @Value("${oauth.token:http://localhost:8082/oauth/token}")
    private String tokenUrl


    @Bean
    protected OAuth2ProtectedResourceDetails resource() {

        ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();

        List scopes = new ArrayList<String>(2);
        scopes.add("write");
        scopes.add("read");
        resource.setAccessTokenUri(tokenUrl);
        resource.setClientId("restapp");
        resource.setClientSecret("restapp");
        resource.setGrantType("password");
        resource.setScope(scopes);

        resource.setUsername("**USERNAME**");
        resource.setPassword("**PASSWORD**");

        return resource;
    }

    @Bean
    public OAuth2RestOperations restTemplate() {
        AccessTokenRequest atr = new DefaultAccessTokenRequest();

        return new OAuth2RestTemplate(resource(), new DefaultOAuth2ClientContext(atr));
    }

}

@Service
@SuppressWarnings("unchecked")
class MyService {
    @Autowired
    private OAuth2RestOperations restTemplate;

    public AcesToken() {

        restTemplate.getAccessToken();


    }
}

Do not forget about @EnableOAuth2Client on your config class, also I would suggest to try that the urls you are using are working with curl first, also try to trace it with the debugger because lot of exceptions are just consumed and never printed out due security reasons, so it gets little hard to find where the issue is. You should use logger with debug enabled set. Good luck

I uploaded sample springboot app on github https://github.com/mariubog/oauth-client-sample to depict your situation because I could not find any samples for your scenario .

这篇关于如何使用OAuth2RestTemplate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆