title: Caused by: java.lang.IllegalArgumentException: Pattern cannot be null or empty
body: <p>I am working on Spring Boot Microservices project using Spring Boot v2.5.2.</p>
  <p>Erorr</p>
  <pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.IllegalArgumentException: Pattern cannot be null or empty
      at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658) ~[spring-beans-5.3.8.jar:5.3.8]
      at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:486) ~[spring-beans-5.3.8.jar:5.3.8]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1334) ~[spring-beans-5.3.8.jar:5.3.8]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177) ~[spring-beans-5.3.8.jar:5.3.8]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564) ~[spring-beans-5.3.8.jar:5.3.8]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.8.jar:5.3.8]
      at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.8.jar:5.3.8]
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.8.jar:5.3.8]
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.8.jar:5.3.8]
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.8.jar:5.3.8]
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.3.8.jar:5.3.8]
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.8.jar:5.3.8]
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) ~[spring-beans-5.3.8.jar:5.3.8]
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.8.jar:5.3.8]
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.8.jar:5.3.8]
      at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.5.2.jar:2.5.2]
      at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) ~[spring-boot-2.5.2.jar:2.5.2]
      at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) ~[spring-boot-2.5.2.jar:2.5.2]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:338) ~[spring-boot-2.5.2.jar:2.5.2]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) ~[spring-boot-2.5.2.jar:2.5.2]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332) ~[spring-boot-2.5.2.jar:2.5.2]
      at com.example.PhotoAppApiUsersApplication.main(PhotoAppApiUsersApplication.java:17) ~[classes/:na]
  Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.IllegalArgumentException: Pattern cannot be null or empty
      at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.3.8.jar:5.3.8]
      at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-5.3.8.jar:5.3.8]
      ... 21 common frames omitted
  Caused by: java.lang.IllegalArgumentException: Pattern cannot be null or empty
      at org.springframework.util.Assert.hasText(Assert.java:289) ~[spring-core-5.3.8.jar:5.3.8]
      at org.springframework.security.web.util.matcher.AntPathRequestMatcher.&lt;init&gt;(AntPathRequestMatcher.java:113) ~[spring-security-web-5.5.1.jar:5.5.1]
      at org.springframework.security.web.util.matcher.AntPathRequestMatcher.&lt;init&gt;(AntPathRequestMatcher.java:98) ~[spring-security-web-5.5.1.jar:5.5.1]
      at org.springframework.security.web.util.matcher.AntPathRequestMatcher.&lt;init&gt;(AntPathRequestMatcher.java:86) ~[spring-security-web-5.5.1.jar:5.5.1]
      at org.springframework.security.web.util.matcher.AntPathRequestMatcher.&lt;init&gt;(AntPathRequestMatcher.java:75) ~[spring-security-web-5.5.1.jar:5.5.1]
      at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.setFilterProcessesUrl(AbstractAuthenticationProcessingFilter.java:358) ~[spring-security-web-5.5.1.jar:5.5.1]
      at com.example.security.WebSecurity.getAuthenticationFilter(WebSecurity.java:43) ~[classes/:na]
      at com.example.security.WebSecurity.configure(WebSecurity.java:34) ~[classes/:na]
      at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.getHttp(WebSecurityConfigurerAdapter.java:217) ~[spring-security-config-5.5.1.jar:5.5.1]
      at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.init(WebSecurityConfigurerAdapter.java:315) ~[spring-security-config-5.5.1.jar:5.5.1]
      at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.init(WebSecurityConfigurerAdapter.java:93) ~[spring-security-config-5.5.1.jar:5.5.1]
      at com.example.security.WebSecurity$$EnhancerBySpringCGLIB$$340b667b.init(&lt;generated&gt;) ~[classes/:na]
      at org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder.init(AbstractConfiguredSecurityBuilder.java:338) ~[spring-security-config-5.5.1.jar:5.5.1]
      at org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder.doBuild(AbstractConfiguredSecurityBuilder.java:300) ~[spring-security-config-5.5.1.jar:5.5.1]
      at org.springframework.security.config.annotation.AbstractSecurityBuilder.build(AbstractSecurityBuilder.java:38) ~[spring-security-config-5.5.1.jar:5.5.1]
      at org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration.springSecurityFilterChain(WebSecurityConfiguration.java:127) ~[spring-security-config-5.5.1.jar:5.5.1]
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64) ~[na:na]
      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
      at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
      at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.3.8.jar:5.3.8]
      ... 22 common frames omitted
  </code></pre>
  <p>pom.xml</p>
  <pre><code>&lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;2.5.2&lt;/version&gt;
          &lt;relativePath /&gt; &lt;!-- lookup parent from repository --&gt;
      &lt;/parent&gt;
      &lt;groupId&gt;com.example&lt;/groupId&gt;
      &lt;artifactId&gt;PhotoAppApiConfigServer&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;name&gt;PhotoAppApiConfigServer&lt;/name&gt;
      &lt;description&gt;Photo App Eureka Discovery Service&lt;/description&gt;
      &lt;properties&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
          &lt;spring-cloud.version&gt;2020.0.3&lt;/spring-cloud.version&gt;
      &lt;/properties&gt;
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
              &lt;artifactId&gt;spring-cloud-config-server&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;!-- &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
              &lt;artifactId&gt;spring-cloud-starter-bus-amqp&lt;/artifactId&gt;
          &lt;/dependency&gt; --&gt;
          &lt;!-- &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;
          &lt;/dependency&gt; --&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
              &lt;artifactId&gt;lombok&lt;/artifactId&gt;
              &lt;optional&gt;true&lt;/optional&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
      &lt;dependencyManagement&gt;
          &lt;dependencies&gt;
              &lt;dependency&gt;
                  &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-cloud-dependencies&lt;/artifactId&gt;
                  &lt;version&gt;${spring-cloud.version}&lt;/version&gt;
                  &lt;type&gt;pom&lt;/type&gt;
                  &lt;scope&gt;import&lt;/scope&gt;
              &lt;/dependency&gt;
          &lt;/dependencies&gt;
      &lt;/dependencyManagement&gt;
  </code></pre>
  <p>application.properties</p>
  <pre><code>server.port=0
  spring.application.name=users-ws
  
  eureka.client.register-with-eureka=true
  eureka.client.fetch-registry=true
  eureka.client.serviceUrl.defaultZone=http://localhost:8010/eureka
  
  eureka.instance.hostname=localhost
  eureka.instance.prefer-ip-address=true
  eureka.instance.instance-id=${spring.application.name}:${spring.application.instance_id:${random.value}}
  
  spring.config.import=optional:configserver:http://locahost:8012
  
  # MYSQL
  
  spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  spring.datasource.url=jdbc:mysql://localhost:3306/test
  spring.datasource.username=root
  spring.datasource.password=root
  spring.sql.init.platform=mysql
  spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
  
  spring.jpa.hibernate.ddl-auto=create
  spring.jpa.properties.hibernate.show_sql=true
  </code></pre>
  <p>WebAecurity.java</p>
  <pre><code>@Configuration
  @EnableWebSecurity
  public class WebSecurity extends WebSecurityConfigurerAdapter {
      @Autowired
      private Environment env;
      
      @Autowired
      private UsersService userService;
      
      @Autowired
      private BCryptPasswordEncoder bCryptPasswordEncoder;
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http.csrf().disable();
  //      http.authorizeRequests().antMatchers(&quot;/users/**&quot;).permitAll();
          
          // If Allow only IP address of API gateway  ==&gt; Works well
          http.authorizeRequests().antMatchers(&quot;/**&quot;).hasIpAddress(env.getProperty(&quot;gateway.ip&quot;))
          .and()
          .addFilter(getAuthenticationFilter())
          ;
      }
  
      private AuthenticationFilter getAuthenticationFilter() throws Exception {
          AuthenticationFilter authenticationFilter = new AuthenticationFilter(userService, env, authenticationManager());
          // This works with login path i.e http://localhost:8082/users-ws/login
  //      authenticationFilter.setAuthenticationManager(authenticationManager());
          
          authenticationFilter.setFilterProcessesUrl(env.getProperty(&quot;login.url.apth&quot;));
          return authenticationFilter;
      }
  
      @Override
      protected void configure(AuthenticationManagerBuilder auth) throws Exception {
          auth.userDetailsService(userService).passwordEncoder(bCryptPasswordEncoder);
      }
  }
  </code></pre>
  <p>AuthenticationFilter.java</p>
  <pre><code>public class AuthenticationFilter extends UsernamePasswordAuthenticationFilter {
      
      private UsersService userService;
      private Environment env;
      
      public AuthenticationFilter(UsersService usersService, Environment environment, AuthenticationManager authenticationManager) {
          this.userService = usersService;
          this.env = environment;
          super.setAuthenticationManager(authenticationManager);
      }
  
      @Override
      public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
              throws AuthenticationException {
  
          try {
              LoginRequestModel creds = new ObjectMapper().readValue(request.getInputStream(), LoginRequestModel.class);
  
              return getAuthenticationManager().authenticate(
                      new UsernamePasswordAuthenticationToken(
                              creds.getEmail(), 
                              creds.getPassword(), 
                              new ArrayList&lt;&gt;()));
          } catch (IOException e) {
              return (Authentication) new RuntimeException(e);
          }
      }
      
      @Override
      protected void successfulAuthentication(HttpServletRequest request, 
                                              HttpServletResponse response, 
                                              FilterChain chain,
                                              Authentication authResult) throws IOException, ServletException {
          
          // Get Username (here emailId) from principal object
          String username = ((User) authResult.getPrincipal()).getUsername();
          
          // Based on Username (i.e emailId) find User from DB
          UserDto userDto = userService.getUserDetailsByEmail(username);
          
          // Create JWT
          String token = Jwts.builder()
              .setSubject(userDto.getUserId())
              .setExpiration(new Date(System.currentTimeMillis()+Long.parseLong(env.getProperty(&quot;token.expiration_time&quot;))))
              .signWith(SignatureAlgorithm.HS512, env.getProperty(&quot;toke.secret&quot;))
              .compact();
          
          // Add it into Response Header to be able to pass in the subsequent requets
          response.addHeader(&quot;token&quot;, token);
          response.addHeader(&quot;userId&quot;, userDto.getUserId());
          
          System.out.println(&quot;TOKEN=&quot;+token);
          System.out.println(&quot;USERID=&quot;+userDto.getUserId());
      }
  }
  </code></pre>