Spring OAuth2 Resource Server 配置
warning:
这篇文章距离上次修改已过229天,其中的内容可能已经有所变动。
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configurers.oauth2.server.resource.OAuth2ResourceServerConfigurer;
@Configuration
public class OAuth2ResourceServerConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().authenticated()
.and()
.oauth2ResourceServer()
.jwt();
}
}
这段代码演示了如何在Spring应用程序中配置OAuth2资源服务器,以保护API端点并验证传入请求的JWT令牌。它使用configure
方法来配置HttpSecurity,启用OAuth2资源服务器并指定使用JWT进行身份验证。
评论已关闭