【问题标题】:Spring boot security block non RestController endpointsSpring boot 安全块非 RestController 端点
【发布时间】:2019-05-11 21:28:17
【问题描述】:

我有一个应用程序暴露了一堆我没想到的端点。例如 localhost:8080/app/ 返回一个 URL 列表,其中包括与休眠实体相关的信息。

我不想启用基本身份验证,因为我配置了自己的身份验证。 但是,如果 URL 不是由我编写的 RestController 表示的 URL,那么我希望它到我拥有的现有禁止页面。

这是我当前的配置,但它不会阻止不需要的端点:

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/api/**").antMatchers("/v2/**").antMatchers("/webjars/**").antMatchers("/swagger-resources/**")
       .antMatchers("/swagger-ui.html");
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    super.configure(http);
    http.httpBasic().disable();
    http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
        .and()
        .csrf().disable();

    http.authenticationProvider(new CustomAuthenticationProvider()).authorizeRequests().anyRequest().authenticated()
        .antMatchers("/v2/**").permitAll().antMatchers("/webjars/**").permitAll().antMatchers("/swagger-resources/**").permitAll()
        .antMatchers("/swagger-ui.html").permitAll()
        .antMatchers("/health").permitAll();
    http.rememberMe().rememberMeServices(rememberMeService).useSecureCookie(useSecureCookie);

    //Show 403 on denied access
    http.exceptionHandling().authenticationEntryPoint(new Forbidden());
}

所以在这种情况下 localhost:8080/app/api/SearchControler/{var} 应该可以工作,但 localhost:8080/app/ 应该转到我的 Forbidden 入口点。而是 localhost:8080/app/ 进入 spring 用户名和密码页面。

首先我不知道为什么这些端点在没有 RestController 的情况下甚至会出现,其次为什么重定向到 403 页面如此困难。

我不确定我缺少什么配置。

* 编辑 *

我也试过了:

http.formLogin().and().httpBasic().disabled();

还有:

@EnableAutoConfiguration(exclude = {SecurityAutoConfiguration.class, ManagementWebSecurityAutoConfiguration.class})

似乎没有什么能阻止 spring 接管并做任何想做的事情。

【问题讨论】:

    标签: spring spring-boot spring-security


    【解决方案1】:

    在您的configure(HttpSecurity http) 方法中删除super.configure(http); 后重试。

    文档

    重写此方法以配置 {@link HttpSecurity}。通常 子类 * 不应通过调用 super 来调用此方法 可能会覆盖他们的 * 配置。默认配置是: http.authorizeRequests().anyRequest().authenticated().and().formLogin().and().httpBasic();

    【讨论】:

    • 我尝试过使用和不使用超级调用,但都不起作用。
    【解决方案2】:

    我认为还有更多您没有展示的配置,但无论如何:

    @Override
    public void configure(WebSecurity web) throws Exception {
        //this is only for ignoring static resources in your app, sth that is never changed (you can cash it) and public (available for any user on the internet (ex. /js /css - sth else static img etc)
        web.ignoring().antMatchers("/webjars/**").antMatchers("/swagger-resources/**");
    }
    
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //super.configure(http); this call the default configuration, if you implement this method you shouldn't call the default one
        http.httpBasic().disable();
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
            .csrf().disable();
    
        http.authenticationProvider(new CustomAuthenticationProvider())
            .authorizeRequests() // the order is matter here, if antMatchers() will match the request the others after him will not be checked, anyRequest() will match any request, so it should be at the end 
          //.permitAll().antMatchers("/webjars/**").permitAll().antMatchers("/swagger-resources/**").permitAll() - there is no need to duplicate what is in web.ignoring() - such requests will not reach this point 
    
            .antMatchers("/swagger-ui.html").permitAll()
            .antMatchers("/health").permitAll()
            .anyRequest().authenticated()
        http.rememberMe().rememberMeServices(rememberMeService).useSecureCookie(useSecureCookie);
    
        //Show 403 on denied access
        http.exceptionHandling().authenticationEntryPoint(new Forbidden());
    }
    

    【讨论】:

    • 此配置将每个请求路由到 spring 基本身份验证登录页面。 http.httpBasic().disable(); 是怎么回事?不会杀死春季基本身份验证中的所有内容吗?在我看来它应该。
    【解决方案3】:

    这个问题与传递依赖完全相关。在删除一些依赖项并添加排除项后,核心问题已经解决。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-10
      • 2016-08-26
      • 1970-01-01
      • 2020-11-13
      • 2020-04-19
      • 2020-05-23
      • 2018-06-27
      • 1970-01-01
      相关资源
      最近更新 更多