title: Configure Spring Security with Jersey REST and Swagger UI
body: <p>I have a Java Spring Boot Jersey (REST) application in which I want to add Spring Security (for LDAP in my case). Also, I'm using Swagger UI.</p>
  
  <p>In my <code>application.yml</code> file I think I need to have <code>spring.jersey.type: filter</code> to serve static Swagger files.
  However, the security doesn't seem to protect the jersey endpoints anymore with that setting.
  It (security) does however protect swagger files.</p>
  
  <p>My controller:</p>
  
  <pre><code>@Component
  @Path("/report")
  @Produces(MediaType.APPLICATION_JSON)
  public class ReportController {
      @GET
      public String getReport(){
          // stuff...
  </code></pre>
  
  <p>My jersey config:</p>
  
  <pre><code>@Component
  public class JerseyConfig extends ResourceConfig {
      public JerseyConfig() {
          packages(true, "package.resources");
          property(ServletProperties.FILTER_FORWARD_ON_404, true);
      }
  }
  </code></pre>
  
  <p>Security config</p>
  
  <pre><code>@Configuration
  @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
  public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Resource
      private LdapConfiguration ldapConfiguration;
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http
              .authorizeRequests()
                  .antMatchers("/**").hasRole(ldapConfiguration.getValidationRole())
                  .and()
              .formLogin();
      }
  
      @Configuration
      protected static class AuthenticationConfiguration extends GlobalAuthenticationConfigurerAdapter {
  
          @Resource
          private LdapConfiguration ldapConfiguration;
  
          private LdapContextSource getLdapContext() throws Exception {
              LdapContextSource context = new LdapContextSource();
              context.setUrl(ldapConfiguration.getUrl());
              context.setBase(ldapConfiguration.getBase());
              context.setUserDn(ldapConfiguration.getUserDn());
              context.setPassword(ldapConfiguration.getPassword());
              context.afterPropertiesSet();
              return context;
          }
  
          @Override
          public void init(AuthenticationManagerBuilder auth) throws Exception {
              auth
                  .ldapAuthentication()
                  .userSearchFilter(ldapConfiguration.getUserSearchFilter())
                  .userSearchBase(ldapConfiguration.getUserSearchBase())
                  .groupSearchBase(ldapConfiguration.getGroupSearchBase())
                  .groupRoleAttribute(ldapConfiguration.getGroupRoleAttribute())
                  .groupSearchFilter(ldapConfiguration.getGroupSearchFilter())
                  .contextSource(getLdapContext());
          }
      }
  }
  </code></pre>
  
  <p>application.yml</p>
  
  <pre><code>server:
    context-path: /myapi
    port: 9091
  spring:
    jersey.type: filter
  </code></pre>