【问题标题】:How to tune authenticationEntryPoint behaviour Spring Security如何调整 authenticationEntryPoint 行为 Spring Security
【发布时间】:2019-10-10 23:12:51
【问题描述】:

我有基于 Spring Boot 2 的安全网关执行 OAuth2 身份验证,位于 GUI 应用程序和后端之前。 它的配置类似于

@Configuration
@EnableOAuth2Client
@EnableWebSecurity
public class SecurityGatewayConfig extends WebSecurityConfigurerAdapter{

    @Bean
    public SecurityGatewayAuthenticationFilter filter() {
        return new SecurityGatewayAuthenticationFilter("/login");
    }

    @Override
    public void configure(HttpSecurity http) {
        http
                .addFilterAfter(new OAuth2ClientContextFilter(), AbstractPreAuthenticatedProcessingFilter.class)
                .addFilterAfter(filter(), OAuth2ClientContextFilter.class)
                .httpBasic().authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/login"))
                .and()
...

它将请求重定向到 /loginSecurityGatewayAuthenticationFilter 对外部 OAuth2 提供者执行身份验证。

这对 GIU 应用程序很好。但是,在访问后端服务时(它们在路径中有 /api/)我需要不同的行为:如果请求未经过身份验证,则不要重定向,而是立即返回 401 错误。

任何想法,如何为此配置 Spring Security?

【问题讨论】:

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


    【解决方案1】:

    如果我的问题是正确的,那么您可以使用不同的 ConfigurationAdapter。基本思路如下:

        @Order(1)
        @Configuration
        @EnableOAuth2Sso
        public static class SecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
    
            @Autowired
            @Qualifier("defaultMatcher")
            private RequestMatcher defaultMatcher;
    
            @Override
            protected void configure(HttpSecurity http) throws Exception {
    
                http.requestMatcher(defaultMatcher)...
            }
        }
    
        @Order(2)
        @Configuration
        public static class OtherConfigurationAdapter extends WebSecurityConfigurerAdapter {
    
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                http.requestMatcher(yourRequestMatcher())...
            }
    
        }
    
    
    
    

    如果您使用@EnableResourceServer,Spring 将按照您使用 Order() 添加的顺序评估每个请求,这将始终具有 Order(3)

    然后您可以像这样构建您的请求匹配器(在此示例中,它匹配所有,但明确排除其他一些):

        @Bean
        public RequestMatcher defaultMatcher(@Qualifier("apiMatcher") RequestMatcher api, @Qualifier("anyother") RequestMatcher anyother) {
            final RequestMatcher all = new AntPathRequestMatcher("/**");
            final RequestMatcher nonApi = new NegatedRequestMatcher(new OrRequestMatcher(api, anyother));
    
            return new AndRequestMatcher(all, nonApi);
        }
    

    希望对您有所帮助。

    最好的问候, 维普

    【讨论】:

      【解决方案2】:

      你需要添加入口点过滤器

      @Component
      public final class CustomAuthenticationEntryPoint implements 
              AuthenticationEntryPoint {
          @Override
          public void commence(final HttpServletRequest request, final 
                  HttpServletResponse response, final AuthenticationException 
              authException) throws IOException {
              response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized");
          }
      }
      

      当客户端未经身份验证访问资源时...

      【讨论】:

        猜你喜欢
        • 2011-12-22
        • 1970-01-01
        • 2012-12-23
        • 1970-01-01
        • 2019-12-16
        • 2020-01-24
        • 2019-06-10
        • 2019-08-29
        • 2021-11-05
        相关资源
        最近更新 更多