简体   繁体   中英

Getting Error “Credentials are required” for accessing dropwizard oauth web services

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)));

OAuth is not something you can accomplish via using a simple browser. What you have implemented expects the following header to be set:

Authorization: Bearer {your username}

Until you provide this, you'll get 401 Credentials required .

A browser can understand Basic Authentication and show you a simple username-password dialog, but that's not possible for OAuth since it's a highly flexible system with different formats for each application. That's the reason you provide web (REST) services using dropwizard and a client (an application with javascript for instance if you want to use browser) queries this service while maintaining the OAuth 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM