Home > Enterprise >  How to use REST API Authentication with java?
How to use REST API Authentication with java?

Time:11-06

My task is to make a user login GUI and add REST API for authentication. then launch the main GUI when the login is correct. Can someone let me know how to do this? Any tutorials for beginners? This must be done using Java. And in Eclipse IDE, I have to use this

CodePudding user response:

There are different method for rest api authentication. Some of them are following.

  • HTTP Basic Authentication
  • Cookies and Session
  • OAuth 2.0 (Token in HTTP Header).
  • API Keys

Best Option

There are several methods for the RESTful Authentication. It depends upon the use case to identify the best approach for the authentication. Our recommendation is to use the OAuth framework which is a powerful, flexible and provides both authorization and authentication.In case you are working on internal application and do not want to set up the entire workflow, probably HTTP basic authentication may work for you.

Here is the example of Oauth (end to end code)

https://www.baeldung.com/java-ee-oauth2-implementation

If you still looking some help. let me know I can write a sample code and send to you.

CodePudding user response:

Which framework are you using ? Since SpringBoot right now is the most popular choice, you are most likely using SpringSecurity to solve the problem.

Given these assumptions, here's how you approach it ...

You can configure the RESP API security like this ...

@Override
protected void configure(final HttpSecurity http) throws Exception {
    http
      ...
      // Secure REST API with expressions like this 
      .antMatchers("/admin/**").hasRole("ADMIN") 
      .antMatchers("/login*").permitAll()
      .anyRequest().authenticated()
      .and()
      // creates a GUI login page
      .formLogin() 
      // ...
}

You can find additional information to further tweak the configuration to fit your specific use cases by looking at these articles.

  • Related