<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: isaac uchechukwu</title>
    <description>The latest articles on DEV Community by isaac uchechukwu (@pryhmez).</description>
    <link>https://dev.to/pryhmez</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1178983%2F81220ec2-ea94-41f9-9196-703121b9fb46.jpeg</url>
      <title>DEV Community: isaac uchechukwu</title>
      <link>https://dev.to/pryhmez</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pryhmez"/>
    <language>en</language>
    <item>
      <title>Implementing Spring Security 6 with Spring Boot 3: A Guide to OAuth and JWT with Nimbus for Authentication</title>
      <dc:creator>isaac uchechukwu</dc:creator>
      <pubDate>Thu, 12 Oct 2023 09:05:49 +0000</pubDate>
      <link>https://dev.to/pryhmez/implementing-spring-security-6-with-spring-boot-3-a-guide-to-oauth-and-jwt-with-nimbus-for-authentication-2lhf</link>
      <guid>https://dev.to/pryhmez/implementing-spring-security-6-with-spring-boot-3-a-guide-to-oauth-and-jwt-with-nimbus-for-authentication-2lhf</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Since the introduction of Spring Security 6, I have encountered many developers who experienced issues getting their heads around setting it up to serve their business needs.&lt;/p&gt;

&lt;p&gt;Therefore if&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You are new to spring security&lt;/li&gt;
&lt;li&gt;or you have been using older versions of Spring Security and Spring Boot and are finding it difficult to implement Spring Security on Spring Boot 3 with Spring Security 6.&lt;/li&gt;
&lt;li&gt;You are looking for a simpler way to set up spring security so that you don't have to install external libraries for JWT and create complete filters.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then this article is for you.&lt;/p&gt;

&lt;p&gt;First, let's dive into the basics of spring security and what is required to set up spring security using Nimbus for JWT.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is spring security
&lt;/h2&gt;

&lt;p&gt;According to the definition in springs documentation Spring Security is a powerful and highly customizable authentication and access-control framework. It is the de facto standard for securing Spring-based applications.&lt;/p&gt;

&lt;p&gt;Spring Security is a framework that focuses on providing both authentication and authorization to Java applications.&lt;/p&gt;

&lt;p&gt;some of the features of spring security include &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Comprehensive and extensible support for both Authentication and Authorization&lt;/li&gt;
&lt;li&gt;Protection against attacks like session fixation, clickjacking, cross-site request forgery, etc&lt;/li&gt;
&lt;li&gt;Servlet API integration&lt;/li&gt;
&lt;li&gt;Optional integration with Spring Web MVC&lt;/li&gt;
&lt;li&gt;Much more…&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;it is important to use Spring security because it includes updated security features hence ensuring your application has up-to-date security features.&lt;/p&gt;

&lt;h2&gt;
  
  
  Project setup
&lt;/h2&gt;

&lt;p&gt;Alright, enough with all the theory with no action. Let's go straight into setting up our project.&lt;br&gt;
Now if you have stayed even a short while with spring you should know every spring developer's special website spring-initializer&lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;a href="https://start.spring.io/" rel="noopener noreferrer"&gt;
      start.spring.io
    &lt;/a&gt;
&lt;/div&gt;
 for initializing our spring projects. &lt;a href="https://start.spring.io/" rel="noopener noreferrer"&gt;https://start.spring.io/&lt;/a&gt;&lt;br&gt;
We are going to add the following dependencies

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqmxj0a6syrenmy5vye4h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqmxj0a6syrenmy5vye4h.png" alt="Image description" width="800" height="455"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So we added&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Spring web for building web APIs&lt;/li&gt;
&lt;li&gt;OAuth2 Resource Server for security&lt;/li&gt;
&lt;li&gt;Spring data JPA since we are going to be making use of storage to store user data&lt;/li&gt;
&lt;li&gt;PostgreSQL Driver because we are going to be using the Postgres database.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: Notice how we did not use spring security instead we used OAuth2 Resource Server That is because i discovered that OAuth2 Resource Server contains Nimbus which could be used for generating and managing JWTs without the need for extra dependency additions. Also, note that I used spring boot 3.1.4.&lt;/p&gt;

