To protect Struts 2 applications, you can use the following security configurations: Disable unused features Enable content type checking Validate input Enable security tokens Prevent CSRF attacks Use RBAC to restrict role-based access
Security Configuration and Hardening of the Struts 2 Framework
Struts 2 is a popular Java web application framework. To protect your Struts 2 applications from security threats, it is crucial to implement appropriate security configurations. This tutorial will guide you step by step on how to secure your Struts 2 application.
1. Disable unused functionality
Disabling unused Struts 2 functionality in your application can reduce the potential attack surface. In the struts.xml
configuration file, you can achieve this by limiting the defaultAction
servlet filter to parsing the default action. For example:
<struts> <constant name="struts.action.excludePattern" value="^/.*/$" /> </struts>
2. Enable content type checking
Struts 2 provides content type checking functionality that prevents users from submitting content that does not match the data type expected by the application. It can be enabled by setting a few properties in the struts.properties
file:
struts.multipart.parser=jakarta-multipart struts.multipart.multiPartParser.maximumRequestSize=2MB struts.multipart.multiPartParser.maximumFileSize=1MB
3. Validating input
Validate the input received from the user Input is critical to preventing injection attacks. Struts 2 provides built-in validators that you can use in Action classes. For example:
@Validate public class MyAction extends ActionSupport { private String name; @Required public String getName() { return name; } }
4. Enable security tokens
Security tokens are used to prevent cross-site request forgery (CSRF) attacks, in which an attacker tricks a victim into submitting Requests that do not belong to them. Struts 2 allows you to create and verify security tokens before submitting a form. You can enable it by configuring the following in the web.xml
file:
<filter> <filter-name>struts2-token</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2-token</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
5. Restricting access
Role-based access control ( RBAC) ensures that only authorized users can access certain resources in an application. Struts 2 supports RBAC through the @RolesAllowed
annotation. For example:
@RolesAllowed("admin") public String doAdminAction() { // 只有管理员才有权访问此操作 }
Practical case
The following is an example Struts 2 Action class that demonstrates the comprehensive use of security configuration:
@Namespace("/") @Action("/secureAction") @RolesAllowed("secure") public class SecureAction extends ActionSupport { @Required private String input; @Override public String execute() { if (!TokenHelper.validToken()) { return INPUT; } if (someValidationRule()) { return SUCCESS; } else { addFieldError("input", "Invalid input"); return INPUT; } } }
By implementing these Security Configuration, you can significantly enhance the security of your Struts 2 application and make it safe from common threats.
The above is the detailed content of Security configuration and hardening of Struts 2 framework. For more information, please follow other related articles on the PHP Chinese website!

JVM works by converting Java code into machine code and managing resources. 1) Class loading: Load the .class file into memory. 2) Runtime data area: manage memory area. 3) Execution engine: interpret or compile execution bytecode. 4) Local method interface: interact with the operating system through JNI.

JVM enables Java to run across platforms. 1) JVM loads, validates and executes bytecode. 2) JVM's work includes class loading, bytecode verification, interpretation execution and memory management. 3) JVM supports advanced features such as dynamic class loading and reflection.

Java applications can run on different operating systems through the following steps: 1) Use File or Paths class to process file paths; 2) Set and obtain environment variables through System.getenv(); 3) Use Maven or Gradle to manage dependencies and test. Java's cross-platform capabilities rely on the JVM's abstraction layer, but still require manual handling of certain operating system-specific features.

Java requires specific configuration and tuning on different platforms. 1) Adjust JVM parameters, such as -Xms and -Xmx to set the heap size. 2) Choose the appropriate garbage collection strategy, such as ParallelGC or G1GC. 3) Configure the Native library to adapt to different platforms. These measures can enable Java applications to perform best in various environments.

OSGi,ApacheCommonsLang,JNA,andJVMoptionsareeffectiveforhandlingplatform-specificchallengesinJava.1)OSGimanagesdependenciesandisolatescomponents.2)ApacheCommonsLangprovidesutilityfunctions.3)JNAallowscallingnativecode.4)JVMoptionstweakapplicationbehav

JVMmanagesgarbagecollectionacrossplatformseffectivelybyusingagenerationalapproachandadaptingtoOSandhardwaredifferences.ItemploysvariouscollectorslikeSerial,Parallel,CMS,andG1,eachsuitedfordifferentscenarios.Performancecanbetunedwithflagslike-XX:NewRa

Java code can run on different operating systems without modification, because Java's "write once, run everywhere" philosophy is implemented by Java virtual machine (JVM). As the intermediary between the compiled Java bytecode and the operating system, the JVM translates the bytecode into specific machine instructions to ensure that the program can run independently on any platform with JVM installed.

The compilation and execution of Java programs achieve platform independence through bytecode and JVM. 1) Write Java source code and compile it into bytecode. 2) Use JVM to execute bytecode on any platform to ensure the code runs across platforms.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
