Skip to content

Spring Security integration with Auth0 to secure your API with JWTs

License

Notifications You must be signed in to change notification settings

foundingnimo/auth0-spring-security-api

 
 

Repository files navigation

Auth0 Spring Security for API

CircleCI MIT Maven Download codecov

Spring Security integration with Auth0 to secure your API with Json Web Tokens (JWT)

This library targets Spring 4 and Spring Boot 1. If you are using Spring 5 and Spring Boot 2, please see the Spring Security 5 API Quickstart.

Download

Get Auth0 Spring Security API using Maven:

<dependency>
    <groupId>com.auth0</groupId>
    <artifactId>auth0-spring-security-api</artifactId>
    <version>1.3.1</version>
</dependency>

or Gradle:

implementation 'com.auth0:auth0-spring-security-api:1.3.1'

Usage

Inside a WebSecurityConfigurerAdapter you can configure your API to only accept RS256 signed JWTs:

@EnableWebSecurity
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        JwtWebSecurityConfigurer
                .forRS256("YOUR_API_AUDIENCE", "YOUR_API_ISSUER")
                .configure(http);
    }
}

or for HS256 signed JWTs:

@EnableWebSecurity
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        JwtWebSecurityConfigurer
                .forHS256("YOUR_API_AUDIENCE", "YOUR_API_ISSUER", "YOUR_API_SECRET".getBytes())
                .configure(http);
    }
}

If you need further customization (like a leeway for JWT verification) use the JwtWebSecurityConfigurer signatures which accept a JwtAuthenticationProvider.

Then using Spring Security HttpSecurity you can specify which paths requires authentication:

    http.authorizeRequests()
        .antMatchers("/api/**").fullyAuthenticated();

To restrict access based on the presence of a specific scope or permission claim, you can use the hasAuthority method. Scope and permissions claim values are prefixed with SCOPE_ and PERMISSION_, respectively.

To require a specific scope (read:users in the example below):

    http.authorizeRequests()
        .antMatchers(HttpMethod.GET, "/api/users/**").hasAuthority("SCOPE_read:users");

To require a specific permission (admin in the example below):

    http.authorizeRequests()
        .antMatchers(HttpMethod.GET, "/api/admin/**").hasAuthority("PERMISSION_admin");

JwtWebSecurityConfigurer#configure(HttpSecurity) also returns HttpSecurity so you can do the following:

@EnableWebSecurity
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        JwtWebSecurityConfigurer
                .forRS256("YOUR_API_AUDIENCE", "YOUR_API_ISSUER")
                .configure(http)
                .authorizeRequests()
                        .antMatchers(HttpMethod.GET, "/api/users/**").hasAuthority("SCOPE_read:users")
                        .antMatchers(HttpMethod.GET, "/api/admin/**").hasAuthority("PERMISSION_admin");
    }
}

Sample

Perhaps the easiest way to learn how to use this library (and quickly get started with a working app) is to study the Auth0 Spring Security API Sample and its README.

What is Auth0?

Auth0 helps you to:

  • Add authentication with multiple authentication sources, either social like Google, Facebook, Microsoft Account, LinkedIn, GitHub, Twitter, Box, Salesforce, amont others, or enterprise identity systems like Windows Azure AD, Google Apps, Active Directory, ADFS or any SAML Identity Provider.
  • Add authentication through more traditional username/password databases.
  • Add support for linking different user accounts with the same user.
  • Support for generating signed Json Web Tokens to call your APIs and flow the user identity securely.
  • Analytics of how, when and where users are logging in.
  • Pull data from other sources and add it to the user profile, through JavaScript rules.

Create a free account in Auth0

  1. Go to Auth0 and click Sign Up.
  2. Use Google, GitHub or Microsoft Account to login.

Issue Reporting

If you have found a bug or if you have a feature request, please report them at this repository issues section. Please do not report security vulnerabilities on the public GitHub issue tracker. The Responsible Disclosure Program details the procedure for disclosing security issues.

Author

Auth0

License

This project is licensed under the MIT license. See the LICENSE file for more info.

About

Spring Security integration with Auth0 to secure your API with JWTs

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%