&lt;p&gt;Now once we are done with setting up our dependencies we download the jar file, extract our project, and open it in any IDE of our choice(in my case I chose Intellij).&lt;/p&gt;
&lt;h2&gt;
  
  
  Setting up application config
&lt;/h2&gt;

&lt;p&gt;Now we need to setup our application configuration and also setup a connection to our &lt;strong&gt;Postgres&lt;/strong&gt; database(that's if you are using Postgres, but if you decide to use an in-memory database like H2 you can check online for how to connect although you will still have to do that in the application.yaml or application.properties based on which you prefer).&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

spring:
  application:
    name: spring-security-jwt
  datasource:
    url: jdbc:postgresql://localhost:5432/spring-security
    username:
    password:
    driver-class-name: org.postgresql.Driver
  jpa:
    hibernate:
      ddl-auto: create-drop
    show-sql: true
    properties:
      hibernate:
        dialect: org.hibernate.dialect.PostgreSQLDialect
        format_sql: true
    database: postgresql
    database-platform: org.hibernate.dialect.PostgreSQLDialect

server:
  port: 8000
  error:
    include-message: always

rsa:
  private-key: classpath:certs/private-key.pem
  public-key: classpath:certs/public-key.pem

logging:
  level:
    org:
      springframework: INFO


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Creating public and private keys for encryption and decryption&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now in the resources directory, we create a folder called certs and then open our terminal and navigate into that directory running this command&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cd src/main/resources/certs&lt;br&gt;
&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;Then we will be using OpenSSL to generate an RSA-key keypair(this should come default for Mac users and can also be set for other users)&lt;/p&gt;

&lt;p&gt;Generate a Private Key (RSA):&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

openssl genpkey -algorithm RSA -out private-key.pem


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;This command generates an RSA private key and saves it to the private-key.pem file. &lt;/p&gt;

&lt;p&gt;Extract the Public Key from the Private Key by running:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

openssl rsa -pubout -in private-key.pem -out public-key.pem


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Then convert it to the appropriate PCKS format and replace the old one&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

 openssl pkcs8 -topk8 -inform PEM -outform PEM -in private-key.pem -out private-key.pem -nocrypt


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Alright, if that last step was all good let's move on to the next step.&lt;/p&gt;

&lt;p&gt;Recall, that in the application.yaml file there was a section of configuration that looked like this.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

rsa:
  private-key: classpath:certs/private-key.pem
  public-key: classpath:certs/public-key.pem


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;all we just did was tell spring where to find our public and private keys for encrypting and decrypting our JWT tokens.&lt;/p&gt;
&lt;h2&gt;
  
  
  Setting up our User modules and the using the RSAkeys in our application
&lt;/h2&gt;

&lt;p&gt;Now that we are done creating our RSAkeys using OpenSSL, what we have to do now is help spring-boot use it through configuration properties.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;first we need to create a package called &lt;strong&gt;config&lt;/strong&gt; then we create a file in the config package we created earlier and name it &lt;strong&gt;RsaKeyConfigProperties&lt;/strong&gt; and then paste the code below inside it&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

package com.tutorial.springsecurityjwt.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

@ConfigurationProperties(prefix = "rsa")
public record RsaKeyConfigProperties(RSAPublicKey publicKey, RSAPrivateKey privateKey ) {
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;Now we will create our &lt;strong&gt;user&lt;/strong&gt; module to manage all user activities then we will create the &lt;strong&gt;User.java&lt;/strong&gt; class that will serve as our entity.&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

package com.tutorial.springsecurityjwt.user;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.micrometer.common.lang.NonNull;
import jakarta.persistence.*;

import java.util.*;

@Entity
@Table(name = "users", uniqueConstraints = {
        @UniqueConstraint(columnNames = "user_name"),
        @UniqueConstraint(columnNames = "email")
})
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.UUID)
    @Column(unique = true)
    private String userId;

    @Column(name = "user_name", unique = true)
    @NonNull
    private String username;

    @NonNull
    @Column(name = "email", unique = true)
    private String email;

    @NonNull
    @JsonIgnore
    private String password;

    public User() {
    }

    public User(String userId, @NonNull String username, @NonNull String email, @NonNull String password) {
        this.userId = userId;
        this.username = username;
        this.email = email;
        this.password = password;
    }


    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    @NonNull
    public String getUsername() {
        return username;
    }

    public void setUsername(@NonNull String username) {
        this.username = username;
    }

    @NonNull
    public String getEmail() {
        return email;
    }

    public void setEmail(@NonNull String email) {
        this.email = email;
    }

    @NonNull
    public String getPassword() {
        return password;
    }

    public void setPassword(@NonNull String password) {
        this.password = password;
    }


    @Override
    public String toString() {
        return "User{" +
                "userId='" + userId + '\'' +
                ", username='" + username + '\'' +
                ", email='" + email + '\'' +
                ", password='" + password + '\'' +
                '}';
    }


}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;We will then create our &lt;strong&gt;UserRepository&lt;/strong&gt; interface to handle JPA database interaction and queries, it will extend JPARepository. paste the following code below.&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

