Category: Spring boot complete tutorial with example

Home / Category: Spring boot complete tutorial with example

As we know we use Eureka server to monitor the status of our microservices. Eureka server provides a dashboard on which we can see every microservice status and other details like port no on which microservice running and how many instance of microservice are running. But if you see we can easily see dashbord by hitting the URL of Eureak and it is open to all if anybody get your Eureka server URL, they will see all the details about your project.

So to protect our Eureka server we have to define some sort of security so only authorized person can see the server details. In this post we will see how to use HTTP basic security in Eureka Server. so to do this we have to follow below steps:

  1. Add required dependency in your pom.xml file of Eureka Server.
  2. Allow basic security property in your application.properties file with username and password.
  3. Enable security using @EnableWebSecurity annotation in your main class.
  4. Define a configuration class that extends WebSecurityConfigurerAdapter class and overrides configure methos.

Follow below steps:

  1. Add required dependency in your pom.xml file of Eureka Server.
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-security</artifactId>
</dependency>

<dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

2- Allow basic security in your application.properties file.

spring.security.basic.enabled=true
spring.security.user.name=vasu
spring.security.user.password=rajput


spring.application.name=netflix-eureka-server
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false



3- Enable security using @EnableWebSecurity annotation in your main class.

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;

@EnableEurekaServer
@EnableWebSecurity
@SpringBootApplication
public class SecureEurekaServerApplication {

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

}

4- Define a configuration class that extends WebSecurityConfigurerAdapter class and overrides configure methos.

package com.example.demo;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http.csrf().disable().authorizeRequests().anyRequest().authenticated().and().httpBasic();
	}

}

Now Run your application and Hit the Eurka Server URL you will get a popup, Now enter username and password if credentials are valid you will be login otherwise you are not able to login on Eureka server.

Now we have to register our Eureka Client to Eureka server. We know how to connect Eureka client with Eureka Server we use

eureka.client.service-url.default-zone=http://localhost:8761/eureka

in application.properties file of our Eureka client. Now if you use the same configuration you will get error that no server found to connect with so you have to change the above property as

eureka.client.serviceUrl.defaultZone=http://vasu:rajput@localhost:8761/eureka

Now your Eureka client application.properties file look like below

spring.application.name=secureEurekaClient
server.port=9091
eureka.client.serviceUrl.defaultZone=http://vasu:rajput@localhost:8761/eureka



In the above URL http://vasu:rajput@localhost:8761/eureka vasu is username and rajput is password.

Now run your Eureka client and it will successfully register to Eureka Server.

Complete Code on GitHub : Eureka Server Security

Help Others, Please Share

In this article we see about Spring Cloud Security. As we know security is the first concern when we work on any project, we don’t want to compromise with user credentials so we have to more concern about security.

JWT ( Json Web Token ) is one of the feature Spring Cloud Security. Any API can be accessed only if user has valid signed token otherwise he will not able to access that APIs.

JWT ( Json Web Token ) consist of three parts. And Structure will be like ( Header.Payload.Secret ). For More Go to ( visit jwt.io )

  1. Header ( Consist of typ that always be JWT and a Hashing algo )
  2. Payload ( Consist of claims or Information )
  3. Signature ( Secret Key that you provide to signed token )

We can use JWT in Spring Boot using below steps:

  1. Add required dependency in your pom.xml file.
  2. Add MySQL configuration in your application.properties file.
  3. Create a class where you define some constatnts like JWT Secret and expire time for JWT token.
  4. Create a utility Class where we can define claim, generate or validate JWT tokens for user.
  5. Create a DAO class where you can perform MySQL activity.
  6. Create a class that implements UserDetailsService and perform user authentication by getting userName and password of user.
  7. create a controller class where we can provide endPoint for getting our Token if authentication successful.
  8. Create Two Model class one is for getting userName and password and other is for JWT Response.
  9. Create a Filter class that perform JWT validation on each request.
  10. create a class that implements AuthenticationEntryPoint for reject every unauthenticated request and Send 401 Error Code for unauthorised user.
  11. Create a class that extends WebSecurityConfigurerAdapter and provide security configuration for endPoint that we want to secure.
  12. After This we will run our application and make a Post request with username and password to authenticate API that we define in controller to get JWT Token.
  13. Now add this Token to your Header with Bearer keyword to get response from Secured APIs. You will get response only if you pass a valid Token.

Follow below steps:

1- Add required dependency in your pom.xml file.

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-security</artifactId>
</dependency>

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
 <groupId>io.jsonwebtoken</groupId>
 <artifactId>jjwt</artifactId>
 <version>0.9.1</version>
</dependency>

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-devtools</artifactId>
 <scope>runtime</scope>
 <optional>true</optional>
</dependency>

<dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 <scope>runtime</scope>
</dependency>

2- Add MySQL configuration in your application.properties file.

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/springBootSecurityMySQL
spring.datasource.username=vasu
spring.datasource.password=12345@
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

3- Create a class where you define some constatnts like JWT Secret and expire time for JWT token.

package com.javadream.constant;

public class JwtConstatnt {

