title: How to do Basic Authentication of a resource in Dropwizard
body: <p>I believe I have basic authentication working but I'm not sure how to protect resources so that they can only be accessed when the user is signed in. </p>
  
  <pre><code>public class SimpleAuthenticator implements Authenticator&lt;BasicCredentials, User&gt; {
      UserDAO userDao;
  
      public SimpleAuthenticator(UserDAO userDao) {this.userDao = userDao;}
  
      @Override
      public Optional&lt;User&gt; authenticate(BasicCredentials credentials) throws AuthenticationException    
      {
          User user = this.userDao.getUserByName(credentials.getUsername());
          if (user!=null &amp;&amp;
                  user.getName().equalsIgnoreCase(credentials.getUsername()) &amp;&amp;
                  BCrypt.checkpw(credentials.getPassword(), user.getPwhash())) {
              return Optional.of(new User(credentials.getUsername()));
          }
          return Optional.absent();
      }
  }
  </code></pre>
  
  <p>My Signin resource is like this:</p>
  
  <pre><code>@Path("/myapp")
  @Produces(MediaType.APPLICATION_JSON)
  public class UserResource {
      @GET
      @Path("/signin")
      public User signin(@Auth User user) {
          return user;
      }
  }
  </code></pre>
  
  <p>And I sign the user with:</p>
  
  <pre><code>~/java/myservice $ curl -u "someuser" http://localhost:8080/myapp/signin
  Enter host password for user 'someuser':
  {"name":"someuser"}
  </code></pre>
  
  <p><strong>Question</strong></p>
  
  <p>Let's say the user signs in from a browser or native mobile app front end using the <code>/myapp/signin</code> endpoint. How then can I protect another endpoint, say, <code>/myapp/{username}/getstuff</code> which <strong>requires a user to be signedin</strong></p>
  
  <pre><code>@GET
  @Path("/myapp/{username}/getstuff")
  public Stuff getStuff(@PathParam("username") String username) {
      //some logic here
      return new Stuff();
  }
  </code></pre>