Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
January 20, 2022 03:31 pm GMT

Spring Boot REST API authentication best practices using JWT (2022)

1. Overview

In this tutorial I will explain how you can implement production ready, token based REST API authentication using JWT (JSON Web Tokens). Further we will use these tokens to identify our acting user in a HTTP request to our API. For this tutorial we will use MongoDB to persist our user data, you can choose any database of your choice.

2. What is a JWT?

JSON Web Token(JWT) is an encoded string which we will use to identify our user in this case. A JWT consist of three parts separated by a period(.):

  1. Header: It contains signing algorithm like SHA256.
  2. Payload: It contains our user data.
  3. Signature: To verify the message wasn't changed along the way, making it secure.

Combing all three will make our JWT look something like this xxxxx.yyyyy.zzzzz. To learn more about JWT please visit - https://jwt.io/

3. Project Initialization

We will start y initializing our Spring Boot project using Spring Initiailizr. For starters I have added 5 dependencies, you can tweak it a little according to your project needs.

spring init.png

Hit generate and import project in your favorite IDE. Also, don't forget to add database properties in application.properties file.

spring.data.mongodb.database=your_db_name_herespring.data.mongodb.port=27017

4. Additional Dependencies

You will have to add following dependencies in order to use JWT in your project. commons-lang3 is optional, I personally use it for its various utility classes.

For maven based projects:

<dependency>    <groupId>io.jsonwebtoken</groupId>    <artifactId>jjwt-api</artifactId>    <version>0.11.2</version></dependency><dependency>    <groupId>io.jsonwebtoken</groupId>    <artifactId>jjwt-impl</artifactId>    <version>0.11.2</version>    <scope>runtime</scope></dependency><dependency>    <groupId>io.jsonwebtoken</groupId>    <artifactId>jjwt-jackson</artifactId>    <version>0.11.2</version>    <scope>runtime</scope></dependency><dependency>    <groupId>org.apache.commons</groupId>    <artifactId>commons-lang3</artifactId>    <version>3.12.0</version></dependency>

For gradle based projects:

dependencies {    implementation 'io.jsonwebtoken:jjwt-api:0.11.2'    runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.2',             'io.jsonwebtoken:jjwt-jackson:0.11.2',             'org.apache.commons:commons-lang3:3.0'}

5. Project Structure

We will follow MVC pattern, please refer to following project structure.

image.png

6. Configuration

In WebSecurityConfig.java, we will modify default spring security features by extending WebSecurityConfigurerAdapter class. Here we will define our HTTP request filter and a default response when the user is unauthenticated. It will act as a middleware for all our HTTP requests.

package com.example.api.config;import com.example.api.util.JwtRequestFilter;import com.fasterxml.jackson.databind.ObjectMapper;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.config.annotation.authentication.builders.AuthenticationManagerBuilder;import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;import org.springframework.security.config.annotation.web.builders.HttpSecurity;import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;import org.springframework.security.config.http.SessionCreationPolicy;import org.springframework.security.core.userdetails.UserDetailsService;import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;import java.util.HashMap;import java.util.Map;@Configuration@EnableWebSecurity@EnableGlobalMethodSecurity(prePostEnabled = true)public class WebSecurityConfig extends WebSecurityConfigurerAdapter {    private final UserDetailsService jwtUserDetailsService;    private final JwtRequestFilter jwtRequestFilter;    public WebSecurityConfig(UserDetailsService jwtUserDetailsService, JwtRequestFilter jwtRequestFilter) {        this.jwtUserDetailsService = jwtUserDetailsService;        this.jwtRequestFilter = jwtRequestFilter;    }    @Autowired    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {        auth.userDetailsService(jwtUserDetailsService).passwordEncoder(new BCryptPasswordEncoder());    }    @Bean    @Override    public AuthenticationManager authenticationManagerBean() throws Exception {        return super.authenticationManagerBean();    }    @Override    protected void configure(HttpSecurity httpSecurity) throws Exception {        httpSecurity.csrf().disable().authorizeRequests().antMatchers("/auth/*").permitAll().anyRequest().authenticated().and().exceptionHandling().authenticationEntryPoint((request, response, authException) -> {            Map<String, Object> responseMap = new HashMap<>();            ObjectMapper mapper = new ObjectMapper();            response.setStatus(401);            responseMap.put("error", true);            responseMap.put("message", "Unauthorized");            response.setHeader("content-type", "application/json");            String responseMsg = mapper.writeValueAsString(responseMap);            response.getWriter().write(responseMsg);        }).and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);    }}

