Posts

WebSecurityConfig in spring boot 3.0.+

Image
From Spring Boot 2.7, WebSecurityConfigurerAdapter is deprecated.  The WebSecurityConfigurerAdapter class is marked as deprecated in Spring Security 5.4. This is because the class was considered too tightly coupled to the servlet API, making it difficult to support other web technologies such as reactive web applications. Instead, developers are encouraged to use the WebSecurityConfigurer interface, which provides a more flexible and scalable approach to configuring web security.  The recommended way to configure security for a web application in Spring Security 6 is to use a Bean that takes an HttpSecurity object as a parameter and returns a SecurityFilterChain object. InMemory basic authentication example: The Bean-based configuration in Spring Security 6 provides a more modern and flexible way to configure security for your application. It replaces the old approach of extending the WebSecurityConfigurerAdapter class, which was used in previous versions of Spring Security....

Aspect Oriented Programming to trace the requests and responses to a REST method in Spring Boot.

Image
  Aspect Oriented Programming ( AOP ) is one of the key feature of Spring framework. AOP is similar to OOPS concept where it breaks code into multiple reusable modules. AOP provides the capability to dynamically add module at runtime. This is like injecting complete module(logging, cahcing, etc ) in Spring framework dynamically. Logging, caching, security, monitoring, etc. are some of the examples cross cutting concern from AOP. At any point of time there modules can be added dynamically. Spring AOP has interceptors which can intercept application and its methods. This is to perform some extra action at the time of property initiation, method initialization or destroy. Let's try it to create a custom annotation to trace the requests and responses to a REST method in Spring Boot At first you we need to use spring-boot-starter-web and spring-boot-starter-aop dependencies to make it work. STEP1 : An interface with the annotation name @Traceable Now , in whichever method you add the...

Spring Cloud Contract

Image
Spring Cloud Contract  is a project that, simply put, helps us write Consumer-Driven Contracts (CDC). This ensures the contract between a Producer and a Consumer, in a distributed system – for both HTTP-based and message-based interactions. Producer – Server Side For our producer side, we'll need the spring-cloud-starter-contract-verifier dependency. Producer Side Setup Test stubs are in /src/test/resources/contracts/ package. When the build is run, the plugin automatically generates a test class named ContractVerifierTest that extends our BaseTestClass and puts it in /target/generated-test-sources/contracts/. The names of the test methods are derived from the prefix “validate_” concatenated with the names of our Groovy test stubs. For the above Groovy file, the generated method name will be “validate_find_provider_by_id()”. Consumer – Client Side The consumer side of our CDC consume stubs generated by the producer side through HTTP interaction to maintain the contract...

Jenkins docker image with docker inside

Image
  Jenkins  is an open source, Java-based automation server that offers an easy way to set up a continuous integration and continuous delivery (CI/CD) pipeline. Continuous Integration (CI) is the process of automating the build and testing of code every time a team member commits changes to version control. Continuous Delivery (CD) is the process to build, test, configure and deploy from a build to a production environment.    Jenkins docker image with docker inside Step 1/ Create Dockerfile extending the official Alpine image. Then get jenkins.war and docker from official sites. Step 2/ Jenkins admin user setup to set a default admin user and password, by creating a Groovy script file. Step 3/ Adding Plugins to auto install some selected plugins. Step 4/ Build Your Custom Jenkins Image Here  is a fully functional Jenkins server.

How can I make reflection work on JDK 16 and later?

Image
  Reflection is another form of meta-programming, and just as valid, as the type-based parameters that you see in most languages these days. Reflection is powerful and generic, and reflective programs are a high order of maintainability (when used correctly, of course) and more so than purely object-orientated or procedural programs.  Reflection is fantastic for building tools for developers. With the advent of Java 9  the approach  to encapsulate has changed and the scary-sounding message appeared: WARNING: An illegal reflective access operation has occurred   WARNING: Illegal reflective access by ReflBytecodeName (file:/Users/ben/projects/books/resources/) to method sun.invoke.util.BytecodeName.parseBytecodeName(java.lang.String) WARNING: Please consider reporting this to the maintainers of ReflBytecodeName   WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations   WARNING: All illegal access operation...

Java Bean IBAN Validation - Creating custom constraint annotation

Image
  Although Bean Validation has plenty of handy built-in constraints, such as to make sure if a field is following a specific regex or even if a date is in the past or future, it is certain that soon or later you will need a specific constraint that is not covered by the standard annotations.  Definition of ConstraintValidator Definition of Constraint Custom constraints are at the heart of JSR 303 Bean Validation flexibility.

Triggering Bean Validation programmatically

Image
Validation in Spring Boot applications can be done in many different ways. Why validation? Validating incoming request data is good to reject nonsense as early as possible. Persistence layer validation should only be used as additional layer of safety. I prefere validation programmatically even if triggering Bean Validation programmatically takes a bit more effort. I think it is usually the most flexible way. Validation is done by using the Bean Validation API. The reference implementation for the Bean Validation API is Hibernate Validator. Triggering Bean Validation programmatically Validating extends SelfValidating class