spring-boot-starter-jwt

Coders Pack Spring Boot JWT Starter

License

License

Categories

Categories

Spring Boot Container Microservices
GroupId

GroupId

de.coderspack
ArtifactId

ArtifactId

spring-boot-starter-jwt
Last Version

Last Version

0.0.3
Release Date

Release Date

Type

Type

pom
Description

Description

spring-boot-starter-jwt
Coders Pack Spring Boot JWT Starter
Project URL

Project URL

http://www.coderspack.de
Source Code Management

Source Code Management

https://github.com/coderspack/jwt-spring-security-demo

Download spring-boot-starter-jwt

How to add to project

<!-- https://jarcasting.com/artifacts/de.coderspack/spring-boot-starter-jwt/ -->
<dependency>
    <groupId>de.coderspack</groupId>
    <artifactId>spring-boot-starter-jwt</artifactId>
    <version>0.0.3</version>
    <type>pom</type>
</dependency>
// https://jarcasting.com/artifacts/de.coderspack/spring-boot-starter-jwt/
implementation 'de.coderspack:spring-boot-starter-jwt:0.0.3'
// https://jarcasting.com/artifacts/de.coderspack/spring-boot-starter-jwt/
implementation ("de.coderspack:spring-boot-starter-jwt:0.0.3")
'de.coderspack:spring-boot-starter-jwt:pom:0.0.3'
<dependency org="de.coderspack" name="spring-boot-starter-jwt" rev="0.0.3">
  <artifact name="spring-boot-starter-jwt" type="pom" />
</dependency>
@Grapes(
@Grab(group='de.coderspack', module='spring-boot-starter-jwt', version='0.0.3')
)
libraryDependencies += "de.coderspack" % "spring-boot-starter-jwt" % "0.0.3"
[de.coderspack/spring-boot-starter-jwt "0.0.3"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

  • jwt-spring-boot-library
  • jwt-spring-boot-autoconfiguration
  • jwt-spring-boot-starter

Spring Boot JWT Starter Build Status License: MIT

This is a spring boot starter based on Stephan's Spring Boot JWT Demo. This starter is still in progress and not production ready.

Requirements

  • JDK 11 or higher
  • Spring Boot 2.3.x

Getting started

Coders pack spring boot jwt starter is available in maven central repository

Maven

<dependency>
  <groupId>de.coderspack</groupId>
  <artifactId>jwt-spring-boot-starter</artifactId>
  <version>0.0.3</version>
</dependency>

Gradle

implementation 'de.coderspack:spring-boot-starter-jwt:0.0.3'

Code

import de.coderspack.spring.boot.jwt.library.security.JWTConfigurer;
import de.coderspack.spring.boot.jwt.library.security.web.access.JwtAccessDeniedHandler;
import de.coderspack.spring.boot.jwt.library.security.web.access.JwtAuthenticationEntryPoint;

@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
   
    private final JWTConfigurer jwtConfigurer;
    private final JwtAuthenticationEntryPoint authenticationErrorHandler;
    private final JwtAccessDeniedHandler jwtAccessDeniedHandler;
 
    public WebSecurityConfig(JWTConfigurer jwtConfigurer,
                             JwtAuthenticationEntryPoint authenticationErrorHandler,
                             JwtAccessDeniedHandler jwtAccessDeniedHandler) {
       this.jwtConfigurer = jwtConfigurer;
       this.authenticationErrorHandler = authenticationErrorHandler;
       this.jwtAccessDeniedHandler = jwtAccessDeniedHandler;
    }

    // PasswordEncoder is required. Choose any!
    @Bean
    public PasswordEncoder passwordEncoder() {
       return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
       // Apply matchers and all the stuff you need!
        httpSecurity
            .authenticationEntryPoint(authenticationErrorHandler)
            .accessDeniedHandler(jwtAccessDeniedHandler)
            .apply(jwtConfigurer);
    }
}

Authenticate

import de.coderspack.spring.boot.jwt.autoconfigure.properties.JwtProperties;
import de.coderspack.spring.boot.jwt.library.security.JwtAuthenticationManager;

@RestController
@RequestMapping("/api")
public class AuthenticationRestController {

   private final JwtAuthenticationManager jwtAuthenticationManager;
   private final JwtProperties jwtProperties;

   public AuthenticationRestController(final JwtAuthenticationManager jwtAuthenticationManager, 
                                       final JwtProperties jwtProperties) {
      this.jwtAuthenticationManager = jwtAuthenticationManager;
      this.jwtProperties = jwtProperties;
   }

   @PostMapping("/authenticate")
   public ResponseEntity<String> authorize(@Valid @RequestBody LoginDto loginDto) {
      final var jwt = jwtAuthenticationManager.authenticate(loginDto.getUsername(), loginDto.getPassword(), loginDto.isRememberMe());

      final var httpHeaders = new HttpHeaders();
      httpHeaders.add(jwtProperties.getHeader(), "Bearer " + jwt);

      return new ResponseEntity<>(jwt, httpHeaders, HttpStatus.OK);
   }
}

Configure application.properties

# This token must be encoded using Base64 with mininum 88 Bits (you can type `echo 'secret-key'|base64` on your command line)
jwt.base64-secret=<my-secret-in-base64>

Implement org.springframework.security.core.userdetails.UserDetailsService.java.

TODO Migrate TODO Notes from https://github.com/coderspack/spring-boot-starter-jwt-java-demo to this documentation.

de.coderspack

coders pack

Versions

Version
0.0.3
0.0.2
0.0.1