	public static final String JWT_SECRET = "123@@#&amp;javadream.in#$%";
	public static final long JWT_TOKEN_VALIDITY = 5 * 60 * 60;
}

4- Create a utility Class where we can define claim, generate or validate JWT tokens for user.

package com.javadream.utility;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.userdetails.UserDetails;

import com.javadream.constant.JwtConstatnt;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

@Configuration
public class JwtTokenUtility implements Serializable{

	private static final long serialVersionUID = -9172337479697036292L;
	private static final Logger logger = LoggerFactory.getLogger(JwtTokenUtility.class);
	
	private static final String JWT_SECRET = JwtConstatnt.JWT_SECRET;
	private static final long JWT_TOKEN_VALIDITY = JwtConstatnt.JWT_TOKEN_VALIDITY;
	
	     //get username from jwt token
		public String getUsernameFromToken(String token) {
			return getClaimFromToken(token, Claims::getSubject);
		}

		public <T> T getClaimFromToken(String token, Function<Claims, T> claimsResolver) {
			final Claims claims = getAllClaimsFromToken(token);
			return claimsResolver.apply(claims);
		}
		
		//for get any information from token we will need the secret key
		private Claims getAllClaimsFromToken(String token) {
			return Jwts.parser().setSigningKey(JWT_SECRET).parseClaimsJws(token).getBody();
		}
		
		//check if the token has expired
		private Boolean isTokenExpired(String token) {
			final Date expiration = getExpirationDateFromToken(token);
			return expiration.before(new Date());
		}
		
		//retrieve expiration date from jwt token
		public Date getExpirationDateFromToken(String token) {
			return getClaimFromToken(token, Claims::getExpiration);
		}
		
		//generate token for user
		public String generateToken(UserDetails userDetails) {
			Map<String, Object> claims = new HashMap<>();
			return doGenerateToken(claims, userDetails.getUsername());
		}
		

		private String doGenerateToken(Map<String, Object> claims, String subject) {
			return Jwts.builder().setClaims(claims).setSubject(subject).setIssuedAt(new Date(System.currentTimeMillis()))
					.setExpiration(new Date(System.currentTimeMillis() + JWT_TOKEN_VALIDITY * 1000))
					.signWith(SignatureAlgorithm.HS512, JWT_SECRET).compact();
		}
		
		//validate token
		public Boolean validateToken(String token, UserDetails userDetails) {
			final String username = getUsernameFromToken(token);
			return (username.equals(userDetails.getUsername()) &amp;&amp; !isTokenExpired(token));
		}
}

5- Create a DAO class where you can perform MySQL activity.

package com.javadream.dao;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Repository;

import com.javadream.model.UserBean;

@Repository
public class UserDAO {
	
	private static final Logger logger = LoggerFactory.getLogger(UserDAO.class);
	
	@Autowired
    private PasswordEncoder encoder;

	@Autowired
	private JdbcTemplate template;
	
	public UserBean getUserInfo(String userName) {
		try {
			String query = "select * from userDetail where userName=?";
			List<Map<String, Object>> userResponse = template.queryForList(query, new Object[] {userName});
			logger.info("vvv::  userResponse= "+userResponse);
			if(userName !=null) {
				UserBean userBean = new UserBean(userResponse.get(0).get("username").toString(),
						userResponse.get(0).get("password").toString());
				return userBean;
			}
		} catch (Exception e) {
			logger.error(e.getStackTrace().toString());
		}
		return null;
	}
	
	   public int insertUser(UserBean user) {
	        try {
	            String userName = user.getUserName();
	            String pass = user.getPassword();

	            logger.info("vvv::  userName= "+userName + " ,pass= "+pass );
	            int updateStatus = template.update("insert into userDetail(username,password) values(?,?)", new Object[] {userName,encoder.encode(pass)});
	            logger.info("vvv::  updateStatus= "+updateStatus);
	            if(updateStatus == 1) {
	             return 1;
	            }
	            return 0;
	        } catch (Exception e) {
	            e.printStackTrace();
	            return 0;
	        }
	    }
}

6- Create a class that implements UserDetailsService and perform user authentication by getting userName and password of user.

package com.javadream.service;

import java.util.ArrayList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;

import com.javadream.dao.UserDAO;
import com.javadream.model.UserBean;

@Service
public class JwtUserDetailService implements UserDetailsService {

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

	@Autowired
	private UserDAO dao;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		try {
			UserBean userInfo = dao.getUserInfo(username);
			logger.info("vvv::  userInfo= " + userInfo);
			if (userInfo != null) {
				UserDetails userDetails = (UserDetails) new org.springframework.security.core.userdetails.User(
						userInfo.getUserName(), userInfo.getPassword(), new ArrayList<>());
				return userDetails;
			} else {
				throw new UsernameNotFoundException("User not found with username: " + username);
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return null;
	}

}

7- create a controller class where we can provide endPoint for getting our Token if authentication successful.

package com.javadream.controller;

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.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.javadream.dao.UserDAO;
import com.javadream.model.JwtResponse;
import com.javadream.model.UserBean;
import com.javadream.service.JwtUserDetailService;
import com.javadream.utility.JwtTokenUtility;

@RestController
public class JwtAuthenticationController {

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

