java - 我需要使用 Spring Security OAuth2 的资源服务器吗?

标签 java spring spring-security jwt spring-security-oauth2

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

但我的问题是:我需要资源服务器吗?它与我潜在的 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 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 Security 配置 (WebSecurityConfigurerAdapter) 来对 Spring Security 中的 HttpSecurity 配置器允许的 protected 资源进行其他自定义,但最好使用资源服务器配置,因为:

  • 封装(资源服务器的所有配置都在一个类中)
  • 配置顺序(无需更改顺序)
  • 复杂性(一个类而不是两个类)

这是推荐的方式。

关于java - 我需要使用 Spring Security OAuth2 的资源服务器吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48629266/

相关文章:

java - 如何一次将数据写入具有不同路径的多个文件?

java - 有没有办法从纯 Java 实现引导 Chef 节点?

java - 如何为 Spring 服务创建 Java 客户端?

java - Spring Boot - 使用 inMemoryAuthentication

java - 使用 NewGlobalRef 时的 JNI "local reference table overflow"

java - Spring 数据jdbc。无法为枚举添加自定义转换器

java - richer如何适配服务激活器和网关接口(interface)?

java - Spring bean 创建失败。 setter 的参数类型可以是 getter 返回类型的父级吗?

spring - 自定义 Spring Security OAuth2 与 Spring Social 集成

spring - 使用 LTPA token 进行预身份验证