Spring OAuth2 Resource Server 配置
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进行身份验证。
评论已关闭