package com.tutorial.springsecurityjwt.user;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface UserRepository extends JpaRepository&amp;lt;User, String&amp;gt; {
    Optional&amp;lt;User&amp;gt; findByUsername(String username);
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Setting up our Auth Modules&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Let's setup our AuthUser which will be used to manage user permissions and roles. Paste the following code in the AuthUser.java class in the auth module&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

package com.tutorial.springsecurityjwt.auth;

import com.tutorial.springsecurityjwt.user.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.stream.Collectors;

public class AuthUser extends User implements UserDetails {

    private final User user;

    public AuthUser(User user) {
        this.user = user;
    }

    public User getUser() {
        return user;
    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUsername();
    }

    @Override
    public Collection&amp;lt;? extends GrantedAuthority&amp;gt; getAuthorities() {
        return null;

    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;We will create our AuthServices.java class which will manage all logic that enable authentication. paste the following code below&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

package com.tutorial.springsecurityjwt.auth;

import com.tutorial.springsecurityjwt.user.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.jwt.JwtClaimsSet;
import org.springframework.security.oauth2.jwt.JwtEncoder;
import org.springframework.security.oauth2.jwt.JwtEncoderParameters;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.stream.Collectors;

@Service
public class AuthService {

    private static final Logger log = LoggerFactory.getLogger(AuthService.class);
    @Autowired
    private JwtEncoder jwtEncoder;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private UserRepository userRepository;


    public String generateToken(Authentication authentication) {
        Instant now = Instant.now();

        String scope = authentication.getAuthorities()
                .stream()
                .map(GrantedAuthority::getAuthority)
                .collect(Collectors.joining(" "));

        JwtClaimsSet claims = JwtClaimsSet.builder()
                .issuer("self")
                .issuedAt(now)
                .expiresAt(now.plus(10, ChronoUnit.HOURS))
                .subject(authentication.getName())
                .claim("scope", scope)
                .build();

        return jwtEncoder.encode(JwtEncoderParameters.from(claims)).getTokenValue();
    }



}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;We will also create a class in the auth package &lt;strong&gt;JpaUserDetailsService.java&lt;/strong&gt; which will handle loading users from the database for login.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

package com.tutorial.springsecurityjwt.auth;

import com.tutorial.springsecurityjwt.user.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;


@Service
public class JpaUserDetailsService implements UserDetailsService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        AuthUser user = userRepository
                .findByUsername(username)
                .map(AuthUser::new)
                .orElseThrow(() -&amp;gt; new UsernameNotFoundException("User name not found: " + username));

        return user;

    }
}



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Setting up our spring security config&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In our config package create a java class named &lt;strong&gt;SecurityConfig.java&lt;/strong&gt; and paste the following code inside.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

package com.tutorial.springsecurityjwt.config;

import com.nimbusds.jose.jwk.JWK;
import com.nimbusds.jose.jwk.JWKSet;
import com.nimbusds.jose.jwk.RSAKey;
import com.nimbusds.jose.jwk.source.ImmutableJWKSet;
import com.nimbusds.jose.jwk.source.JWKSource;
import com.nimbusds.jose.proc.SecurityContext;
import com.tutorial.springsecurityjwt.auth.JpaUserDetailsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.jwt.JwtDecoder;
import org.springframework.security.oauth2.jwt.JwtEncoder;
import org.springframework.security.oauth2.jwt.NimbusJwtDecoder;
import org.springframework.security.oauth2.jwt.NimbusJwtEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.web.servlet.handler.HandlerMappingIntrospector;

@Configuration
@EnableWebSecurity
@EnableMethodSecurity
public class SecurityConfig {


    private static final Logger log = LoggerFactory.getLogger(SecurityConfig.class);
    @Autowired
    private RsaKeyConfigProperties rsaKeyConfigProperties;
    @Autowired
    private JpaUserDetailsService userDetailsService;


    @Bean
    public AuthenticationManager authManager() {

        var authProvider = new DaoAuthenticationProvider();
        authProvider.setUserDetailsService(userDetailsService);
        authProvider.setPasswordEncoder(passwordEncoder());
        return new ProviderManager(authProvider);
    }



    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http, HandlerMappingIntrospector introspector) throws Exception {

        return http
                .csrf(csrf -&amp;gt; {
                    csrf.disable();
                })
                .cors(cors -&amp;gt; cors.disable())
                .authorizeHttpRequests(auth -&amp;gt; {
                    auth.requestMatchers("/error/**").permitAll();
                    auth.requestMatchers("/api/auth/**").permitAll();
                    auth.anyRequest().authenticated();
                })
                .sessionManagement(s -&amp;gt; s.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
                .oauth2ResourceServer((oauth2) -&amp;gt; oauth2.jwt((jwt) -&amp;gt; jwt.decoder(jwtDecoder())))
                .userDetailsService(userDetailsService)
                .httpBasic(Customizer.withDefaults())
                .build();
    }

    @Bean
    public JwtDecoder jwtDecoder() {
        return NimbusJwtDecoder.withPublicKey(rsaKeyConfigProperties.publicKey()).build();
    }

    @Bean
    JwtEncoder jwtEncoder() {
        JWK jwk = new RSAKey.Builder(rsaKeyConfigProperties.publicKey()).privateKey(rsaKeyConfigProperties.privateKey()).build();

        JWKSource&amp;lt;SecurityContext&amp;gt; jwks = new ImmutableJWKSet&amp;lt;&amp;gt;(new JWKSet(jwk));
        return new NimbusJwtEncoder(jwks);
    }

    @Bean
    PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;N.B: you have to add this line of code&lt;br&gt;
&lt;code&gt;@EnableConfigurationProperties(RsaKeyConfigProperties.class)&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
 to your base application, in my case SpringSecurityJwtApplication.java. Else your application won't pick up your configuration.&lt;/p&gt;

&lt;p&gt;Your base application should look something like this.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

package com.tutorial.springsecurityjwt;

import com.tutorial.springsecurityjwt.config.RsaKeyConfigProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;


@EnableConfigurationProperties(RsaKeyConfigProperties.class)
@SpringBootApplication
public class CollaboMainApplication {

    public static void main(String[] args) {
        SpringApplication.run(CollaboMainApplication.class, args);
    }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Setting up the controller
&lt;/h2&gt;

&lt;p&gt;Now we need to set up our auth rest controller to have routes for login and in a real-life case signup etc.&lt;br&gt;
But for this example, we would just be setting up a login route and we would create a hard-coded user in the database.&lt;br&gt;
In other to do this we have to first create our &lt;strong&gt;AuthDTO&lt;/strong&gt;(DTO means data transfer object) so that we can receive login username and password then we set up our &lt;strong&gt;AuthController.java&lt;/strong&gt; in our auth module/package and then paste the following code inside.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

package com.tutorial.springsecurityjwt.auth;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/auth")
@Validated
public class AuthController {

    private static final Logger log = LoggerFactory.getLogger(AuthController.class);

    @Autowired
    private AuthService authService;
    @Autowired
    private AuthenticationManager authenticationManager;

    @PostMapping("/login")
    public ResponseEntity&amp;lt;?&amp;gt; login(@RequestBody AuthDTO.LoginRequest userLogin) throws IllegalAccessException {
        Authentication authentication =
                authenticationManager
                        .authenticate(new UsernamePasswordAuthenticationToken(
                                userLogin.username(),
                                userLogin.password()));
        SecurityContextHolder.getContext().setAuthentication(authentication);

        AuthUser userDetails = (AuthUser) authentication.getPrincipal();


        log.info("Token requested for user :{}", authentication.getAuthorities());
        String token = authService.generateToken(authentication);

        AuthDTO.Response response = new AuthDTO.Response("User logged in successfully", token);

        return ResponseEntity.ok(response);
    }
}



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;Now we are done with the basic steps for setting up spring security and JWT with Nimbus, now we need to test and we need dummy users in the database. We are going to do this by using the commandline runner to create users at the start of our application. Hence we will create a bean in our &lt;strong&gt;SpringSecurityJwtApplication.java&lt;/strong&gt; class which is the entry point into our application. you can update you entry class with the code below.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

package com.tutorial.springsecurityjwt;

import com.tutorial.springsecurityjwt.config.RsaKeyConfigProperties;
import com.tutorial.springsecurityjwt.user.User;
import com.tutorial.springsecurityjwt.user.UserRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@EnableConfigurationProperties(RsaKeyConfigProperties.class)
@SpringBootApplication
public class SpringSecurityJwtApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringSecurityJwtApplication.class, args);
    }

    @Bean
    public CommandLineRunner initializeUser(UserRepository userRepository, BCryptPasswordEncoder passwordEncoder) {
        return args -&amp;gt; {

                User user = new User();
                user.setUsername("exampleuser");
                user.setEmail("example@gmail.com");
                user.setPassword(passwordEncoder.encode("examplepassword"));

                // Save the user to the database
                userRepository.save(user);

        };
    }

}



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Testing our login and JWT
&lt;/h2&gt;

