我正在尝试使用 JWT token 实现 OAuth2 身份验证。如果我理解,我需要将凭据发送到授权服务器,这会验证我的凭据,并返回签名的 JWT token 。接下来,我尝试实现扩展了 WebSecurityConfigurerAdapter
的 WebSecurityConfig
,并且我必须设置哪些端点是安全的,哪些不是。
但我的问题是:我需要资源服务器吗?它与我潜在的 WebSecurityConfig
执行相同的工作,或者不是?
我的目标是为我的网站创建简单的 JWT 身份验证。
最佳答案
您需要资源服务器,因为它是 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 Security 配置 (WebSecurityConfigurerAdapter
) 来对 Spring Security 中的 HttpSecurity 配置器允许的 protected 资源进行其他自定义,但最好使用资源服务器配置,因为:
- 封装(资源服务器的所有配置都在一个类中)
- 配置顺序(无需更改顺序)
- 复杂性(一个类而不是两个类)
这是推荐的方式。
关于java - 我需要使用 Spring Security OAuth2 的资源服务器吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48629266/