2017-10-09 59 views
0

我开发一个弹簧引导应用程序和问题,我有如下:春季安全的Oauth 2重定向行为对/的OAuth /令牌

默认情况下,我的控制器每个请求重定向到/登录,所以我不能用这种方式。

我可以通过在application.properties设置

security.oauth2.resource.filter-order = 3 

禁用/登录。

但我想知道如何禁用它只适用于非浏览器,所以我仍然可以使用登录。

编辑:

这将是我的做法:从什么/ API运行

http.antMatcher("/api/**") 
       .authorizeRequests().anyRequest().authenticated(); 

     http.antMatcher("/**") 
       .formLogin(); 

一切/是休息和只能由端Oauth处理。 其余应通过登录处理。

但它仍然没有工作,我仍然得到重定向。

没关系,修复它。感谢userWithLongNumber!

就我而言,事实证明我的WebSecurity配置错误。 这现在工作:

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    http. 
      formLogin().permitAll() 
      .and() 
      .authorizeRequests().anyRequest().authenticated(); 
} 

我RessourceServerConfig看起来是这样的:

@Override 
public void configure(HttpSecurity http) throws Exception { 

     http 
       .exceptionHandling() 
       .authenticationEntryPoint(customAuthenticationEntryPoint) 
       .and() 
       .logout() 
       .logoutUrl("/oauth/logout") 
       .logoutSuccessHandler(customLogoutSuccessHandler) 
       .and() 
       .headers() 
       .frameOptions().disable() 
       .and() 
       .sessionManagement() 
       .sessionCreationPolicy(SessionCreationPolicy.STATELESS) 
       .and() 
       .authorizeRequests() 
       .antMatchers("/api/**").authenticated(); 

    } 

} 

回答

1

您需要降低主要应用安全过滤器的优先级。

正如Spring Boot教程中所解释的。

的@EnableResourceServer注释创建了@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER-1)默认情况下,保安过滤器,所以通过移动主要应用安全@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER),我们可以确保对OAuth的规则资源优先。

设置

security.oauth2.resource.filter阶= 3

不会禁用主安全过滤器。它只是将其优先级改为较低的值。 (负值具有最高优先级,-3具有高于3的优先级)

据我所知,您的问题是在ResourceServerConfiguration中没有正确设置匹配器。使其更具体如下。

http.antMatcher("/helloOAuth") 
      .authorizeRequests().anyRequest().authenticated(); 

https://github.com/lanka-guide/simple-oauth2-server有一个示例实现。可能有帮助。