In above configure(HttpSecurity httpSecurity) method we have defined to permit all request starting with /auth route that's where we will add our Authentication Controller. If the request is unauthorized our API will throw a 401 error message.

image.png

7. Request Filter

In JwtRequestFilter.java we will define our request filter which we mentioned in our API middleware in previous chapter. For this we will extend OncePerRequestFilter, Spring guarantees that it is executed only once for a given request.

In this doFilterInternal() method we will fetch JWT token from the request header and process it by validating and obtaining username from token's payload. Further if token is valid we will fetch user from database and add it in SecurityContextHolder, we can further use it any of our service to perform various user related operations.

package com.example.api.util;import com.example.api.service.JwtUserDetailsService;import io.jsonwebtoken.ExpiredJwtException;import org.apache.commons.lang3.StringUtils;import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;import org.springframework.security.core.context.SecurityContextHolder;import org.springframework.security.core.userdetails.UserDetails;import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;import org.springframework.stereotype.Component;import org.springframework.web.filter.OncePerRequestFilter;import javax.servlet.FilterChain;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;@Componentpublic class JwtRequestFilter extends OncePerRequestFilter {    private final JwtUserDetailsService jwtUserDetailsService;    private final JwtTokenUtil jwtTokenUtil;    public JwtRequestFilter(JwtUserDetailsService jwtUserDetailsService, JwtTokenUtil jwtTokenUtil) {        this.jwtUserDetailsService = jwtUserDetailsService;        this.jwtTokenUtil = jwtTokenUtil;    }    @Override    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)            throws ServletException, IOException {        final String requestTokenHeader = request.getHeader("Authorization");        if (StringUtils.startsWith(requestTokenHeader,"Bearer ")) {            String jwtToken = requestTokenHeader.substring(7);            try {                String username = jwtTokenUtil.getUsernameFromToken(jwtToken);                if (StringUtils.isNotEmpty(username)                        && null == SecurityContextHolder.getContext().getAuthentication()) {                    UserDetails userDetails = jwtUserDetailsService.loadUserByUsername(username);                    if (jwtTokenUtil.validateToken(jwtToken, userDetails)) {                        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken =                                new UsernamePasswordAuthenticationToken(                                        userDetails, null, userDetails.getAuthorities());                        usernamePasswordAuthenticationToken                                .setDetails(new WebAuthenticationDetailsSource().buildDetails(request));                        SecurityContextHolder.getContext()                                .setAuthentication(usernamePasswordAuthenticationToken);                    }                }            } catch (IllegalArgumentException e) {                logger.error("Unable to fetch JWT Token");            } catch (ExpiredJwtException e) {                logger.error("JWT Token is expired");            } catch (Exception e) {                logger.error(e.getMessage());            }        } else {            logger.warn("JWT Token does not begin with Bearer String");        }        chain.doFilter(request, response);    }}

In JwtTokenUtil.java we will perform all JWT token related operations such as generating new token and Validating given token.

