An open API service indexing awesome lists of open source software.

https://github.com/bytebigboss/bcors

A CORS utility library for Java web applications.
https://github.com/bytebigboss/bcors

cors java java-ee-7

Last synced: 10 months ago
JSON representation

A CORS utility library for Java web applications.

Awesome Lists containing this project

README

          

# Bcors Library

## Overview

Bcors is a Java library designed to simplify Cross-Origin Resource Sharing (CORS) configuration in Java web applications. It automatically applies CORS headers, handles session cookies, and allows for easy customization of CORS settings.

## Features

- **Automatic CORS Handling**: Automatically applies CORS headers to all incoming requests.
- **Session Cookie Handling**: Configures session cookies with security attributes like `HttpOnly`, `Secure`, and `SameSite=None`.
- **Global Configuration**: CORS settings are applied globally when the application starts, with easy customization options.

## Installation

### Download

Download the latest version of the Bcors library from the link below:

- [Download Bcors-1.1.jar](https://github.com/ByteBigBoss/Bcors/releases/download/Bcors-1.1/Bcors-1.1.jar)
- View all releases [here](https://github.com/ByteBigBoss/Bcors/releases).

### Adding to Your Project

1. **Add `Bcors.jar` to Your Project**: Add the `Bcors.jar` file to your project's classpath.

## Usage

### Automatic CORS Configuration

The Bcors library automatically applies CORS headers and handles session cookies through a servlet filter and context listener. No manual CORS configuration in servlets is required.

### Customization

To customize the default CORS settings, you can create a custom `ServletContextListener`:

```java
import com.bytebigboss.bcors.Bcors;
import java.util.Arrays;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class CustomCorsListener implements ServletContextListener {

@Override
public void contextInitialized(ServletContextEvent sce) {
// Customize CORS settings here
Bcors corsContext = Bcors.getInstance();
corsContext.setAllowedOrigins(Arrays.asList("http://yourdomain.com"));
corsContext.setAllowedMethods(Arrays.asList("POST", GET", "OPTIONS"));
corsContext.setCookieDomain("yourdomain.com");
System.out.println("Custom CORS configurations applied.");
}

@Override
public void contextDestroyed(ServletContextEvent sce) {
// Clean up code if needed
}
}
```

### Sample Usage

If needed, you can still manually set CORS headers in specific servlets:

```java
import com.bytebigboss.bcors.Bcors;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "SystemStatus", urlPatterns = {"/SystemStatus"})
public class SystemStatus extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
Bcors.setCors(req, res); // This line is usually not needed due to the automatic filter
res.getWriter().write("System is up and running!");
}
}
```

## How It Works

- **CorsFilter**: Automatically applied to all requests to add the necessary CORS headers and handle `OPTIONS` requests.
- **CorsListener**: Triggered when the web application starts, ensuring global CORS configuration.

### Note

With the inclusion of CorsFilter and CorsListener in the Bcors library, there's no need for users to add these manually to their project. The library handles CORS configuration automatically, ensuring a seamless integration experience.

## License

This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.

## Author

Created by ByteBigBoss.

## Contributions

Contributions are welcome! Please feel free to submit a pull request or open an issue if you have any suggestions or improvements.