【问题标题】:Unable to have a secure and non secure endpoint in a single Oauth secured Spring controller无法在单个 Oauth 安全 Spring 控制器中拥有安全和非安全端点
【发布时间】:2015-10-31 05:15:34
【问题描述】:

我正在使用 Spring Boot 组合一个 REST api,该 API 将通过 OAuth2 进行保护。我构建了一个安全应用程序,可以很好地管理 jwt 令牌。

我正在组合一个单独的应用程序,它将处理一些一般的用户配置文件资源请求,例如忘记密码、注册和配置文件获取操作。这是用 EnableOAuth2Resource 注释的,它正确地将 OAuth2AuthenticationProcessingFilter 添加到过滤器链中。

@SpringBootApplication
@EnableOAuth2Resource
public class ProfileApplication extends SpringBootServletInitializer {

    public static void main(String[] args) throws IOException {
        SpringApplication.run(ProfileApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(ProfileApplication.class);
    }
}

我面临的挑战是我找不到配置安全性的方法,以便对 /profiles 端点的 POST 请求不安全,而对 GET 或 PUT 的请求从提供的不记名令牌传入派生的 @AuthenticationPrincipal。

我想在 API 中设置以下内容 POST /profile 创建一个新用户 - 没有安全性 GET /profile/{id} 通过 id 获取用户 - 需要管理员权限或用户是 authd POST /password/reset - 开始密码重置 - 没有安全性

我有以下 bean 来配置安全性

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Order(-10) //SecurityProperties.ACCESS_OVERRIDE_ORDER)
class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override

    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/", "/public/**").permitAll()                
                .antMatchers(HttpMethod.POST, "/profiles/**").permitAll()
                .antMatchers(HttpMethod.GET, "/profiles/**").fullyAuthenticated()
                .antMatchers("/password/**").permitAll()
                .anyRequest().fullyAuthenticated()
                .and()
                .csrf().disable();
    }
}

由于上述情况,GET 端点调用失败并出现 403 错误,而没有尝试从令牌中查找当前用户,但帖子将通过。查看日志,我不再在过滤器链中看到 OAuth2AuthenticationProcessingFilter 。我尝试添加一些额外的过滤器似乎导致它不再被注册。

这个控制器方法看起来像:

@RequestMapping(method = {RequestMethod.GET}, value="/profiles/{login:.+}")
@ResponseBody
public ResponseEntity<Profile> get(@AuthenticationPrincipal Principal currentUser, @PathVariable String login) {

如果我将顺序设置为 SecurityProperties.ACCESS_OVERRIDE_ORDER 则 GET 请求有效,并且我看到基于 jwt 不记名令牌中的配置文件对我的 oauth 服务的查找,但对配置文件或密码控制器的 POST 请求失败并显示 401 . 所以看起来代码永远不会到达这个过滤器,而是被 OAuth2AuthenticationProcessingFilter 拦截并且请求失败了。

在使用 @EnableOAuth2Resource 时,有没有办法部分保护 Spring Boot 应用程序?我是否必须设置不同的配置 bean 来提供必要的覆盖,如果需要,基于什么接口?

【问题讨论】:

    标签: spring spring-security spring-boot spring-security-oauth2


    【解决方案1】:

    我认为您应该将您的 oauth2 资源从您的 SecurityConfig 中的请求匹配器中移动,并允许它们由资源服务器过滤器处理 - 扩展 ResourceServerConfigurerAdapter 的类。

    我的回答灵感来自 Dave Syer 的回答 here。你可以找到一个好的安全配置here

    或者尝试给 bean 一个更高的顺序。

    【讨论】:

    • 谢谢,但这似乎并不能解决问题。我的问题似乎仍然是,一旦将过滤器引入链中,就不会从传递的令牌中查找经过身份验证的用户
    • @AndrewRutter 尝试调试 org.springframework.security.web.FilterChainProxy 类的日志记录并检查过滤器链顺序以确认。 (stackoverflow.com/questions/10140515/…)
    【解决方案2】:

    @maleenc 上面的建议是正确的。我缺少以下 ResourceServerConfigurerAdapter。添加我的 OAuth2ServerConfiguration 类并删除其他安全过滤器解决了我的问题

    @Configuration
    public class OAuth2ServerConfiguration {
        @Configuration
        @EnableResourceServer
        protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
            @Override
            public void configure(HttpSecurity http) throws Exception {
                http.authorizeRequests()
                        .antMatchers("/", "/public/**").permitAll()                
                        .antMatchers("/password/**").permitAll()
                        .antMatchers(HttpMethod.POST, "/profiles").permitAll()
                        .and()
                        .csrf().disable();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-27
      • 2015-03-05
      • 2019-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-28
      相关资源
      最近更新 更多