package com.example.api.util;import io.jsonwebtoken.Claims;import io.jsonwebtoken.Jwts;import io.jsonwebtoken.SignatureAlgorithm;import io.jsonwebtoken.security.Keys;import org.springframework.security.core.userdetails.UserDetails;import org.springframework.stereotype.Component;import java.io.Serializable;import java.security.Key;import java.util.Date;import java.util.HashMap;import java.util.Map;import java.util.function.Function;@Componentpublic class JwtTokenUtil implements Serializable {    public static final long JWT_TOKEN_VALIDITY = 5 * 60 * 60;    Key key = Keys.secretKeyFor(SignatureAlgorithm.HS256);    public String getUsernameFromToken(String token) {        return getClaimFromToken(token, Claims::getSubject);    }    public Date getExpirationDateFromToken(String token) {        return getClaimFromToken(token, Claims::getExpiration);    }    public <T> T getClaimFromToken(String token, Function<Claims, T> claimsResolver) {        final Claims claims = getAllClaimsFromToken(token);        return claimsResolver.apply(claims);    }    private Claims getAllClaimsFromToken(String token) {        return Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(token).getBody();    }    private Boolean isTokenExpired(String token) {        final Date expiration = getExpirationDateFromToken(token);        return expiration.before(new Date());    }    public String generateToken(UserDetails userDetails) {        Map<String, Object> claims = new HashMap<>();        return Jwts.builder().setClaims(claims).setSubject(userDetails.getUsername()).setIssuedAt(new Date(System.currentTimeMillis())).setExpiration(new Date(System.currentTimeMillis() + JWT_TOKEN_VALIDITY * 1000)).signWith(key).compact();    }    public Boolean validateToken(String token, UserDetails userDetails) {        final String username = getUsernameFromToken(token);        return (username.equals(userDetails.getUsername()) && !isTokenExpired(token));    }}

8. Model and Repository

We will use Lombok framework here to quickly create our User.java model. It is completely optional but it is my favorite way of defining a model class. Afterall life is too short to write getters and setters.

package com.example.api.model;import lombok.AllArgsConstructor;import lombok.Getter;import lombok.NoArgsConstructor;import lombok.Setter;import org.springframework.data.annotation.Id;import org.springframework.data.mongodb.core.index.Indexed;import org.springframework.data.mongodb.core.mapping.Document;@Document("users")@Getter@Setter@AllArgsConstructor@NoArgsConstructorpublic class User {    @Id    private String userName;    private String firstName;    private String lastName;    @Indexed(unique = true)    private String email;    private String password;    private String role;}

We will write our UserRepository.java interface and define a method to fetch user details from username.

package com.example.api.repository;import com.example.api.model.User;import org.springframework.data.mongodb.repository.MongoRepository;import org.springframework.data.mongodb.repository.Query;public interface UserRepository extends MongoRepository<User, String> {    @Query(value = "{userName:'?0'}")    User findUserByUsername(String username);}

9. UserDetailsService

In JwtUserDetailsService.java class we will customize default spring security way of getting user by implementing UserDetailsService interface.

package com.example.api.service;import com.example.api.repository.UserRepository;import org.springframework.security.core.GrantedAuthority;import org.springframework.security.core.authority.SimpleGrantedAuthority;import org.springframework.security.core.userdetails.User;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;import java.util.ArrayList;import java.util.List;@Servicepublic class JwtUserDetailsService implements UserDetailsService {    final UserRepository userRepository;    public JwtUserDetailsService(UserRepository userRepository) {        this.userRepository = userRepository;    }    @Override    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {        com.example.api.model.User user = userRepository.findUserByUsername(username);        List<GrantedAuthority> authorityList = new ArrayList<>();        authorityList.add(new SimpleGrantedAuthority("USER_ROLE"));        return new User(user.getUserName(), user.getPassword(), authorityList);    }}

10. Controllers

Last but not the least we will define controllers in order to communicate with our API.

AuthenticationController.java will deal with user login and register. In both the routes we will generate JWT tokens and send it in response to the user.

