我需要具有 Spring Security OAuth2 的资源服务器吗?
Do I need resource server with Spring Security OAuth2?
我正在尝试使用 JWT 令牌实施 OAuth2 身份验证。如果我理解,我需要将凭据发送到授权服务器,这将验证我的凭据,并 return 回签 JWT 令牌。接下来我尝试实现扩展 WebSecurityConfigurerAdapter
的 WebSecurityConfig
,我必须在那里设置哪些端点是安全的,哪些不是。
但我的问题是:我需要资源服务器吗?它和我的潜在 WebSecurityConfig
做同样的工作吗?
我的目标是为我的网站创建简单的 JWT 身份验证。
是的,您需要通过扩展 ResourceServerConfigurerAdapter
配置受 JWT 保护的资源。基本实现可能如下所示
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
}
这意味着您不需要扩展 WebSecurityConfigurerAdapter
,因为上面的配置配置了与您将在 WebSecurityConfigurerAdapter
中配置的相同的 HttpSecurity
对象。 public void configure(HttpSecurity http)
在 类.
中的作用相同
我们选择 ResourceServerConfigurerAdapter
而不是 WebSecurityConfigurerAdapter
的原因是因为它是您正在使用的 spring-security-oauth2 模块的一部分,将在幕后使用由框架。
您当然需要确保您对授权服务器和资源服务器使用相同的签名密钥。如果您在同一个应用程序中定义安全配置 bean,资源服务器将自动使用相同的 bean,否则您将需要复制授权服务器上的任何 JWT 相关配置。
我会试着用一个例子来回答:假设你想写一个很棒很酷的网络应用程序,可以以某种方式一起管理 GMAIL 帐户和 Google-日历数据。显然,您的用户必须使用他们 google 的凭据登录,这样您的应用才能获取他们的数据并进行管理。您的应用程序管理用户的数据,而无需获取用户的凭据。
到目前为止一切顺利。
在此示例中,Authorization-Server 是 Google 帐户。 Resource Server 是 Google-Main 和 Google-Calendar(两者),Client 是您的应用程序.
希望这是有道理的。
您需要资源服务器,因为它是 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 的一部分。
资源服务器配置不仅仅是安全配置,参见OAuth 2 Developers Guide:
Resource Server Configuration
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: 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
The @EnableResourceServer
annotation adds a filter of type OAuth2AuthenticationProcessingFilter
automatically to the Spring Security filter chain.
您可以使用 Spring 安全配置 (WebSecurityConfigurerAdapter
) 对 受保护资源的其他自定义 Spring 安全 [=35] 中的 HttpSecurity 配置器允许=],但最好使用资源服务器配置,因为:
- 封装(资源服务器所有配置合一class)
- 配置顺序(您不必更改顺序)
- 复杂性(一个 class 而不是两个 classes)
也是推荐的方式
我正在尝试使用 JWT 令牌实施 OAuth2 身份验证。如果我理解,我需要将凭据发送到授权服务器,这将验证我的凭据,并 return 回签 JWT 令牌。接下来我尝试实现扩展 WebSecurityConfigurerAdapter
的 WebSecurityConfig
,我必须在那里设置哪些端点是安全的,哪些不是。
但我的问题是:我需要资源服务器吗?它和我的潜在 WebSecurityConfig
做同样的工作吗?
我的目标是为我的网站创建简单的 JWT 身份验证。
是的,您需要通过扩展 ResourceServerConfigurerAdapter
配置受 JWT 保护的资源。基本实现可能如下所示
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
}
这意味着您不需要扩展 WebSecurityConfigurerAdapter
,因为上面的配置配置了与您将在 WebSecurityConfigurerAdapter
中配置的相同的 HttpSecurity
对象。 public void configure(HttpSecurity http)
在 类.
我们选择 ResourceServerConfigurerAdapter
而不是 WebSecurityConfigurerAdapter
的原因是因为它是您正在使用的 spring-security-oauth2 模块的一部分,将在幕后使用由框架。
您当然需要确保您对授权服务器和资源服务器使用相同的签名密钥。如果您在同一个应用程序中定义安全配置 bean,资源服务器将自动使用相同的 bean,否则您将需要复制授权服务器上的任何 JWT 相关配置。
我会试着用一个例子来回答:假设你想写一个很棒很酷的网络应用程序,可以以某种方式一起管理 GMAIL 帐户和 Google-日历数据。显然,您的用户必须使用他们 google 的凭据登录,这样您的应用才能获取他们的数据并进行管理。您的应用程序管理用户的数据,而无需获取用户的凭据。
到目前为止一切顺利。
在此示例中,Authorization-Server 是 Google 帐户。 Resource Server 是 Google-Main 和 Google-Calendar(两者),Client 是您的应用程序.
希望这是有道理的。
您需要资源服务器,因为它是 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 的一部分。
资源服务器配置不仅仅是安全配置,参见OAuth 2 Developers Guide:
Resource Server Configuration
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 aResourceServerConfigurer
. The following features can be configured:
- 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
The
@EnableResourceServer
annotation adds a filter of typeOAuth2AuthenticationProcessingFilter
automatically to the Spring Security filter chain.
您可以使用 Spring 安全配置 (WebSecurityConfigurerAdapter
) 对 受保护资源的其他自定义 Spring 安全 [=35] 中的 HttpSecurity 配置器允许=],但最好使用资源服务器配置,因为:
- 封装(资源服务器所有配置合一class)
- 配置顺序(您不必更改顺序)
- 复杂性(一个 class 而不是两个 classes)
也是推荐的方式