I am doing a dropwizard maven project for custom authentication. I followed the document https://github.com/remmelt/dropwizard-oauth2-provider/blob/master/src/main/java/com/remmelt/examples/auth/SimpleAuthenticator.java. But I am getting the "Credentials are required to access this resource." this message when I run the project and access the URL from browser. Any help will be appreciated. My code is like this..
public class ExampleAuthenticator implements Authenticator<String, User>
{
@Override
public Optional<User> authenticate(String arg0)throws AuthenticationException {
// TODO Auto-generated method stub
User u = new User(arg0);
System.out.println("\n\n\nString arg0"+arg0);
return Optional.of(new User(u.getName()));
}
}
And code in Resource class
@GET
public String Token(@Auth User user)
{
System.out.println("In UserResource Class in Token Method");
return "Hello...";
}
Initializing authenticator in application class
environment.jersey().register(AuthFactory.binder(new OAuthFactory<User>(new ExampleAuthenticator(),"SUPER SECRET STUFF",User.class)));
OAuthis not something you can accomplish via using a simple browser. What you have implemented expects the following header to be set:Until you provide this, you'll get
401 Credentials required.A browser can understand
Basic Authenticationand show you a simple username-password dialog, but that's not possible forOAuthsince it's a highly flexible system with different formats for each application. That's the reason you provide web (REST) services usingdropwizardand a client (an application with javascript for instance if you want to use browser) queries this service while maintaining theOAuth token.I suggest reading more about
OAuth. In the meantime, you can test this application by adding the header I've mentioned above using a simple REST application such as Postman.