	@Autowired
	private AuthenticationManager authenticationManager;

	@Autowired
	private JwtTokenUtility jwtTokenUtil;

	@Autowired
	private JwtUserDetailService userDetailService;
	
	@Autowired
	private UserDAO dao;

	@PostMapping("/authenticateAPI")
	public ResponseEntity<?> createAuthenticationToken(@RequestBody UserBean authenticationRequest) {
		try {
			logger.info("vvv::  Loading createAuthenticationToken for UserBean= " + authenticationRequest);
			authenticateUser(authenticationRequest.getUserName(), authenticationRequest.getPassword());
			final UserDetails userDetails = userDetailService.loadUserByUsername(authenticationRequest.getUserName());

			final String token = jwtTokenUtil.generateToken(userDetails);

			return ResponseEntity.ok(new JwtResponse(token));
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
	}
	
	@PostMapping("/insert")
    public String insert(@RequestBody UserBean user) {
        try {
            logger.info("vvv::  user= "+user);
            if(user == null) {
                return "Some technical error try again";
            }
            int insertUser = dao.insertUser(user);
            return "Insert Status= "+insertUser;
        } catch (Exception e) {
            e.printStackTrace();
            return "Some Technical Error Try Again";
        }
         
    }

	private void authenticateUser(String username, String password) throws Exception {
		try {
			authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
		} catch (DisabledException e) {
			throw new Exception("USER_DISABLED", e);
		} catch (BadCredentialsException e) {
			throw new Exception("INVALID_CREDENTIALS", e);
		}
	}
}

Create Another Controller Whose APIs are secured.

package com.javadream.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SecureController {

	
	@GetMapping("/SecureAPI")
	public String getSecureEndPoint() {
		return "Nice try! You have learned Spring Boot JWT Security";
	}
}

8- Create Two Model class one is for getting userName and password and other is for JWT Response.

package com.javadream.model;

public class UserBean {

	private String userName;
	private String password;

	public UserBean() {
		super();
	}

	public UserBean(String userName, String password) {
		super();
		this.userName = userName;
		this.password = password;
	}

	@Override
	public String toString() {
		return "UserBean [userName=" + userName + ", password=" + password + "]";
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

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

}

package com.javadream.model;

import java.io.Serializable;

public class JwtResponse implements Serializable {

	private static final long serialVersionUID = 2624374270511956111L;

	private final String jwttoken;

	public JwtResponse(String jwttoken) {
		this.jwttoken = jwttoken;
	}

	public String getToken() {
		return this.jwttoken;
	}
}

9- Create a Filter class that perform JWT validation on each request.

package com.javadream.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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 com.javadream.service.JwtUserDetailService;

import com.javadream.utility.JwtTokenUtility;

import io.jsonwebtoken.ExpiredJwtException;

@Component
public class JwtFilter extends OncePerRequestFilter {
	private static final Logger logger = LoggerFactory.getLogger(JwtFilter.class);

	@Autowired
	private JwtTokenUtility jwtTokenUtil;

	@Autowired
	private JwtUserDetailService jwtUserDetailsService;

	@Override
	protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
			throws ServletException, IOException {
		final String requestTokenHeader = request.getHeader("Authorization");

		String username = null;
		String jwtToken = null;

		if (requestTokenHeader != null &amp;&amp; requestTokenHeader.startsWith("Bearer ")) {
			jwtToken = requestTokenHeader.substring(7);
			try {
				username = jwtTokenUtil.getUsernameFromToken(jwtToken);
			} catch (IllegalArgumentException e) {
				System.out.println("Unable to get JWT Token for this user");
			} catch (ExpiredJwtException e) {
				System.out.println("JWT Token has been expired");
			}
		} else {
			logger.warn("JWT Token do not start with Bearer keyword");
		}

		// Once we get the token validate it.
		if (username != null &amp;&amp; SecurityContextHolder.getContext().getAuthentication() == null) {

			UserDetails userDetails = this.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);
			}
		}
		filterChain.doFilter(request, response);

	}

}

10- create a class that implements AuthenticationEntryPoint for reject every unauthenticated request and Send 401 Error Code for unauthorised user.

package com.javadream.service;

import java.io.IOException;
import java.io.Serializable;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

@Component
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint, Serializable {

	@Override
	public void commence(HttpServletRequest request, HttpServletResponse response,
			AuthenticationException authException) throws IOException, ServletException {
		response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized");

	}

}

11- Create a class that extends WebSecurityConfigurerAdapter and provide security configuration for endPoint that we want to secure.

package com.javadream.config;

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.web.builders.HttpSecurity;
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.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import com.javadream.filter.JwtFilter;
import com.javadream.service.JwtAuthenticationEntryPoint;

@Configuration
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter{

	@Autowired
	private JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;

	@Autowired
	private UserDetailsService jwtUserDetailsService;

	@Autowired
	private JwtFilter jwtRequestFilter;

	@Bean
	public PasswordEncoder passwordEncoder() {
		return new BCryptPasswordEncoder();
	}
	
	@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		auth.userDetailsService(jwtUserDetailsService).passwordEncoder(passwordEncoder());
	}

