【问题标题】:How to override the Spring Boot default OAuthTokenEndpoint如何覆盖 Spring Boot 默认的 OAuthTokenEndpoint
【发布时间】:2014-09-15 05:18:30
【问题描述】:

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

我正在使用@EnableAuthorizationServer 声明

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

这很好,但我似乎无法覆盖令牌端点部分或强制执行仅 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 安全性,是否有类似的构建器语法?

【问题讨论】:

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


    【解决方案1】:

    我最终创建了自己的配置,使用 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,以便它使用我的配置。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-29
    • 2016-06-06
    • 2017-01-29
    • 2015-08-30
    • 2019-05-15
    • 2015-12-27
    • 2018-06-13
    • 2016-08-20
    相关资源
    最近更新 更多