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.
- Automatic CORS Handling: Automatically applies CORS headers to all incoming requests.
- Session Cookie Handling: Configures session cookies with security attributes like
HttpOnly,Secure, andSameSite=None. - Global Configuration: CORS settings are applied globally when the application starts, with easy customization options.
Download the latest version of the Bcors library from the link below:
- Download Bcors-1.1.jar
- View all releases here.
- Add
Bcors.jarto Your Project: Add theBcors.jarfile to your project's classpath.
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.
To customize the default CORS settings, you can create a custom ServletContextListener:
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
}
}If needed, you can still manually set CORS headers in specific servlets:
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!");
}
}- CorsFilter: Automatically applied to all requests to add the necessary CORS headers and handle
OPTIONSrequests. - CorsListener: Triggered when the web application starts, ensuring global CORS configuration.
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.
This project is licensed under the MIT License. See the LICENSE file for details.
Created by ByteBigBoss.
Contributions are welcome! Please feel free to submit a pull request or open an issue if you have any suggestions or improvements.