	@Bean
	@Override
	public AuthenticationManager authenticationManagerBean() throws Exception {
		return super.authenticationManagerBean();
	}

	@Override
	protected void configure(HttpSecurity httpSecurity) throws Exception {

		httpSecurity.csrf().disable()
				.authorizeRequests().antMatchers("/authenticateAPI","/insert").permitAll().
				
				anyRequest().authenticated().and().
				exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint)
				.and().sessionManagement()
				.sessionCreationPolicy(SessionCreationPolicy.STATELESS);

		httpSecurity.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
	}
}

Now our application has been ready to run, Just run your application and first create a user ( SignUp ). we do not define any security on signup API because everybody allow to do SignUp but rest endPoint are secured. So now make a SignUp request using Postman.

Complete Code on GitHub : SpringBoot Security JWT

Spring Cloud Security

12- After This we will run our application and make a Post request with username and password to authenticate API that we define in controller to get JWT Token.

Spring Cloud Security

13- Now add this Token to your Header with Bearer keyword to get response from Secured APIs. You will get response only if you pass a valid Token.

Spring Cloud Security

You may also like

Spring Boot Security With MySql

spring batch example

Spring Boot with Apache kafka.

File Upload in Spring Boot

HTTPS in Spring Boot

Help Others, Please Share

As we know security is the first concern when we working on any web application. Spring Boot also provide this feature to prevent our user from unauthorised access. Some time we also want Role wise authentication in our application means we want a user to access API only if he will authorised to use that. In this post we will see how we can manage a complete role wise authentication using MySQL in Spring boot, So to do that we have to follow below steps:

  1. Add required dependency in your pom.xml file.
  2. Add MySQL configuration in your application.properties file
  3. create user and authorities table in your MySQL.
  4. Enable security in your main class using @EnableWebSecurity annotaion.
  5. Create a Model class User.
  6. Create a configuration class that extends WebSecurityConfigurerAdapter and specify security config in this class.
  7. Create a DAO Class that Perform MySQL activity.
  8. Create a class that implements UserDetailsService and perform Spring Boot Security validation.
  9. Create Controller class for Rest Endpoint .

Follow above steps:

1- Add below dependency in your pom.xml file.

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-security</artifactId>
</dependency>

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-devtools</artifactId>
 <scope>runtime</scope>
 <optional>true</optional>
</dependency>

<dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 <scope>runtime</scope>
</dependency>

2- Add MySQL configuration in your application.properties file.

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/springBootSecurityMySQL
spring.datasource.username=vasu
spring.datasource.password=12345@
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

3- Create user and authorities table in MySQL.

