import os import re directory = '/home/x79/Documents/sisvietnamvn_01/sisvietnamvn_main/src/main/resources/templates/manage' layout = '/home/x79/Documents/sisvietnamvn_01/sisvietnamvn_main/src/main/resources/templates/fragments/manage-layout.html' csrf_token = '\n ' def process_file(filepath): with open(filepath, 'r', encoding='utf-8') as f: content = f.read() # Check if csrf token is already present if '_csrf.parameterName' in content: print(f"Skipping {filepath} - already has CSRF token") return # Regex to find
# It finds the end of the tag that has method="post" pattern = r'(]*?method=["\']post["\'][^>]*?>)' # We want to replace the match with the match + csrf_token new_content = re.sub(pattern, r'\1' + csrf_token, content, flags=re.IGNORECASE) if new_content != content: with open(filepath, 'w', encoding='utf-8') as f: f.write(new_content) print(f"Updated {filepath}") for root, _, files in os.walk(directory): for file in files: if file.endswith('.html'): process_file(os.path.join(root, file)) process_file(layout)