&lt;p&gt;Now we are good to go all you have to do is to run your application and make sure you followed all the procedures and when your application starts without any errors you can test your login endpoint on Postman.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frbhpiqcrjfskn51ldxl4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frbhpiqcrjfskn51ldxl4.png" alt="Image description" width="800" height="619"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;A quick explanation of all the components of our setup and the authentication flow&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We set up our project using springdataJPA, OAuth2 Resource Server, postgres driver, and spring web.&lt;/li&gt;
&lt;li&gt;We created our user package, auth package, and config package.&lt;/li&gt;
&lt;li&gt;We created public-keys and private-keys using OpenSSL for encrypting and decrypting JWTs and linked it to our application through the applicationConfig.yaml and setting up properties.&lt;/li&gt;
&lt;li&gt;we created a user entity and a user repository for making JPA database calls.&lt;/li&gt;
&lt;li&gt;we created an auth user that would manage roles, credentials, etc.&lt;/li&gt;
&lt;li&gt;we created a JpaUserDetailsService for managing user details at sign-in, authService for managing auth logic like generating tokens.&lt;/li&gt;
&lt;li&gt;We created a DTO to help us manage data transfers between the client and server request and response.&lt;/li&gt;
&lt;li&gt;We used created an authController to manage routes for authentication requests like login.&lt;/li&gt;
&lt;li&gt;Then we setup our Security configurations to used Nimbus to manage JWTs and also user our UserDetailsService to manage user details on signin.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Below is a picture of what the file structure should look like&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F520bjgsbofn4mfi95ep4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F520bjgsbofn4mfi95ep4.png" alt="Image description" width="728" height="1378"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;in case you encounter any issue you can always refer to the GitHub repository. Below is a link to the GitHub repository containing the project code.&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/pryhmez" rel="noopener noreferrer"&gt;
        pryhmez
      &lt;/a&gt; / &lt;a href="https://github.com/pryhmez/springsecuritytutorial" rel="noopener noreferrer"&gt;
        springsecuritytutorial
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      
    &lt;/h3&gt;
  &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;and you can send me a mail&lt;br&gt;
&lt;strong&gt;&lt;a href="mailto:siruchechukwuisaac@gmail.com"&gt;siruchechukwuisaac@gmail.com&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
if you do have any questions.&lt;/p&gt;

&lt;p&gt;I really hope this article was of any help to you.&lt;/p&gt;

&lt;p&gt;Thanks.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>springsecurity</category>
      <category>springboot</category>
      <category>backenddevelopment</category>
    </item>
  </channel>
</rss>
