GithubHelp home page GithubHelp logo

nickscha / servlet-auth Goto Github PK

View Code? Open in Web Editor NEW
4.0 0.0 0.0 29 KB

A simplistic servlet example project for programmatic authentication

License: Apache License 2.0

Java 90.24% HTML 9.76%
servlet authentication sample-app sample-code jsf webapp

servlet-auth's Introduction

servlet-auth

Build Status Size License

A simplistic servlet example project for custom programmatic authentication.

When JASPIC, Container, JAAS, Identity Stores (Keycloak, ...) do not fit with your requirements this can be an alternative. You write your custom authentication code once and you can deploy it to every available container which supports your targeted servlet specification.

Please note: For EE Containers EJB Security won't work.

Installation

  • Download, Build with Maven and deploy it to any container with servlet capabilities
  • Open your browser and go to localhost:8080/servlet-auth
  • The login page will automatically popup
  • Enter test=test as user and password
  • MyLoginModule class will be invoked and MyCustomPrincipal will be set

Launch project with embedded jetty

  mvn jetty:run-war

Requirements

  • Java 8 or later
  • A container with servlet 3.1 support

Tested with

  • Tomcat 8.5.15
  • Jetty 9.4.6
  • Wildfly 10
  • Payara 172

Classes and responsibilities

AuthenticationRequestFilter

This Servlet fiter is invoked for all requests on your webapp and checks if you're logged in and authorized to access an page, resource, image, ... If not you'll be redirected to the login page.

AuthenticationRequestWrapper

This request wrapper will override methods such as request::getUserPrincipal, request::isUserInRole(String), request::getRemoteUser() so that you get your custom principal, roles will be assigned whenever you call them on your webapp.

Notice: These methods will also work available in JSF for the FacesContext (e.g. ExternalContext::getUserPrincipal)

MyLoginServlet (path=/login, action=post)

Authenticates the user and in this sample project creates the MyCustomPrincipal object. Delegate to your custom authentication (LDAP, ...) here.

MyLogoutServlet (path=/logout, action=post)

Invalidates the Http Session.

Further Stuff

CDI @Inject MyCustomPrincipal

Use the following code (JSF + CDI):

  @Produces
  public MyCustomPrincipal producePrincipal(){
    // getRemoteUser, isCallerInRole will also work as usual
    return FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
  }

servlet-auth's People

Contributors

nickscha avatar

Stargazers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.