CREATE TABLE IF NOT EXISTS `user` (
  `username` varchar(50) NOT NULL,
  `password` varchar(150) NOT NULL,
  `enabled` tinyint(1) NOT NULL,
  PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;



CREATE TABLE IF NOT EXISTS `authorities` (
  `username` varchar(50) NOT NULL,
  `authority` varchar(50) NOT NULL,
  UNIQUE KEY `ix_auth_username` (`username`,`authority`),
  CONSTRAINT `fk_authorities_users` FOREIGN KEY (`username`) REFERENCES `user` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

4- Enable security in your main class using @EnableWebSecurity annotaion.

package com.javadream;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;

@EnableWebSecurity
@SpringBootApplication
public class SpringBootSecurityMySqlApplication {

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

}

5- Create a Model class User.

package com.javadream.model;

public class User {

	private String userName;
	private String password;
	private String role;

	public User() {
		super();
	}

	public User(String userName, String password, String role) {
		super();
		this.userName = userName;
		this.password = password;
		this.role = role;
	}

	@Override
	public String toString() {
		return "User [userName=" + userName + ", password=" + password + ", role=" + role + "]";
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

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

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

}

6- Create a configuration class that extends WebSecurityConfigurerAdapter

package com.javadream.config;

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.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import com.javadream.service.UserAuthenticationService;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

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

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

	@Autowired
	UserAuthenticationService authenticationService;

	@Autowired
	private PasswordEncoder encoder;

	@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {

		auth.userDetailsService(authenticationService).passwordEncoder(encoder);
	}

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http.authorizeRequests()
		.antMatchers("/signUp/**").permitAll()
		.antMatchers("/userRoleAPIs/**")
		.hasAnyRole("USER")
		.antMatchers("/adminRoleAPIs/**")
		.hasAnyRole("ADMIN")
		.antMatchers("/anyRoleAPIs/**")
		.hasAnyRole("USER","ADMIN")
		.and().csrf().disable().formLogin();
	}

}

In above class we override configure method of WebSecurityConfigurerAdapter class. Inside this function we define the APIs end point and the role that can access that APIs. We do not define any type of security on SignUp endPoint because anybody can do Sign Up and we don’t required any authentication for Sing Up process.

7- Create a DAO Class that Perform MySQL activity .

package com.javadream.dao;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Repository;
import org.springframework.security.authentication.*;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.javadream.model.User;

@Repository
public class UserDao {
	private static final Logger logger = LoggerFactory.getLogger(UserDao.class);

	@Autowired
	private JdbcTemplate template;
	
	@Autowired
	private PasswordEncoder encoder;

	public User getUserInfo(String userName) {
		try {
			String sql = "SELECT u.username name, u.password pass, a.authority role FROM "
					+ "user u INNER JOIN authorities a on u.username=a.username WHERE "
					+ "u.enabled =1 and u.username = ?";
			List<Map<String, Object>> userResponse = template.queryForList(sql, new Object[] { userName });
			logger.info("vvv::  userResponse= " + userResponse);
			
			if (userResponse != null) {
				User user = new User(userResponse.get(0).get("name").toString(),
						userResponse.get(0).get("pass").toString(),
						userResponse.get(0).get("role").toString());
				
				logger.info("vvv:: user= " + user);
				return user;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;

	}
	
	
	public int insertUser(User user) {
		try {
			String userName = user.getUserName();
			String pass = user.getPassword();
			String role = user.getRole();
			logger.info("vvv::  userName= "+userName + " ,pass= "+pass + ", role= "+role);
			int updateStatus = template.update("insert into user(username,password,enabled) values(?,?,?)", new Object[] {userName,encoder.encode(pass),"1"});
			logger.info("vvv::  updateStatus= "+updateStatus);
			if(updateStatus == 1) {
				int insertIntoAuthTable = template.update("insert into authorities(username,authority) values(?,?)", new Object[] {userName,role});
                logger.info("vvv::  insertIntoAuthTable = "+insertIntoAuthTable);
                if(insertIntoAuthTable == 1) {
                	return 1;
                }
			}
			return 0;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

}

In above class we define a bean PasswordEncoder , because we don’t want to save our user password in Plain Text in MySQL table so we use PasswordEncoder to encrypt our password before save it to MySQL.

8- Create a class that implements UserDetailsService and perform Spring Boot Security validation.

package com.javadream.service;

import java.util.Arrays;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
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.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import com.javadream.dao.UserDao;
import com.javadream.model.User;

@Service
public class UserAuthenticationService implements UserDetailsService {

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

	@Autowired
	private UserDao dao;

	@Autowired
	private BCryptPasswordEncoder encoder;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		try {
			logger.info("vvv::  calling loadUserByUsername for username= " + username);
			User userInfo = dao.getUserInfo(username);
			GrantedAuthority authority = new SimpleGrantedAuthority(userInfo.getRole());
			UserDetails userDetails = (UserDetails) new org.springframework.security.core.userdetails.User(
					userInfo.getUserName(), userInfo.getPassword(), Arrays.asList(authority));

			return userDetails;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
	}

}

9- Create Controller Class for Rest EndPoint.

Here we define three controller to demonstrate how APIs will be access with valid role.

First We create a SignUp controller to Sign Up. APIs for Sign up is open to all any body can access that.

package com.javadream.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
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;

import com.javadream.dao.UserDao;
import com.javadream.model.User;

@RestController
@RequestMapping("signUp")
public class InsertController {
	private static final Logger logger = LoggerFactory.getLogger(InsertController.class);
	private static final String GLOBAL_ERROR = "Some technical Error try again!";

	@Autowired
	private UserDao dao;
	
	
	@PostMapping("/insert")
	public String insert(@RequestBody User user) {
		try {
			logger.info("vvv::  user= "+user);
			if(user == null) {
				return GLOBAL_ERROR;
			}
			int insertUser = dao.insertUser(user);
			return "Insert Status= "+insertUser;
		} catch (Exception e) {
			e.printStackTrace();
			return "Some Technical Error Try Again";
		}
		
	}
}

Second We create a UserController. APIs for this controller is accessed by those who have USER role.

package com.javadream.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.javadream.dao.UserDao;
import com.javadream.model.User;

@RestController
@RequestMapping("userRoleAPIs")
public class UserController {

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

	
	@GetMapping("/testAPI")
	public String demo() {
		return "Nice Try! You have learned Spring Boot Security With MySql";
	}
}

Third we create a AdminController. APIs for this controller is accessed by those who have ADMIN role.

package com.javadream.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/adminRoleAPIs")
public class AdminController {

	@GetMapping("/testAPI")
	public String demo() {
		return "Nice Try! You have learned Spring Boot Security With MySql";
	}
}

Fourth we create a AnyRoleController. APIs for this controller is accessed by those who any role ADMIN role or USER role.

package com.javadream.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/anyRoleAPIs")
public class AnyRoleController {

	
	@GetMapping("/testAPI")
	public String demo() {
		return "Nice Try! You have learned Spring Boot Security With MySql";
	}
}

Now Run this application and use below steps to test your application functionality.

  1. Make a Post Request to Sign Up URL using postman .
  2. Define role parameter accordingly for Admin role use parameter ROLE_ADMIN and for user role use parameter ROLE_USER
  3. Now try to access any API from any controller ( UserController, AdminController, AnyRoleController).
  4. when you hit API a default SignUp Page will open now enter your credentials if your user has valid credential and valid role then you will get the response otherwise 403 error will display.

Complete Code On GitHub: SpringBoot Role Wise authentication with MySQL

Help Others, Please Share

We know that Spring boot micro services application is divided into modules or separate application. And in many cases it is necessary that we have to call the one application end point from another application. So to Call one application end point from another application Spring boot provided two choices.

  1. RestTemplae
  2. Feign Client

In this example we are using Feign Client to communicate between two applications.

So to working with Feign Client we have to create two applications with some end Point. You can follow my Create Application in Spring boot microservice Blog to see how to create application in Spring boot Microservices.

Suppose we have two differnet application hosted on Eureka Server with name CLIENT1 , CLIENT2.

Note: CLIENT1 and CLIENT2 are the name that you give to your application inside application.properties using.

spring.application.name=client1

Now Suppose we are Calling end point of CLIENT2 application from CLIENT1

So we will perform some steps in CLIENT1 application

1- Add below dependency in your pom.xml file

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

2- Enable Feign Client using @EnableFeignClients annotation in your main class.

@EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
public class EurekaClient1Application {

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

3- Create an Interface with @FeignClient and @RibbonClient annotation and give the name of application that display on your Eureka Server.

@FeignClient(name = "CLIENT2")
@RibbonClient(name = "CLIENT2")
public interface Client2proxy {

	@GetMapping("client2")
	public String proxyResponse();
}

In above Interface we are given name of CLIENT2 application and create a end point with same that we created in CLIENT2 application.

Note: In CLIENT2 application you must have an end point /client2 that return some string.

4- Now create a Controller class in CLIENT1 application and Auto-wired your proxy interface in that application

@RestController
public class ClientController1 {
	
	@Autowired
	private Client2proxy proxy;

	@GetMapping("/client1")
	public String client1() {
		return "I am client 1";
	}
	
	@GetMapping("/testProxy")
	public String testP() {
		String proxyResponse = proxy.proxyResponse();
		return proxyResponse;
	}
}

Now Hit below URL in your browser you will get the response from CLIENT2 application end point

http://localhost:8081/testProxy

Help Others, Please Share

As we know in microservice architecture we have many independent application and some time it is needed that one application communicates with other application to do some operation. So if we want to communicate between two microservice then spring cloud provide us two useful Component

  1. RestTemplate
  2. Feign Client

In This Post we will discuss how two microservice communicate between them using RestTemplate. Make Sure you have your Eureka Server Running and both the application is registered on that. To use RestTemplate in our application we have to follow below steps:

  1. Add required dependeny in your pom.xml file.
  2. Create Two MicroService project. In this we create two project one is MicroService-1 and another is MicroService-2.
  3. Give ApplicationName , Port and Eureka server path in your application.properties file.
  4. Create a Bean for RestTemplate because we can’t auto-wired RestTemlate directly. So we create a configuration class and initialize a Bean for RestTemplate.
  5. Autowired RestTemplate in your Service class to call Rest End Point of another application.

Follow above Steps:

1- Add below dependency in your pom.xml file.

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<dependency>
 <groupId>com.googlecode.json-simple</groupId>
 <artifactId>json-simple</artifactId>
 <version>1.1.1</version>
</dependency>

2- Now we are creating a project Name MicroService-1 and from here we call RestEndPoint of MicroService-2 application.

3- Add below property in your application.properties file.

spring.application.name=microservice-1
server.port=8081
eureka.client.service-url.default-zone=http://localhost:8761/eureka

4- Create a Bean for RestTemplate.

package com.javadream.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

@Configuration
public class Config {

	@Bean
	public RestTemplate template() {
		HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
		httpRequestFactory.setConnectionRequestTimeout(4000);
		httpRequestFactory.setConnectTimeout(4000);
		httpRequestFactory.setReadTimeout(4000);
		return new RestTemplate(httpRequestFactory);
	}
}

5- Now create a Controller class autowired RestTemplate inside that and call RestEndPoint of MicroService-2 application.

For Calling a Get Request of MicroService-2 we use below syntax:

String responseFromMS2 = restTemplate.getForObject("http://localhost:8082/student/showAllStudent",
					String.class);

For Calling a Post Request of MicroService-2 we use below syntax:

HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		JSONObject studentObj = new JSONObject();
		studentObj.put("rollNo", 199);
		studentObj.put("name", "New_Student");
		studentObj.put("email", "new.student@gmail.com");
		org.springframework.http.HttpEntity<String> s = new org.springframework.http.HttpEntity<String>(studentObj.toString(),headers);
		String postForObject = restTemplate.postForObject("http://localhost:8082/student/addStudent", s, String.class);

Complete Code For Calling Get and Post EndPoint of MicroService-2 application:

package com.javadream.controller;

import org.json.simple.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/restTemplateDemo")
public class RestTemplateDemoController {

	private static final Logger logger = LoggerFactory.getLogger(RestTemplateDemoController.class);
	private final String GLOBAL_ERROR = "Some technical Error Please try again after some time";

	@Autowired
	private RestTemplate restTemplate;

	@GetMapping("call_MS2_showAllStudent_endPoint")
	public String callMicroservice_2_ShowAllStudent() {
		try {
			String responseFromMS2 = restTemplate.getForObject("http://localhost:8082/student/showAllStudent",
					String.class);
			logger.info("vvv::  responseFromMS2= " + responseFromMS2);
			return responseFromMS2;
		} catch (Exception e) {
			e.printStackTrace();
			return GLOBAL_ERROR;
		}
	}
	
	@GetMapping("call_MS2_addStudent_endPoint")
	public String call_MS_2_addStudent_endPoint() {
		HttpHeaders headers = new HttpHeaders();
	      headers.setContentType(MediaType.APPLICATION_JSON);
		JSONObject studentObj = new JSONObject();
		studentObj.put("rollNo", 199);
		studentObj.put("name", "New_Student");
		studentObj.put("email", "new.student@gmail.com");
		org.springframework.http.HttpEntity<String> s = new org.springframework.http.HttpEntity<String>(studentObj.toString(),headers);
		String postForObject = restTemplate.postForObject("http://localhost:8082/student/addStudent", s, String.class);
		return postForObject;
	}
	
	
}

Complete Code on GitHub : RestTemplate in Spring Cloud

You may also like:

Spring boot security using JWT ( JSON WEB TOKENS ).

How to deploy spring boot application on docker 

Spring Boot with Apache kafka.

Swagger2 in Spring Boot

Create WAR of Spring Boot Project

Help Others, Please Share

In Last Post ( Eureka Server ) we have seen that how to create a Eureka Server. Now we learn how to register our microservices on Eureka Server, We register our microservice on Eureka because of we need a dashboard where we can check status of our all microservices. So for registering them on eureka we have to follow below steps.

  1. Create a project and add required dependency in your pom.xml file
  2. Enable your main class as a client using @EnableEurekaClient annotation.
  3. Define Eureka Server path in your application.properties file.

Follow above steps:

1- Add Below dependency in pom.xml

<dependency>			 
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>			 
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

2- Enable Eureka Client using @EnableEurekaClient

package com.javadream;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableEurekaClient
@SpringBootApplication
public class MicroService1Application {

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

}

3- Define below lines in your application.properties file

spring.application.name=microservice-1
server.port=8081
eureka.client.service-url.default-zone=http://localhost:8761/eureka

Now run this application and check your Eureka Server You will get a instance of MICROSERVICE-1 on port 8081.

NOTE: Make Sure before running your microservice-1 application your eureka server is already running otherwise it will throw exception that no server found.

Complete Code On GitHub : Register MicroService Source Code

Help Others, Please Share

We know that in Monolithic application complete Code is at one place, in MicroService we divide this big monolithic application in small small microservices.

Suppose we create a separate application named EurekaClient-1 with some rest end points and host this application on Eureka Server so we don’t need to worry about details of this application.

To create this separate client application names EurekaClient-1 we have to follow below steps:

1- Add below dependency in your pom.xml file

<dependency>			 
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
</dependency>
		
<dependency>			 
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

2- Enable Eureka client using @EnableEurekaClient in your main class.

@EnableEurekaClient
@SpringBootApplication
public class EurekaClient1Application {
   public static void main(String[] args) {
	SpringApplication.run(EurekaClient1Application.class, args);
	}
}

3- Give this application name, and a Port on which it will run, and path of the Eureka Server.

spring.application.name=client1
server.port=8081
eureka.client.service-url.default-zone=http://localhost:8761/eureka

4- Create Controller Class and make some end points.

@RestController
public class ClientController1 {

	@GetMapping("/client1")
	public String client1() {
		return "I am client 1";
	}

}

Run your application and see your eureka server you will see your application hosted there.

Note: Before running your application make sure your eureka server is running.

Now access below url for getting your end point response

http://localhost:8081/client1

Help Others, Please Share

Eureka Server is used to monitor all the application. With the help of Eureka Server we don’t need to worry about information like which application running on which port and how many instances of application are running, all this are take cares by Eureka Server.So to Create a Eureka Server we have to follow below steps:

  1. Add required dependency in you pom.xml file
  2. Enable Your main class as a Eureka Server using @EnableEurekaServer annotation.
  3. Define applicationName and portNo in application.properties file.
  4. By Default Eureka act as a client but now we want this application to work as Eureka Server so we make Eureka registry as false in application.properties file.

1- Add the Eureka Server dependency in your pom.xml file.

<dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

2- Enable Eureka Server by using @EnableEurekaServer annotation in your main class.

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
   public static void main(String[] args) {
	SpringApplication.run(EurekaServerApplication.class, args);
	}
}

3- By Default Eureka Server application running as a client so if we have want to act this application as a server then we have to add below properties in application.properties file

 spring.application.name=netflix-eureka-server
 server.port=8761
 eureka.client.register-with-eureka=false
 eureka.client.fetch-registry=false

In above application.properties file

server.port define the port on which Eureka Server running in our case it is running on 8761 port so to access your eureka server just hit below URL in your browser.

http://localhost:8761/

Help Others, Please Share

In this post we will see spring boot profiles. We know that our application go through many phases like development, testing, production. So for them we have to do configuration accordingly.

Spring boot provides profile feature for this. So Using spring boot profiles we can use different properties in different phases. Suppose i have three environment development, testing, production.

Now if we want that developer will use property defined for developers and tester will use properties defined for tester and in production our application use properties for production environment.

To see How to set profiles in spring boot we have to follow the below steps:

Project Structure:

How to Use Profile in Spring BOOT - JavaDream

1- Add below dependency in your pom.xml file

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
 </dependency>

 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
 </dependency>

 <dependency>			 
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
    <optional>true</optional>
 </dependency>

2- create application.properties for development and testing phase syntax would be like

application-{Your-Phase-Name}.properties

Eg: application-dev.properties      // this is for development phase
    application-test.properties     //this is for testing phase

application-dev.properties – In this property file we will define the property that will be used by the developer. Suppose i define only one property with key as name and value as development.

name=development environment 

application-test.properties – In this property file we will define the property that will be used by the tester. And here we define only one property same as in developer property with key as name and value as testing.

name=testing environment

3- application.properties – This is our main property file here we define which profile need to be activated. Suppose currently developer is working on the application so our application should fetch property from application-dev.properties file so for this we have to write below property in our application.properties file.

spring.profiles.active=dev

Above syntax define that our application-dev.properties is activated. Now suppose the application is in testing phase so now tester wants that his property will be fetched so for using testing profile we will use below property in our application.properties file

spring.profiles.active=test

Now Check this using code suppose i want to print the name property value that i have defined in both the dev and test property. Suppose first i want to print the name from dev property than in my main application.properties file i will use spring.profiles.active=dev

And when we have to print the name from test property than in my main application.properties file i will use spring.profiles.active=test

Now create a controller class and get the value from dev or test profiles.

4- Create a controller with a rest end point that return name property value for different phase.

@RestController
public class ProfileController {
	@Value("${name}")
	private String name;
	
	@GetMapping("/profile")
	public String testProfile() {
           return "Current Envirnment is "+ name;
	}
}

5- Now Run your application and hit the URL

http://localhost:8080/profile

OUTPUT:

Set active profile to dev in your application.properties file

Set active profile to test in your application.properties file

Profile in spring boot

Find Complete Code on GitHub- [ Spring Boot profile GithubUrl ]

You may also like.

Spring boot Actuator for production environment monitoring Example

How to use multiple databases with single spring boot project.

Help Others, Please Share

In this post we will see how to use log4j in java or spring boot . Logging is a most important feature that we use to detect if any error occurs or not .

Spring boot by default provides slf4j logging in spring boot. For Using slf4j logging we have to include only web starter dependency.

Logging plays a important role while we debug our application on server. using log4j in java is very important. So try to make as simple as possible so anybody can understand about the issue after seeing your logs.

In this post we will see how to use Apache Log4j in spring boot application. To use Apache Log4j we have to follow below steps.

1- Add below dependency in your pom.xml file or get this from maven Repositry to enable log4j in java
<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.16</version>
</dependency>
2- create a properties file with name log4j.properties in your project resource folder and add below lines .

For Linux Server:

log4j.rootCategory=INFO, A1

log4j.appender.A1=org.apache.log4j.FileAppender

log4j.appender.A1.File=/var/log/data.log

log4j.appender.A1.layout=org.apache.log4j.PatternLayout

log4j.appender.A1.layout.ConversionPattern=%d{DATE} %F|%L : %m%n

For Window Console or your Eclipse Console make below entry in your log4j.properties :

pattern=%d %5p [%24t] %32c{2}|%L - %m%n
log4j.rootLogger=DEBUG, Console, Appender1

# console is set to be a ConsoleAppender.
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=${pattern}
log4j.appender.Console.Threshold=info

# Define the file appender
log4j.appender.Appender1=org.apache.log4j.DailyRollingFileAppender
log4j.appender.Appender1.File=/var/log/R2faSupportNew.log

#log4j.appender.Appender1.File=${catalina.home}/logs/revesecure_api.log
log4j.appender.Appender1.DatePattern='.'yyyy-MM-dd

# Set the append to false, should not overwrite
log4j.appender.Appender1.Append=true

# Set the threshold to debug mode
log4j.appender.Appender1.Threshold=info

# Define the layout for file appender
log4j.appender.Appender1.layout=org.apache.log4j.PatternLayout

log4j.appender.Appender1.layout.conversionPattern=${pattern}
3- Now go to your class and initialize the Logger object like below
private static final Logger logger = Logger.getLogger(CalculateSMSCost.class.getName());
4- Now go to your java class and print your logs like below.
logger.info("Apache log4j logging Example");
Now run your application and see the logs on console.

You may also like:

File Upload in Spring Boot

Spring boot security using JWT ( JSON WEB TOKENS ).

Actuator in spring boot for monitoring application

Help Others, Please Share