Spring security 如何在Spring Oauth2 Security中配置/public、/protected、/private访问路径

Spring security 如何在Spring Oauth2 Security中配置/public、/protected、/private访问路径,spring-security,Spring Security,我有一个非常具体的要求,在我的项目有关的身份和授权。我想从我的REST服务模块中打开3个路径/public/,/protected/和/private/,其行为如下: 以/public/开头的URL无需任何身份验证或授权即可访问 只有用户经过身份验证,才能访问以/private/开头的URL 只有用户经过身份验证和授权,才能访问以/protected/开头的URL 为了实现这一点,我通过扩展“SpringResourceServerConfigurator&覆盖configure方法”构建了一个

我有一个非常具体的要求,在我的项目有关的身份和授权。我想从我的REST服务模块中打开3个路径
/public/
/protected/
/private/
,其行为如下:

  • /public/
    开头的URL无需任何身份验证或授权即可访问
  • 只有用户经过身份验证,才能访问以
    /private/
    开头的URL
  • 只有用户经过身份验证和授权,才能访问以
    /protected/
    开头的URL
  • 为了实现这一点,我通过扩展“SpringResourceServerConfigurator&覆盖
    configure
    方法”构建了一个
    Configurator
    。但不幸的是,它不起作用。我也尝试过使用“SpringWebServiceConfigurator&使用IgnoreAntURL支持”,但同样的方法也不起作用。仅适用于
    /private/
    /protected/
    URL的配置如下所示

    http.anonymous()
        .disable()
        .requestMatchers()
        .antMatchers("/protected/**", "/private/**")
        .and();
    
    for (String protectedApiEp : configuredApis) {
        http.authorizeRequests()
            .antMatchers("/protected/" + protectedApiEp + "/**")
            .hasAuthority(protectedApiEp);
    }
    
    http.authorizeRequests()
        .antMatchers("/protected/**").denyAll()
        .antMatchers("/private/**").permitAll()
        .and()
        .sessionManagement()
        .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    

    有人能告诉我如何使用上述配置启用对所有用户开放的
    /public/
    URL吗?

    以下配置应该可以工作:

    @EnableWebSecurity
    公共类WebApplicationSecurityConfiguration扩展了WebSecurity配置适配器{
    @凌驾
    受保护的void configure(最终HttpSecurity http)引发异常{
    //允许Spring安全性对请求进行授权。
    http
    .授权请求()
    //允许任何人访问以/public/开头的URL。
    .antMatchers(“/public/**”).permitAll()
    //允许具有受保护角色的任何人访问以/protected/开头的URL。
    .antMatchers(“/protected/**”).hasAuthority(“protected”)
    //允许成功通过身份验证的任何人访问所有其他URL。
    .anyRequest().authenticated()
    .及()
    .会议管理()
    .sessionCreationPolicy(sessionCreationPolicy.STATELESS);
    }
    }
    

    是一个示例应用程序,它显示了此配置的作用。以mvn clean spring boot的身份启动应用程序:运行,然后导航到以访问该应用程序。

    哪种类型的安全性?简单的spring安全性或spring oauth?/为什么要添加
    .anonymous().disable()
    (您编写的,您希望匿名访问
    public
    )和
    .antMatchers(“/protected/**”,“/private/**”)
    (覆盖其他行)。