Skip to content

skn437/skn-spring-security

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

15 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

SKN Reactive Spring Boot Security Configuration Library

java

Java

Maven Central Javadoc Apache License 2.0

Β 

JavaDocs:

Read the Javadoc for the main Service APIs

Β 

Introduction:

This is a simple Java Reactive Spring Boot Library for security configuration

I made this library so that I can use it in most of my spring boot reactive projects without writing the same codes over and over again

The main API Classes of this library are CsrfService, PrincipalService & SessionService, each of which holds a method for GET request

Β 

Details:

(1) CsrfService Class:

  • It needs to be instantiated first
  • It must be used in controller GET requests
  • It has 1 method to get CSRF Token if CSRF is enabled in Spring Security
  • This method throws Exception if error occurs getting and showing CSRF token

(2) PrincipalService Class:

  • It needs to be instantiated first
  • It must be used in controller GET requests
  • It has 1 method to get Principal information i.e. the info of the authenticated users
  • This method throws Exception if error occurs getting and showing Principal info

(3) SessionService Class:

  • It needs to be instantiated first
  • It must be used in controller GET requests
  • It has 1 method to get Web Session information
  • This method throws Exception if error occurs getting and showing Web Session info

Β 

Requirements:

  • πŸ’€ Minimum Java Version: 21
  • πŸ’€ Minimum Maven Version: 3.9.6
  • πŸ’€ Minimum Spring Boot Version: 3.2.5
  • πŸ’€ Spring Web Flux (Reactive Spring Boot)
  • πŸ’€ Spring Security
  • πŸ’€ Spring Session
  • πŸ’€ Spring OAuth2 Client

Β 

Usage:

For Maven, inside dependencies tag of pom.xml, copy the following

<dependency>
  <groupId>best.skn</groupId>
  <artifactId>skn-spring-security</artifactId>
  <version>1.2.1</version>
</dependency>

Write the following in the terminal

mvn install

First create a configuration class

import best.skn.security.configurations.*;

@Configuration
@Import(
  {
    CsrfConfiguration.class,
    PrincipalConfiguration.class,
    SessionConfiguration.class
  }
)
public class SecurityInfoConfiguration {}

Inside your Java Code, import the package like this for CsrfService

import best.skn.security.services.CsrfService;

Inside your Java Code, import the package like this for PrincipalService

import best.skn.security.services.PrincipalService;

Inside your Java Code, import the package like this for SessionService

import best.skn.security.services.SessionService;

For external configuration in application.yaml, follow the instructions below

(1) CORS Configuration: When setting your CORS allowed origins, you have to give a list of String as a parameter in Spring Security, but with this library you can externalize it

(a) First inside your CORS configuration, do this
import best.skn.security.properties.CorsSourceProperty;

@Configuration
@EnableConfigurationProperties(CorsSourceProperty.class)
public class CorsSourceConfiguration() {

  @Autowired
  private CorsSourceProperty corsSourceProperty;

  // your configuration here. The class name is arbitary.
}
(b) In application.yaml, do this
skn:
  security:
    cors:
      origins:
        - http://localhost:3000
        - http://localhost:5173

(2) Login Configuration: When setting your custom login url, you have to give a String as a parameter in Spring Security, but with this library you can externalize it

(a) First inside your Login configuration, do this
import best.skn.security.properties.LoginProperty;

@Configuration
@EnableConfigurationProperties(LoginProperty.class)
public class LoginConfiguration() {

  @Autowired
  private LoginProperty loginProperty;

  // your configuration here. The class name is arbitary.
}
(b) In application.yaml, do like this
skn:
  security:
    login:
      url: http://localhost:5173/api/auth/login

(3) OAuth2 Configuration: When setting your custom successful login redirection url for OAuth2, you have to give a String as a parameter in Spring Security, but with this library you can externalize it

(a) First inside your OAuth2 configuration, do this
import best.skn.security.properties.OAuth2Property;

@Configuration
@EnableConfigurationProperties(OAuth2Property.class)
public class OAuth2Configuration() {

  @Autowired
  private OAuth2Property oAuth2Property;

  // your configuration here. The class name is arbitary.
}
(b) In application.yaml, do just like this
skn:
  security:
    oauth2:
      client:
        success:
          redirect: http://localhost:5173/api/auth/status

(4) Security Configuration: When setting your authenticated exchanges, you have to give a list of String as a parameter in Spring Security, but with this library you can externalize it

(a) First inside your Security configuration, do this
import best.skn.security.properties.SecurityProperty;

@Configuration
@EnableConfigurationProperties(SecurityProperty.class)
public class SecurityConfiguration() {

  @Autowired
  private SecurityProperty securityProperty;

  // your configuration here. The class name is arbitary.
}
(b) In application.yaml, you can do this
skn:
  security:
    authorize:
      exchanges:
        - /
        - /skn/**
        - /api/auth/**
        - /api/mail/**

For instructions for Gradle & others: Visit Maven Central

Β 

Dedicated To:

  • πŸ‘©β€πŸŽ¨Prodipta Das Logno & πŸ§›β€β™€οΈAtoshi Sarker Prithula: The two most special ladies of my life. I can't thank them enough for always treasuring me a lot. I am lucky that I met with these two amazing ladies. They have two special places in my heart and no other girl can ever replace them.
  • πŸ’―My Parents: The greatest treasures of my life ever.