Giter Site home page Giter Site logo

springsecurity-demo's Introduction

Spring-security-jwt-demo

Introduce

Spring Security is a very powerful framework in the Spring family for authentication and permission control, and we can easily extend it to meet our current system security requirements.

This demo mainly uses Spring Security and Spring Boot, and all the dependencies adopt the latest stable version. Beyond the initial project, the JPA technology was also used.

Files Structure

Snipaste_2022-03-26_18-58-52

Tech

Some frameworks/services used in the project:

  • Database: H2 in-memory database, no manual installation required.
  • Cache : Redis
  • Permission framework: Spring Security
  • ORM framework : JPA (a small amount of SQL)
  • Interface documentation :

Features

  • Add H2 in-memory database support, no need for MySQL, just one click to start the project to access http://localhost:8081/api/h2-console (username:root,password:root)
  • Add Swagger, easy to call interface
  • Refactor the exception handling part of the code to optimize the return structure
  • Create a new role table, then associate users with roles by creating a new role_user table
  • File structure refactoring
  • add Jpa auditing functionality
  • login (login) interface exposed at controller level
  • Logout function: redis stores token information (key->user id,value->token) and removes the token information from redis after logout
  • Re-login will update the token information stored in redis

Setup

  1. git clone this repo.
  2. open project and wait maven to install project Dependencies
  3. change application.properties change the database connection information parameter to your own
  4. Run the project (related data tables will be created automatically, if you don't understand, take a look at JPA)

Sample

1.Sign in

URL: POST http://localhost:8081/api/users/sign-up

RequestBody:

{"userName":"jake","fullName":"IronMan","password":"123"}

Newly registered users are bound by default to the following roles: USER and MANAGER.

Snipaste_2022-03-26_18-12-26

2.Log in

URL:POST http://localhost:8081/api/auth/login

RequestBody:

{"username": "jake", "password": "123","rememberMe":true}

Success

Snipaste_2022-03-26_18-12-17

Failed

Snipaste_2022-03-26_18-29-12

3.Use the correct Token to access resources that require authentication

We use a GET request to access /api/users, the access rights for this interface are:

@PreAuthorize("hasAnyRole('ROLE_USER','ROLE_MANAGER','ROLE_ADMIN')")

Snipaste_2022-03-26_18-12-56

4.Access without a Token or with an invalid Token

We use GET requests to access /api/users but without a token or with an invalid token.

Snipaste_2022-03-26_18-13-12

5.With the correct Token but with access rights

URL:POST http://localhost:8081/api/users?username=jake

We use a DELETE request to access /api/users?username=xxx, carrying a valid token, but the token does not have enough access rights.

Snipaste_2022-03-26_18-14-38

springsecurity-demo's People

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.