zainabed-spring-security-jwt

JWT based authentication and authorization for Spring Boot projects

Лицензия

Лицензия

Категории

Категории

Безопасность
Группа

Группа

com.zainabed.spring
Идентификатор

Идентификатор

zainabed-spring-security-jwt
Последняя версия

Последняя версия

1.0.0
Дата

Дата

Тип

Тип

jar
Описание

Описание

zainabed-spring-security-jwt
JWT based authentication and authorization for Spring Boot projects
Ссылка на сайт

Ссылка на сайт

https://projects.spring.io/spring-boot/#/spring-boot-starter-parent/zainabed-spring-security-jwt
Система контроля версий

Система контроля версий

https://github.com/zainabed/zainabed-spring-security-jwt/tree/master

Скачать zainabed-spring-security-jwt

Как подключить последнюю версию

<!-- https://jarcasting.com/artifacts/com.zainabed.spring/zainabed-spring-security-jwt/ -->
<dependency>
    <groupId>com.zainabed.spring</groupId>
    <artifactId>zainabed-spring-security-jwt</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.zainabed.spring/zainabed-spring-security-jwt/
implementation 'com.zainabed.spring:zainabed-spring-security-jwt:1.0.0'
// https://jarcasting.com/artifacts/com.zainabed.spring/zainabed-spring-security-jwt/
implementation ("com.zainabed.spring:zainabed-spring-security-jwt:1.0.0")
'com.zainabed.spring:zainabed-spring-security-jwt:jar:1.0.0'
<dependency org="com.zainabed.spring" name="zainabed-spring-security-jwt" rev="1.0.0">
  <artifact name="zainabed-spring-security-jwt" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.zainabed.spring', module='zainabed-spring-security-jwt', version='1.0.0')
)
libraryDependencies += "com.zainabed.spring" % "zainabed-spring-security-jwt" % "1.0.0"
[com.zainabed.spring/zainabed-spring-security-jwt "1.0.0"]

Зависимости

compile (3)

Идентификатор библиотеки Тип Версия
org.springframework.boot : spring-boot-starter-security jar 2.0.6.RELEASE
org.springframework.boot : spring-boot-starter-web jar 2.0.6.RELEASE
io.jsonwebtoken : jjwt-api jar 0.10.5

runtime (2)

Идентификатор библиотеки Тип Версия
io.jsonwebtoken : jjwt-impl jar 0.10.5
io.jsonwebtoken : jjwt-jackson jar 0.10.5

test (4)

Идентификатор библиотеки Тип Версия
org.springframework.boot : spring-boot-starter-test jar 2.0.6.RELEASE
org.springframework.security : spring-security-test jar
com.jayway.jsonpath : json-path jar 2.2.0
com.jayway.jsonpath : json-path-assert jar 2.2.0

Модули Проекта

Данный проект не имеет модулей.

Zainabed Spring Security JWT

Build Status

Security JWT makes it easy to configure authentication and authorization security system into Spring Boot applications. It secures application with few configurations.

Our objectives are

  • Application specific authentication
  • Decouple authentication & authorization
  • Configurable JWT token based security

Concept

Authentication

Spring Security Jwt uses Basic Authentication schema to validate user.

Basic authentication is a simple authentication scheme built into the HTTP protocol. The client sends HTTP requests with the Authorization header that contains user credentials. Authorization header is constructed using string username:password encoded in Base64 and prefixed with String Basic

Authorization: Basic dGVzdDp0ZXN0
Authorization

Once the user is logged in, Spring Security JWT creates JWT token as HTTP response to client.

Response example

{
    token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
    type: Bearer
    refereshToken: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
}

Then each subsequent request will have to include the JWT token, allowing the user to access resources that are permitted with that token.

Whenever the user wants to access a protected resource, the client should send the JWT token in the Authorization header using the Bearer schema. The content of the header should look like the following:

Authorization: Bearer <token>

Installation

Use your favorite Maven-compatible build tool to pull the dependencies from Maven Central

Maven

<dependency>
  <groupId>com.zainabed.spring</groupId>
  <artifactId>zainabed-spring-security-jwt</artifactId>
  <version>1.0.0</version>
</dependency>

Configuration

First step is to enable JWT security by extending JwtWebSecuriy class and annotation it with @EnableJwtSecurity.

    import com.zainabed.spring.security.jwt.annotation.EnableJwtSecurity;
    import com.zainabed.spring.security.jwt.security.JwtWebSecuriy;

    @EnableJwtSecurity
    public class ApplicationWebSecurity extends JwtWebSecuriy{
    }

Second step is to set JWT properties in application.properties file.

jwt.token.secret= <secret value>
jwt.token.expiration= <expiration time in seconds>

This is common configuration to enable both authentication and authorization.

Authentication

To activate authentication define JWT authentication property and set value as true.

jwt.authentication=true

Authentication is mapped at "/auth" route. To generate JWT token HTTP POST request should call "/auth" request with Basic Authentication header which should include user credentials which should be encoded with Base64

URL: http://localhost:8080/auth

Header:
Authorization: Basic <username-value:password-value>
Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l

Security authentication controller let you to define your own authentication module to verify user credential. to do so you have to implement JwtAuthenticationService and annotate it with @Service.

@Service
public class JwtAuthenticationServiceImpl implements JwtAuthenticationService {

	@Override
	public UserDetail authenticate(UserCredential userCredential) throws AuthenticationException {
		// Define your own authentication mechanism and return result as UserDetail object
	}

}
Authorization

Authorization process get activated when you define token secret and expiration time in properties file and extend JwtWebSecuriy , you can secure you REST controller as

@RestController
@RequestMapping(value = "/test")
public class TestControlller {

	@Secured("ROLE_USER")
	@RequestMapping(value = "/user", method = RequestMethod.GET)
	public String testUserWithRole() {
		return "Test user with User role.";
	}

	@Secured(value = "ROLE_ADMIN")
	@RequestMapping(value = "/admin", method = RequestMethod.GET)
	public String testAdmin() {
		return "Test user with Admin role.";
	}
}

Версии библиотеки

Версия
1.0.0