Skip to main content

Posts

How to add custom user attributes in keycloak and access them in spring boot application

Sometime it may be possible you want to add more parameters to standard registration page of keyloak for your users and aaccess that data in your spring boot application. This artical will show step by steps on how to add such extra attributes. What is Keycloak Keycloak is an open source software product to allow single sign-on with Identity Management and Access Management aimed at modern applications and services, to learn more visit  h ttps://www.keycloak.org/ What is Spring boot Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run". To learn more visit  https://spring.io/projects/spring-boot To add an extra attribute in keyclkoak server you will need to edit actuall html template and then registaer new attribute in json response so that it will be available on client. Edit HTML template Lets assume we want to add mobile number on default registration page. Go to Keyc

How to create Spring boot cloud config server

What is Spring boot cloud config server? Spring Cloud Config Server provides an HTTP resource-based API for external configuration (name-value pairs or equivalent YAML content). The server is embeddable in a Spring Boot application, by using the  @EnableConfigServer  annotation. Consequently, the following application is a config server In Simple terms, Spring boot cloud config server takes your application.yml or application.properties from your spring boot application and serve it over HTTP, and spring application which need to use it just need to delete the application.yml or application.properties and create one bootstrap.yml/properties and defined 2 simple properties  spring.application.name  and  spring.cloud.config.uri Create New java project using one of following instructions. Create project with Java, maven and Intellij Edit POM.xml, add following content in pom.xml First specify the packaging jar also add parent of this pom to be N

How to create java maven project in intelij

Open intellij Create a new java maven project in intellij . Select Maven type, Select JDK you want to use and click Next. Enter GroupId and ArtifactId, click Next Select project Location and click finish Intelij will display a warning, just press Ok. Once project is created , a popup may appear asking for auto import, select "Enable Auto Import" You will have a project created, looking something like this Note: I have created these instructions using, Instruction for other OS or intellij Version shouldn't be much different, if you need instruction for other version leave a comment and i will try to come up with another set of instructions. Intellij 2017.2.1 Java 1.8 Mac OS

Convert Java object to/from json using Gson (Library from Google)

In this article I am going to explain how you can use Gson library to convert Java Object to/from Json. com.google.code.gson gson 2.8.0 compile Test Bean class public class Person{ private long id; private String name; private int age; } Convert Java Object to Json Service Person person = new person(); person.setName("John Doe"); person.setAge(21); Gson gson = new Gson(); String jsonString = gson.toJson(person); System.out.println(jsonString); Converting Json to Java Object [Plain Object] String jsonString = "{id:1, name:'Ravi Sharma'}" Gson gson = new Gson(); Person person = gson.fromJson(jsonString, Person.class);

Few tips to write low latency programs

People ask me can you write Low latency Java programs , and i always feel low latency is a relative terms and based on lots of things, not just my java code. But then i thought as a programmer i should think how my code could use my underlying resources in better way and make JVM performance better. (My JVM or java code cant make harddisk run faster but i can write programs so that i can access hardisk less :) ) Few tips which i think we should keep in mind while writing code which should have low latency Initialise/create Collection objects as actual size as near as possible. If you can guess actual size of collection at run time, then its always better to create collections of that particular size. e.g. if i know my list will have 100 items(approx) and those will be added at once, then its always good to create list like this List<Object> list = new ArrayList<Object>(100); instead of List<Object> list = new ArrayList<Object>(); and for Hash b

Validate HTML as XHTML in Intelij

When working in intelij with HTML, sometimes we need to make sure our HTML is XHTML compliance, no un ened tags etc. i.e. we should not have <img src="myimage.jpg> If You put above code in an HTML and run it in browser it will work fine, but if you are using some server side framework like Thymeleaf with Spring, it will generate error saying <img> tag must be terminated. By Default intelij will not complaint about this and when you are writing HTML you may ignore the error and will get the error when you actually run it with in server. Or worst some HTML developer is writing the code HTML code for you and then you will have to fix all these errors. Intelij can help you to make sure you write a valid HTML(XHTML). You can enable XHTML validation for HTML files by following below steps. Go to Settings(Windows)/Preference(mac) in Intelij. Search for "File Types" Find HTML as shown in image below. You will see HTML has some extension associated with

Understanding Equals and hashCode contract,and what can go wrong

SO what about Equals and hashcode? Most people will say these are the two basic function available in Object class and could be called from any Class object created in Java. Well thats true but there is more then this. First take a look at HashCode: Every JVM provides a good hashCode implementation mostly based on the object's memory location and for two different object(by location) it produce different hashCode. And at the same time Equal method also consider two different memory location object as Not Equal. So at core two are in a contract and works great. But as soon as start writing code and write a class and create a state of a class(means create few data variables in a class), the core contract of hashCode and Equal doesnt work any more. e.g. Class MyFirstCLass{ int value; public MyFirstCLass(int value){ this.value = value; } } MyFistCLass obj1 = new MyFirstCLass(10); MyFistCLass obj2 = new MyFirstCLass(10); Now obj1 and obj2, theoratically should be equal a