com.github.achatain:java-webapp-authentication

Authentication framework leveraging Google sign-in for servlet based Java webapps

License

License

Categories

Categories

Java Languages
GroupId

GroupId

com.github.achatain
ArtifactId

ArtifactId

java-webapp-authentication
Last Version

Last Version

1.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

com.github.achatain:java-webapp-authentication
Authentication framework leveraging Google sign-in for servlet based Java webapps
Project URL

Project URL

https://github.com/achatain/java-webapp-authentication
Source Code Management

Source Code Management

http://github.com/achatain/java-webapp-authentication/tree/master

Download java-webapp-authentication

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.achatain/java-webapp-authentication/ -->
<dependency>
    <groupId>com.github.achatain</groupId>
    <artifactId>java-webapp-authentication</artifactId>
    <version>1.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.achatain/java-webapp-authentication/
implementation 'com.github.achatain:java-webapp-authentication:1.1.0'
// https://jarcasting.com/artifacts/com.github.achatain/java-webapp-authentication/
implementation ("com.github.achatain:java-webapp-authentication:1.1.0")
'com.github.achatain:java-webapp-authentication:jar:1.1.0'
<dependency org="com.github.achatain" name="java-webapp-authentication" rev="1.1.0">
  <artifact name="java-webapp-authentication" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.achatain', module='java-webapp-authentication', version='1.1.0')
)
libraryDependencies += "com.github.achatain" % "java-webapp-authentication" % "1.1.0"
[com.github.achatain/java-webapp-authentication "1.1.0"]

Dependencies

compile (5)

Group / Artifact Type Version
javax.servlet : javax.servlet-api jar 3.1.0
com.google.inject : guice jar 4.1.0
com.google.inject.extensions : guice-servlet jar 4.1.0
com.google.code.gson : gson jar 2.8.0
com.google.api-client : google-api-client jar 1.22.0

test (3)

Group / Artifact Type Version
junit : junit jar 4.12
org.mockito : mockito-core jar 2.2.21
org.hamcrest : hamcrest-junit jar 2.0.0.0

Project Modules

There are no modules declared in this project.

java-webapp-authentication

Build Status Coverage Status Maven Central

https://github.com/achatain/java-webapp-authentication

#What is it? JWA (Java Webapp Authentication) assists in leveraging Google Sign-In for backend server applications built with Java. Despite being in its early days, JWA provides you with a bunch of robust and easy to use features:

  • authentication filter
  • sign-in and sign-out servlets
  • session management service
  • etc.

#How do I integrate it in my backend app?

  1. Add the dependency in your pom file
<dependency>
 <groupId>com.github.achatain</groupId>
 <artifactId>java-webapp-authentication</artifactId>
 <version>1.1.0</version>
</dependency>
  1. Install the AuthenticationModule to enable the dependency injection (suggested to use Google Guice)
 class AppConfig extends GuiceServletContextListener {
   @Override
   protected Injector getInjector() {
     return Guice.createInjector(
       new AuthenticationModule()
     );
   }
 }
  1. Filter your restricted API through the SessionFilter and serve the sign-in and sign-out servlets
 class AppServletModule extends ServletModule {
   @Override
   protected void configureServlets() {
     Map<String, String> initParams = new HashMap<>();
     initParams.put(SessionFilter.LOGIN_URL_REDIRECT, "https://myapp.com/google-sign-in/");
     filter("/api/*").through(SessionFilter.class, initParams);
     serve("/google-auth").with(GoogleSigninServlet.class);
     serve("/signout").with(SignOutServlet.class);
   }
 }

#How do I know who is logged-in? From any servlet filtered through the SessionFilter, you can get the current user thanks to the SessionService

public class MyServlet extends HttpServlet {

   private final transient SessionService sessionService;

   @Inject
   private MyServlet(final SessionService sessionService) {
       this.sessionService = sessionService;
   }

   @Override
   protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
       AuthenticatedUser user = sessionService.getUserFromSession(req.getSession());
       System.out.println("The logged-in user is " + user);
   }
}

Versions

Version
1.1.0
1.0.1
1.0.0