1

我一直致力于使用 Spring Security Oauth 保护 Restful 服务。我一直在努力尝试使用 SSL 保护 /oauth/token 端点并且只允许 POST 调用。

我正在使用 @EnableAuthorizationServer 声明

用于在当前应用程序上下文中启用授权服务器(即 AuthorizationEndpoint 和 TokenEndpoint)的便利注释,该上下文必须是 DispatcherServlet 上下文。可以使用 AuthorizationServerConfigurer 类型的 @Beans 自定义服务器的许多功能(例如,通过扩展 AuthorizationServerConfigurerAdapter)。用户负责使用普通 Spring Security 功能(@EnableWebSecurity 等)保护授权端点(/oauth/authorize),但令牌端点(/oauth/token)将使用客户端凭据上的 HTTP 基本身份验证自动保护。必须通过一个或多个 AuthorizationServerConfigurer 提供 ClientDetailsS​​ervice 来注册客户端。

这很好,但我似乎无法覆盖令牌端点部分或强制执行仅 POST 调用,就像使用 intercept-url xml 语法一样

@Configuration
@EnableAuthorizationServer
protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {

    @Bean
    public TokenStore tokenStore() {
        return new InMemoryTokenStore()
    }

    @Autowired
    AuthenticationManager authenticationManager

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
        endpoints
                .tokenStore(tokenStore())
                .authenticationManager(authenticationManager);

    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients
                .inMemory()
                .withClient('testApp')
                .scopes("read", "write")
                .authorities('ROLE_CLIENT')
                .authorizedGrantTypes("password","refresh_token")
                .secret('secret')
                .accessTokenValiditySeconds(7200)

    }
}

我保护了我的资源服务器

@Configuration
@EnableResourceServer
protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {

    @Autowired
    private RestAuthenticationEntryPoint authenticationEntryPoint;


    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .exceptionHandling()
                    .authenticationEntryPoint(authenticationEntryPoint)
                .and()
                    .requiresChannel().anyRequest().requiresSecure()
                .and()
                    .csrf()
                    .requireCsrfProtectionMatcher(new AntPathRequestMatcher("/oauth/authorize"))
                    .disable()
                .headers()
                    .frameOptions().disable()
                    .sessionManagement()
                    .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                    .authorizeRequests()
                    .antMatchers("/api/**").authenticated()
    }
}

使用 requiresChannel 的授权服务器 TokenEndpoint 安全性是否有类似的构建器语法?

4

1 回答 1

0

我最终使用创建了自己的配置 org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerSecurityConfiguration

因为我使用的是 Spring boot,所以我只是自动连接了 SecurityProperties 并在 Oauth 端点上为 SSL 添加了这一行

if (this.security.isRequireSsl()) {
    http.requiresChannel().anyRequest().requiresSecure();
}

对于 POST 要求

http .authorizeRequests() .antMatchers(HttpMethod.POST,tokenEndpointPath).fullyAuthenticated() .antMatchers(HttpMethod.GET,tokenEndpointPath).denyAll()

之后删除了 @EnableAuthorizationServer 以便它使用我的配置。

于 2014-07-31T18:44:49.863 回答