Basic Authentication Application(JWT and Role based Authentication) — Spring Boot, MySQL


  1. Basic Spring boot knowledge
  2. MySQL
  3. Hibernate
  4. IntelliJ

Lets proceed with EXAMPLE

Let’s say Spanish football league LaLiga wants to give user a streaming platform which will stream all the matches on their website. They want to give users functionality to chat as they want to collect data when the open stadiums to people they can convey fans about it.

LaLiga already has made a streaming platform and chat window, but now they want only registered users to chat, but unregistered user can stream the match. They also want to make some people moderators, who can perform some actions like block a user, change title and they want some users who could start the stream.

Now, LaLiga admin gives you a contract to make a service on top of the their streaming platform so that only authenticated users can perform some actions.

Let’s Get Started

In the Fig 1. we see two concepts Authentications and Authorizations. First we talk about authentication, it refers to the process of validating the user, this will tell if the user exists with our system or not. On the other hand, authorization is process to validate if the existed user has the permission to make changes that he has requested for.

Authentication with JWT

There are many authentication mechanism by which we can identify the user, JWT(Json Web Token) works best when we want to authenticate user without compromising their credentials.

Checkout more at


We will use role based authorization to check if the request coming from user is authorised to perform the action or not.

Jumping To Code

Project Structure


  1. UserEntity — id, userName(unique), email(unique), password, set of roles
  2. Role — id, name

A mapping table(user_roles) will be made to store all the user(id) mapped to role(id).


  1. We will add to add a filter to requests that we want JWT authentication, internally the code will be overridden by doFilterInternal code.
public class AuthTokenFilter extends OncePerRequestFilter {
private JwtUtils jwtUtils;

private UserDetailsServiceImpl userDetailsService;

private static final Logger logger = LoggerFactory.getLogger(AuthTokenFilter.class);

protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
try {
String jwt = parseJwt(request);
if (jwt != null && jwtUtils.validateJwtToken(jwt)) {
String username = jwtUtils.getUserNameFromJwtToken(jwt);

UserDetails userDetails = userDetailsService.loadUserByUsername(username);
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
userDetails, null, userDetails.getAuthorities());
authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));

} catch (Exception e) {
logger.error("Cannot set user authentication: ", e);

filterChain.doFilter(request, response);

private String parseJwt(HttpServletRequest request) {
String headerAuth = request.getHeader("Authorization");

if (StringUtils.hasText(headerAuth) && headerAuth.startsWith("Bearer ")) {
return headerAuth.substring(7);

return null;

a. From token we are getting username and password(encrypted by key).
b. Then by that we make UserDetails which is then user to make authentication token to pass to security context for verification.

2. We will add JwtUtils which will contain some helper functions(comments mention below the use of the imp statements)

public class JwtUtils{
private static final Logger logger = LoggerFactory.getLogger(JwtUtils.class);
// this is hashing key(keep it secret)
private final String jwtSecret = "mySecret";
// timeout of token, the token will not work after this much millis
private final int jwtExpirationMs = 1000 * 60 * 60 * 10;

public String generateJwtToken(Authentication authentication) {

UserDetailsImpl userPrincipal = (UserDetailsImpl) authentication.getPrincipal();
// building and returning the processed jwt token with username and // password
return Jwts.builder()
.setIssuedAt(new Date())
.setExpiration(new Date((new Date()).getTime() + jwtExpirationMs))
.signWith(SignatureAlgorithm.HS512, jwtSecret)

public String getUserNameFromJwtToken(String token) {
return Jwts.parser().setSigningKey(jwtSecret).parseClaimsJws(token).getBody().getSubject();

public boolean validateJwtToken(String authToken) {
try {
return true;
} catch (SignatureException e) {
logger.error("Invalid JWT signature: {}", e.getMessage());
} catch (MalformedJwtException e) {
logger.error("Invalid JWT token: {}", e.getMessage());
} catch (ExpiredJwtException e) {
logger.error("JWT token is expired: {}", e.getMessage());
} catch (UnsupportedJwtException e) {
logger.error("JWT token is unsupported: {}", e.getMessage());
} catch (IllegalArgumentException e) {
logger.error("JWT claims string is empty: {}", e.getMessage());

return false;

3. Making which will be implementation of UserDetails which contains the info about the user. We will just add a function to make UserDetails from UserEntity.

public static UserDetailsImpl build(UserEntity user) {
List<GrantedAuthority> authorities = user.getRoles().stream()
.map(role -> new SimpleGrantedAuthority(role.getName().name()))

return new UserDetailsImpl(

4. We have to override the loadUserByUsername function from spring boot UserDetailsService. So, we make the which will get the user details from our database.

public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
UserEntity user = userRepository.findFirstByUserName(username)
.orElseThrow(() -> new UsernameNotFoundException("User Not Found with username: " + username));


5. Now we have to configure WebSecurityConfig which is to add all the filters and encoder we made.

prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
UserDetailsServiceImpl userDetailsService;

private AuthEntryPointJwt unauthorizedHandler;

public AuthTokenFilter authenticationJwtTokenFilter() {
return new AuthTokenFilter();
// password encoder will be required in spring to run the
// authentication
public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {

public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();

public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
// /v1/auth contains login and signup and jwt doesn't make sense on // them and /api/test/** will contain our apis which requires jwt
protected void configure(HttpSecurity http) throws Exception {
// for each api the filter has been added with above http request
// conditions
http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);

6. Now create a login and signup apis with requirements needed(code available in github repository) and configure mysql accordingly.

#For roles please run these commands

Hope you like the article, thank you very much for reading this.




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


Software Development Engineer- Glance(Inmobi)