title: Implement Spring Security for Rest Api
body: <p>I use this code for Rest API authentication:</p>
  
  <pre><code>@Override
      public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
              throws Exception {
          Optional&lt;String&gt; basicToken = Optional.ofNullable(request.getHeader(HttpHeaders.AUTHORIZATION))
                  .filter(v -&gt; v.startsWith("Basic"))
                  .map(v -&gt; v.split("\\s+")).filter(a -&gt; a.length == 2).map(a -&gt; a[1]);
          if (!basicToken.isPresent()) {
              return sendAuthError(response);
          }
  
          byte[] bytes = Base64Utils.decodeFromString(basicToken.get());
          String namePassword = new String(bytes, StandardCharsets.UTF_8);
          int i = namePassword.indexOf(':');
          if (i &lt; 0) {
              return sendAuthError(response);
          }
          String name = namePassword.substring(0, i);
          String password = namePassword.substring(i + 1);
  //        Optional&lt;String&gt; clientId = authenticationService.authenticate(name, password, request.getRemoteAddr());
          Merchants merchant = authenticationService.authenticateMerchant(name, password, request.getRemoteAddr());
          if (merchant == null) {
              return sendAuthError(response);
          }
          request.setAttribute(CURRENT_CLIENT_ID_ATTRIBUTE, merchant.getId());
          return true;
      }
  </code></pre>
  
  <p>How I can rewrite the code with Spring Security in order to get the same result but for different links to have authentication? For example:</p>
  
  <pre><code>localhost:8080/v1/notification - requests should NOT be authenticated.
  localhost:8080/v1/request - requests should be authenticated.
  </code></pre>