Code Monkey home page Code Monkey logo

blog-springsecurity's Introduction

Blog Application Backend

This project is a backend for a blog application developed using Spring Boot. It incorporates Spring Security for authentication and authorization, utilizing JWT (JSON Web Tokens) for secure, stateless authentication. The application supports role-based access control, distinguishing between roles like ADMIN, MODERATOR and USER. It connects to a MySQL database by default but can also be configured to use an H2 in-memory database for development and testing purposes.

Features

  • RESTful API for managing blog posts, comments, and users.
  • Secure user authentication using JWT.
  • Role-based access control.
  • Connection to MySQL and optional H2 database for easy testing.
  • Comprehensive error handling and validation.

Prerequisites

Before you can run this project, you will need:

  • Java JDK 11 or later
  • Maven 3.6 or later
  • MySQL Server (Optional for production)

Configuration

Database Setup

MySQL

  1. Install MySQL and start the MySQL service.

  2. Create a database for the application:

    CREATE DATABASE blog_app;
  3. Update src/main/resources/application.properties for MySQL:

    spring.datasource.url=jdbc:mysql://localhost:3306/blog_app
    spring.datasource.username=your_mysql_username
    spring.datasource.password=your_mysql_password

H2 Database

The application is configured to use MySQL in development. But H2 databases can also be easily implemented, you can access the H2 console at http://localhost:8080/h2-console with the JDBC URL set to jdbc:h2:mem:testdb.

JWT Configuration

Set your JWT secret and expiration time in src/main/resources/application.properties:

app.jwt.secret=your_secret_key
app.jwt.expiration-ms=86400000  # 24 hours

Running the Application

To run the application, use the following command from the root directory of the project:

mvn spring-boot:run

API Endpoints

The following are the main endpoints provided by the blog application:

Auth

  • POST /api/auth/signup - Register a new user.
  • POST /api/auth/login - Authenticate a user and return a JWT.

Posts

  • POST /api/home/posts - Create a new blog post (Admin and User).
  • GET /api/home/posts - Retrieve all posts.

Security

This application uses Spring Security to handle authentication and authorization. The security configuration is set up in com.example.blog.security.SecurityConfig. The configuration includes JWT authentication filter, which intercepts and processes the authentication token.

blog-springsecurity's People

Contributors

anantsingh1510 avatar

Watchers

 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.