Spring Security OAuth2受保护资源实际上没有受到保护......过滤器不起作用?

时间:2017-05-31 18:56:45

标签: spring spring-security oauth-2.0 spring-security-oauth2 spring-oauth2

直接点击端点:http://localhost:8080/oauth2-password/helloworld ,仍然可以获得字符串“Hello World!”..请查看下面的配置,请告诉我原因。这非常令人沮丧。

AUTHORIZATION SERVER

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    AuthenticationManager authenticationManager; 

    @Primary
    @Bean
    InMemoryTokenStore tokenStore() {
        return new InMemoryTokenStore();
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
      endpoints.authenticationManager(this.authenticationManager).tokenStore(this.tokenStore());
    }


    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security.checkTokenAccess("isAuthenticated()");
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
        .withClient("client")
        .resourceIds("app")
        .authorizedGrantTypes("password")
        .scopes("read", "write", "trust")
        .refreshTokenValiditySeconds(20000)
        .accessTokenValiditySeconds(600);
    }

}

资源服务器

@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {

    @Autowired
    AuthenticationManager authManager;

    @Autowired
    TokenStore tokenStore;

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/").permitAll().antMatchers("/helloworld/**").authenticated();
    }

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        resources.resourceId("app").tokenStore(this.tokenStore).authenticationManager(this.authManager);
    }

}

WEB SECURITY CONFIG

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

}

1 个答案:

答案 0 :(得分:2)

哇,惊讶没有人能抓到这个。记录极差,但经过几天的搜索后找到答案。

对于那些以这种方式发现并且发现他们正确配置ResourceServer,AuthorizationServer和WebSecurityConfigurerAdapter的人来说,你仍然可以完全正常地点击端点,好像那个怪异的过滤器还没有活着, 这是答案:

在实现@Configuration的类路径中添加AbstractSecurityWebApplicationInitializer带注释的类。调用SecurityWebAppInitializer类或任何你想要生成senes的东西。确保覆盖所有方法,并将它们保留为默认实现。确保将此类注册到Spring上下文中(以及其他配置类)。

重新编译,重启服务器等......

动臂。工作,就像这样。点击一个端点,并使用401 未经授权。

这个Abstract类的作用是注册DelegatingFilterProxy以在任何其他注册的Filter之前使用springSecurityFilterChain。啊。注册springSecurityFilterChain时,在XML中轻松完成的任务。

相关问题