de.coderspack:jwt-spring-boot-sample-app

Coders Pack Spring Boot JWT Starter

License

License

Categories

Categories

Spring Boot Container Microservices
GroupId

GroupId

de.coderspack
ArtifactId

ArtifactId

jwt-spring-boot-sample-app
Last Version

Last Version

0.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

Coders Pack Spring Boot JWT Starter
Project URL

Project URL

http://www.coderspack.de/jwt-spring-boot-sample-app

Download jwt-spring-boot-sample-app

How to add to project

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

Dependencies

compile (5)

Group / Artifact Type Version
de.coderspack : jwt-spring-boot-starter jar 0.0.1
org.springframework.boot : spring-boot-starter-data-jpa jar 2.2.1.RELEASE
org.springframework.boot : spring-boot-starter-data-rest jar 2.2.1.RELEASE
org.springframework.boot : spring-boot-devtools jar 2.2.1.RELEASE
org.springframework.security : spring-security-test jar 5.2.0.RELEASE

provided (1)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter-security jar 2.2.1.RELEASE

runtime (1)

Group / Artifact Type Version
com.h2database : h2 jar 1.4.199

test (2)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter-test jar 2.2.1.RELEASE
org.junit.jupiter : junit-jupiter jar 5.5.2

Project Modules

There are no modules declared in this project.

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.1