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.
- Host: GitHub
- URL: https://github.com/bytebigboss/bcors
- Owner: ByteBigBoss
- License: mit
- Created: 2024-09-09T17:39:50.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-09-10T04:09:01.000Z (over 1 year ago)
- Last Synced: 2025-01-28T01:19:06.826Z (12 months ago)
- Topics: cors, java, java-ee-7
- Language: Java
- Homepage:
- Size: 14.1 MB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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.