我是否需要具有Spring Security OAuth2的资源服务器? [英] Do I need resource server with Spring Security OAuth2?

查看:287
本文介绍了我是否需要具有Spring Security OAuth2的资源服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JWT令牌实现OAuth2身份验证。如果我理解,我需要向授权服务器发送凭据,这将验证我的凭据,并返回签名的JWT令牌。接下来我尝试实现 WebSecurityConfig 扩展 WebSecurityConfigurerAdapter ,然后我必须设置哪些端点是安全的,哪些不是。

I am trying implement OAuth2 authentication with JWT tokens. If I understand, I need send credentials to authorization server, this verify my credentials, and return back signed JWT token. Next I tried implement WebSecurityConfig which extends WebSecurityConfigurerAdapter, and there I have to set which endpoints are secured and which aren't.

但我的问题是:我需要资源服务器吗?它与我潜在的 WebSecurityConfig 做同样的工作?

But my question is: do I need resource server? It do same job as my potential WebSecurityConfig, or not?

我的目标是为我的网站创建简单的JWT身份验证。

My goal is create simple JWT authentication for my website.

推荐答案

您需要资源服务器,因为它是 OAuth2规范

You need the resource server, because it is part of the OAuth2 spec:


资源服务器

resource server

托管受保护资源的服务器,能够接受
并使用访问令牌响应受保护资源请求。

The server hosting the protected resources, capable of accepting and responding to protected resource requests using access tokens.

因此它也是Spring Security OAuth2的一部分。

Hence it is also part of Spring Security OAuth2.

资源服务器配置不仅仅是安全配置,请参阅 OAuth 2开发人员指南

The resource server configuration is more than a security configuration, see OAuth 2 Developers Guide:


资源服务器配置

资源服务器(可以与授权服务器或单独的应用程序相同)提供受OAuth2令牌保护的资源。 Spring OAuth提供了一个实现此保护的Spring Security身份验证过滤器。您可以在 @Configuration 类上使用 @EnableResourceServer 打开它,并使用<配置它(根据需要) code> ResourceServerConfigurer 。可以配置以下功能:

A Resource Server (can be the same as the Authorization Server or a separate application) serves resources that are protected by the OAuth2 token. Spring OAuth provides a Spring Security authentication filter that implements this protection. You can switch it on with @EnableResourceServer on an @Configuration class, and configure it (as necessary) using a ResourceServerConfigurer. The following features can be configured:


  • tokenServices:定义令牌服务的bean(ResourceServerTokenServices的实例)。

  • resourceId:资源的id(可选,但建议由auth服务器验证,如果存在)。

  • resourecs服务器的其他扩展点(例如tokenExtractor)用于从传入请求中提取令牌)

  • 请求匹配受保护资源(默认为所有)

  • 访问受保护资源的规则(默认为经过身份验证的)

  • Spring Security中HttpSecurity配置器允许的受保护资源的其他自定义

  • tokenServices: the bean that defines the token services (instance of ResourceServerTokenServices).
  • resourceId: the id for the resource (optional, but recommended and will be validated by the auth server if present).
  • other extension points for the resourecs server (e.g. tokenExtractor for extracting the tokens from incoming requests)
  • request matchers for protected resources (defaults to all)
  • access rules for protected resources (defaults to plain "authenticated")
  • other customizations for the protected resources permitted by the HttpSecurity configurer in Spring Security

@EnableResourceServer 注释会自动将类型为 OAuth2AuthenticationProcessingFilter 的过滤器添加到Spring Security过滤器链中。

The @EnableResourceServer annotation adds a filter of type OAuth2AuthenticationProcessingFilter automatically to the Spring Security filter chain.

您可以使用Sprin g安全配置( WebSecurityConfigurerAdapter )用于 Spring Security中HttpSecurity配置器允许的受保护资源的其他自定义,但最好使用该资源服务器配置,因为:

You could use a Spring Security configuration (WebSecurityConfigurerAdapter) for other customizations for the protected resources permitted by the HttpSecurity configurer in Spring Security, but it is better to use the resource server configuration, because of:


  • 封装(一个类中资源服务器的所有配置)

  • 配置订购(您不必更改订单)

  • 复杂性(一个类而不是两个类)

,这是推荐的方式。

这篇关于我是否需要具有Spring Security OAuth2的资源服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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