Home >Java >javaTutorial >Protect against social engineering attacks in Java
Prevent social engineering attacks in Java
Social engineering attack is an attack that uses psychology and social engineering techniques to deceive people and obtain illegal benefits. means. In Java development, Java has become a target of hackers due to its open source and wide application. This article will introduce some ways to protect against social engineering attacks in Java and provide some code examples.
import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import java.util.Base64; public class EncryptionUtils { private static final String KEY = "MySecretKey12345"; public static String encrypt(String data) { try { SecretKeySpec secretKey = new SecretKeySpec(KEY.getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); byte[] encryptedBytes = cipher.doFinal(data.getBytes()); return Base64.getEncoder().encodeToString(encryptedBytes); } catch (Exception e) { e.printStackTrace(); } return null; } public static String decrypt(String encryptedData) { try { SecretKeySpec secretKey = new SecretKeySpec(KEY.getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, secretKey); byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedData)); return new String(decryptedBytes); } catch (Exception e) { e.printStackTrace(); } return null; } }
Use this tool class for encryption and decryption:
public class Main { public static void main(String[] args) { String password = "password123"; String encryptedPassword = EncryptionUtils.encrypt(password); System.out.println("加密后的密码:" + encryptedPassword); String decryptedPassword = EncryptionUtils.decrypt(encryptedPassword); System.out.println("解密后的密码:" + decryptedPassword); } }
public class InputValidation { public static boolean isEmailValid(String email) { String regex = "^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,}$"; return email.matches(regex); } public static boolean isPasswordValid(String password) { String regex = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$"; return password.matches(regex); } public static boolean isPhoneNumberValid(String phoneNumber) { String regex = "^\d{11}$"; return phoneNumber.matches(regex); } } public class Main { public static void main(String[] args) { String email = "example@test.com"; boolean isEmailValid = InputValidation.isEmailValid(email); System.out.println("邮箱是否有效:" + isEmailValid); String password = "Password123"; boolean isPasswordValid = InputValidation.isPasswordValid(password); System.out.println("密码是否有效:" + isPasswordValid); String phoneNumber = "12345678901"; boolean isPhoneNumberValid = InputValidation.isPhoneNumberValid(phoneNumber); System.out.println("手机号是否有效:" + isPhoneNumberValid); } }
@Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/admin/**").hasRole("ADMIN") .antMatchers("/**").permitAll() .and() .formLogin(); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication() .withUser("admin").password("{noop}admin123").roles("ADMIN") .and() .withUser("user").password("{noop}user123").roles("USER"); } } @RestController public class AdminController { @GetMapping("/admin") public String admin() { return "Welcome, admin!"; } } @RestController public class UserController { @GetMapping("/user") public String user() { return "Welcome, user!"; } } @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
The above code demonstrates role permission control on the "/admin" path, only users with the "ADMIN" role to access this path.
Through the above methods of preventing social engineering attacks, we can improve the security of Java applications. Of course, these are just some basic preventive measures. Developers still need to continue to learn and explore more security technologies to deal with the ever-changing hacker attack methods.
The above is the detailed content of Protect against social engineering attacks in Java. For more information, please follow other related articles on the PHP Chinese website!