首页 > 编程知识 正文

springcloud认证服务器,oauth2服务端实现

时间:2023-05-05 22:22:16 阅读:117196 作者:3621

上一篇文章介绍了如何使用Cloud Foundry UAA项目启动OAuth2许可证服务器,以及如何使用与OAuth2许可证代码过程相关的参与者进行输入。

在数字操作网站上,我发现这篇文章在解释OAuth2许可证代码的流程方面做得很好。 因此,与其重新散列与此流程相关的内容,不如直接使用Spring Boot/Spring Security实现流。

下图显示了许可密钥授予类型的高级流程。

有两个APP应用程序。 一个资源服务器发布用户的部分资源,另一个客户端APP应用程序代表用户访问。 正如上一篇博客文章中所述,可以启动许可证服务器本身。

文章的其余部分可以很容易地遵循我的github存储库中的代码

许可证服务器可以使用以前的博客文章中介绍的步骤轻松启动云基础uaa服务器。 完成后,可以使用以下uaac命令输入运行示例所需的各种凭据:

这些脚本为客户端APP应用程序创建客户端证书,并在resource.read和resource.write范围内添加名为" user1"的用户。

# loginasacannedclientuaactokenclientgetadmin-sadminsecret # addaclientcredentialwithclient _ idofclient1and client _ secrecret --name client1-scopererient resource.write- sclient1-- authorized _ grant _ types authorization _ code,refion client _ credenttion-authorities uaa.resource # anotherclientcredentialresource1/资源1 uaacclientaddresource1_-- name资源1 _-s-authorized _ grant _ types client _ credentials - authorities uaa.resource # addausercalleduser1/user1uaacuseradduser1- puser1--emails user1@ user1.com # addtwoscoom resource.writeuaacgroupaddresource.readuaacgroupaddresource.write # assign user1both resource.read, 通过以下方式,resource.write scopes . uaacmemberaddresource.read user1uaacmemberaddresource.write user 1资源服务器资源服务器

@ restcontrollerpublicclassgreetingscontroller { @ pre authorize (' # oauth2. has scope (' resource.read ' ) } @ request mopope @ responsebodypublicstringread (身份验证(return string.format ) ) reatication }@preauthorize('# oauth2.hasscope ) ' resource.write ' ) )请求映射) method=请求方法. get, value='/secured/write ' ) @ responsebodypublicstringwrite (认证字符串.格式) ) worration 许可证用于范围“resource.read”的“/安全/读取”和范围“resource.write”的“/安全/写入”

以下配置保护这些端点并将APP应用程序标记为资源服务器:

@ configuration @ enableresourceserver @ ENA

bleWebSecurity@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter { @Override public void configure(ResourceServerSecurityConfigurer resources) throws Exception { resources.resourceId("resource"); } @Override public void configure(HttpSecurity http) throws Exception { http .antMatcher("/secured/**") .authorizeRequests() .anyRequest().authenticated(); }}

该配置以及描述如何验证令牌的属性是使资源服务器运行所需的全部。

客户

使用Spring Security OAuth2的OAuth2的客户端配置也相当简单,@ EnableAuth2SSO批注提取所有必需的配置以为OAuth2流连接Spring安全过滤器:

@EnableOAuth2Sso@Configurationpublic class OAuth2SecurityConfig extends WebSecurityConfigurerAdapter { @Override public void configure(WebSecurity web) throws Exception { super.configure(web); } @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable(); //@formatter:off http.authorizeRequests() .antMatchers("/secured/**") .authenticated() .antMatchers("/") .permitAll() .anyRequest() .authenticated(); //@formatter:on }}

要调用下游系统,客户端必须将OAuth令牌作为标头传递到下游调用中,方法是通过挂接一个称为OAuth2RestTemplate的专用RestTemplate来完成,该模板可以从上下文中获取访问令牌并将其传递到下游连接了一个安全的下游呼叫,如下所示:

public class DownstreamServiceHandler { private final OAuth2RestTemplate oAuth2RestTemplate; private final String resourceUrl; public DownstreamServiceHandler(OAuth2RestTemplate oAuth2RestTemplate, String resourceUrl) { this.oAuth2RestTemplate = oAuth2RestTemplate; this.resourceUrl = resourceUrl; } public String callRead() { return callDownstream(String.format("%s/secured/read", resourceUrl)); } public String callWrite() { return callDownstream(String.format("%s/secured/write", resourceUrl)); } public String callInvalidScope() { return callDownstream(String.format("%s/secured/invalid", resourceUrl)); } private String callDownstream(String uri) { try { ResponseEntity<String> responseEntity = this.oAuth2RestTemplate.getForEntity(uri, String.class); return responseEntity.getBody(); } catch(HttpStatusCodeException statusCodeException) { return statusCodeException.getResponseBodyAsString(); } }} 示范

可以使用此处的说明启动客户端和资源服务器。 一旦所有系统启动,访问客户端将向用户显示一个页面,如下所示:


访问安全页面将导致授权服务器显示登录页面:

客户端正在向用户请求“ resource.read”和“ resource.write”范围,提示用户授权这些范围:

假设用户已授权“ resource.read”但未授权“ resource.write”,则令牌将呈现给用户:


在这一点上,如果请求的下游资源要求范围为“ resource.read”,则应检索它:

并且,如果请求的下游资源具有用户未授权的范围,在这种情况下为“ resource.write”:

参考 大多数代码基于此处提供的Cloud Foundry UAA应用程序示例– https://github.com/pivotal-cf/identity-sample-apps 帖子中的代码在这里 :https://github.com/bijukunjummen/oauth-uaa-sample

翻译自: https://www.javacodegeeks.com/2017/03/using-uaa-oauth2-authorization-server-client-resource.html

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。