package com.example.api.controller;import com.example.api.model.User;import com.example.api.repository.UserRepository;import com.example.api.service.JwtUserDetailsService;import com.example.api.util.JwtTokenUtil;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.http.ResponseEntity;import org.springframework.security.authentication.AuthenticationManager;import org.springframework.security.authentication.BadCredentialsException;import org.springframework.security.authentication.DisabledException;import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;import org.springframework.security.core.Authentication;import org.springframework.security.core.userdetails.UserDetails;import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;import java.util.Map;@RestController@RequestMapping("/auth")public class AuthenticationController {    protected final Log logger = LogFactory.getLog(getClass());    final UserRepository userRepository;    final AuthenticationManager authenticationManager;    final JwtUserDetailsService userDetailsService;    final JwtTokenUtil jwtTokenUtil;    public AuthenticationController(UserRepository userRepository, AuthenticationManager authenticationManager,                                    JwtUserDetailsService userDetailsService, JwtTokenUtil jwtTokenUtil) {        this.userRepository = userRepository;        this.authenticationManager = authenticationManager;        this.userDetailsService = userDetailsService;        this.jwtTokenUtil = jwtTokenUtil;    }    @PostMapping("/login")    public ResponseEntity<?> loginUser(@RequestParam("user_name") String username,                                       @RequestParam("password") String password) {        Map<String, Object> responseMap = new HashMap<>();        try {            Authentication auth = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username                    , password));            if (auth.isAuthenticated()) {                logger.info("Logged In");                UserDetails userDetails = userDetailsService.loadUserByUsername(username);                String token = jwtTokenUtil.generateToken(userDetails);                responseMap.put("error", false);                responseMap.put("message", "Logged In");                responseMap.put("token", token);                return ResponseEntity.ok(responseMap);            } else {                responseMap.put("error", true);                responseMap.put("message", "Invalid Credentials");                return ResponseEntity.status(401).body(responseMap);            }        } catch (DisabledException e) {            e.printStackTrace();            responseMap.put("error", true);            responseMap.put("message", "User is disabled");            return ResponseEntity.status(500).body(responseMap);        } catch (BadCredentialsException e) {            responseMap.put("error", true);            responseMap.put("message", "Invalid Credentials");            return ResponseEntity.status(401).body(responseMap);        } catch (Exception e) {            e.printStackTrace();            responseMap.put("error", true);            responseMap.put("message", "Something went wrong");            return ResponseEntity.status(500).body(responseMap);        }    }    @PostMapping("/register")    public ResponseEntity<?> saveUser(@RequestParam("first_name") String firstName,                                      @RequestParam("last_name") String lastName,                                      @RequestParam("user_name") String userName, @RequestParam("email") String email            , @RequestParam("password") String password) {        Map<String, Object> responseMap = new HashMap<>();        User user = new User();        user.setFirstName(firstName);        user.setLastName(lastName);        user.setEmail(email);        user.setPassword(new BCryptPasswordEncoder().encode(password));        user.setRole("USER");        user.setUserName(userName);        UserDetails userDetails = userDetailsService.loadUserByUsername(userName);        String token = jwtTokenUtil.generateToken(userDetails);        userRepository.save(user);        responseMap.put("error", false);        responseMap.put("username", userName);        responseMap.put("message", "Account created successfully");        responseMap.put("token", token);        return ResponseEntity.ok(responseMap);    }}

Example of response to our register request:

image.png

You can save this token from response in local storage of your client (Reactive web or Mobile app) and use this token later in protected routes of your API. If we provide invalid credentials to our login request we will get a response with error code 401:

image.png

Now its time to actually use our JWT token to identify user associated to a HTTP request. Following code snippet will help you get the authenticated user anywhere in your project:

Authentication authentication = SecurityContextHolder                .getContext().getAuthentication();String username = authentication.getName();

For testing we will define UserController.java. Here you can get the user we added earlier during request filter in SecurityContextHolder.

package com.example.api.controller;import org.springframework.security.core.Authentication;import org.springframework.security.core.context.SecurityContextHolder;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;import java.util.Map;@RestController@RequestMapping("/user")public class UserController {    @GetMapping    public Map<String, Object> getUserName() {        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();        Map<String, Object> userMap = new HashMap<>();        userMap.put("username", authentication.getName());        userMap.put("error", false);        return userMap;    }}

When we will send the newly created JWT token in Authorization header we will get a proper response as follows:

image.png

11. Conclusion

We saw how you can implement token based authentication for REST API and various amazing frameworks to make life easier.

Complete code for this tutorial is commited in my Github repository. Don't forget to hit the star button :p

Thank you for reading this post, please give your valuable feedback in comments section.


Original Link: https://dev.to/prafful/spring-boot-rest-api-authentication-best-practices-using-jwt-2022-3j2d

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To