首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用antMatchers配置oauth2ResourceServer

用antMatchers配置oauth2ResourceServer
EN

Stack Overflow用户
提问于 2022-01-26 07:46:08
回答 1查看 394关注 0票数 0

我的目标是以以下方式配置Spring安全性:

从私有开始的任何路由都应该通过Spring oauth2ResourceServer

  • All进行身份验证,其他路由应该是可自由访问的

我已经尝试了下面的代码,但这给了我一个问题,即它也试图验证非私有的其他路由。

代码语言:javascript
运行
复制
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and()
                .authorizeRequests()
                .antMatchers("/private/**").authenticated()
                .antMatchers("/**").permitAll()
                .and()
                .oauth2ResourceServer().jwt();

      }

}

我的依赖关系是:

  • org.springframework.boot:spring-boot-starter-oauth2-resource-server:2.6.2
  • org.springframework.boot:spring-boot-starter-security:2.6.2
  • org.springframework.boot:spring-boot-starter-web:2.6.2

有什么想法吗?

EN

回答 1

Stack Overflow用户

发布于 2022-01-26 11:07:14

结果被csrf保护所阻塞,这在春季安全中是默认的。

以下是我的工作:

代码语言:javascript
运行
复制
@Override
public void configure(HttpSecurity http) throws Exception {
    http
            .cors().and()
            .csrf().disable()
            .authorizeRequests()
            .mvcMatchers("/private/**").authenticated()
            .mvcMatchers("/**").permitAll()
            .and()
            .oauth2ResourceServer().jwt();
}

请注意,要使其工作,您需要在application.properties中指定以下内容。

代码语言:javascript
运行
复制
spring.security.oauth2.resourceserver.jwt.jwk-set-uri

例如,在google oauth2的例子中,这是:

代码语言:javascript
运行
复制
spring.security.oauth2.resourceserver.jwt.jwk-set-uri=https://www.googleapis.com/oauth2/v3/certs

这指向用于验证令牌的JSON密钥(JWK)。您的令牌应该以以下形式作为授权头发送到您的spring服务器:

代码语言:javascript
运行
复制
bearer {{your token here}}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70860005

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档