当前位置: 首页 > news >正文

前后端传输加密

1. HTTPS协议:安全加密传输的基石

HTTPS协议是安全传输数据的基石,它在HTTP的基础上,通过TLS/SSL协议对数据进行加密,确保数据在网络传输过程中的私密性、完整性和数据来源的可靠性。实现HTTPS主要是服务器端的配置,确保使用SSL/TLS证书,客户端可通过HttpsURLConnectionApache HttpClient进行HTTPS请求。

Nginx 配置:
对于部署在 Nginx 后面的应用,可以通过 Nginx 配置来强制使用 HTTPS。


server {listen 80;server_name example.com;return 301 https://$host$request_uri;
}server {listen 443 ssl;server_name example.com;ssl_certificate /etc/nginx/ssl/example.crt;ssl_certificate_key /etc/nginx/ssl/example.key;...
}

Spring Boot 应用:
对于 Spring Boot 应用,可以使用嵌入式 Tomcat 的 HTTPS 配置。

@Configuration
public class WebSecurityConfig implements WebMvcConfigurer {@Beanpublic EmbeddedServletContainerCustomizer containerCustomizer() {return (container -> {container.setPort(8443);container.setSessionTimeout(Duration.ofMinutes(30));container.addAdditionalTomcatConnectors(createHttpConnector());});private Connector createHttpConnector() {Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);connector.setScheme("http");connector.setPort(8080);connector.setSecure(false);connector.setRedirectPort(8443);return connector;}
}

2. 对称加密:AES算法的安全通信

对称加密,即加密和解密使用的是同一个密钥。AES(高级加密标准)是一种广泛采用的对称加密算法,它通过一定的密钥将密码加密,再通过网络传输。这种机制适用于密钥已知于发送端和接收端的场景。在Java实现AES加密解密时,需要利用javax.crypto.Cipherjavax.crypto.spec.SecretKeySpec类完成对密码的加密和解密操作。
使用javax.crypto包中的Cipher和SecretKeySpec类:

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;public class AESEncryption {private static final String ALGORITHM = "AES";public static String encrypt(String data, String key) throws Exception {byte[] keyBytes = new byte[16];byte[] b = key.getBytes("UTF-8");int len = b.length;if (len > keyBytes.length) len = keyBytes.length;System.arraycopy(b, 0, keyBytes, 0, len);SecretKeySpec secretKey = new SecretKeySpec(keyBytes, ALGORITHM);Cipher cipher = Cipher.getInstance(ALGORITHM);cipher.init(Cipher.ENCRYPT_MODE, secretKey);byte[] encrypted = cipher.doFinal(data.getBytes());return Base64.getEncoder().encodeToString(encrypted);}public static String decrypt(String data, String key) throws Exception {byte[] keyBytes = new byte[16];byte[] b = key.getBytes("UTF-8");int len = b.length;if (len > keyBytes.length) len = keyBytes.length;System.arraycopy(b, 0, keyBytes, 0, len);SecretKeySpec secretKey = new SecretKeySpec(keyBytes, ALGORITHM);Cipher cipher = Cipher.getInstance(ALGORITHM);cipher.init(Cipher.DECRYPT_MODE, secretKey);byte[] decodedValue = Base64.getDecoder().decode(data);byte[] decValue = cipher.doFinal(decodedValue);return new String(decValue);}
}

3. 非对称加密:RSA双钥机制

非对称加密使用两个不同的密钥,公钥用于加密,私钥用于解密,这种机制确保即使公钥被泄露,数据也能够安全传输。RSA算法是典型的非对称加密算法,它不仅提供了数据的加密机制,还支持数据的验证。在Java中,实现RSA加密解密需要利用javax.crypto.Cipherjava.security.KeyPairGenerator生成公钥和私钥,并使用它们分别进行加密和解密操作。
使用javax.crypto和java.security包中的相关类:


import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;public class RSACrypto {public static KeyPair generateKeyPair() throws Exception {KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");keyGen.initialize(2048);return keyGen.generateKeyPair();}public static String encrypt(String data, PublicKey publicKey) throws Exception {Cipher cipher = Cipher.getInstance("RSA");cipher.init(Cipher.ENCRYPT_MODE, publicKey);byte[] encrypted = cipher.doFinal(data.getBytes());return Base64.getEncoder().encodeToString(encrypted);}public static String decrypt(String data, PrivateKey privateKey) throws Exception {byte[] decoded = Base64.getDecoder().decode(data);Cipher decriptCipher = Cipher.getInstance("RSA");decriptCipher.init(Cipher.DECRYPT_MODE, privateKey);byte[] decrypted = decriptCipher.doFinal(decoded);return new String(decrypted);}
}

4. 哈希算法:SHA-256的密码保护

哈希算法可以生成固定长度的哈希值,对于任何输入的数据,哈希算法将输出一个固定长度的散列值,该散列值几乎指定了唯一的输出。SHA-256便是广泛应用的哈希算法之一。在Java中实现SHA-256,主要依赖于javax.security.MessageDigest类,通过此类的实例可以将任何长度的数据转换为一个固定长度的散列值,这在存储和验证密码时提供了额外的安全层。
在Java中使用MessageDigest类:


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;public class SHA256Hash {public static String hash(String password) {try {MessageDigest digest = MessageDigest.getInstance("SHA-256");byte[] encodedHash = digest.digest(password.getBytes());StringBuilder hexString = new StringBuilder();for (byte b : encodedHash) {String hex = Integer.toHexString(0xff & b);if (hex.length() == 1) hexString.append('0');hexString.append(hex);}return hexString.toString();} catch (NoSuchAlgorithmException e) {throw new RuntimeException("Error: SHA-256 algorithm not found.", e);}}
}

5. OAuth 和 OpenID Connect

使用 Spring Security 集成 OAuth2。

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {@Autowiredprivate AuthenticationManager authenticationManager;@Overridepublic void configure(ClientDetailsServiceConfigurer clients) throws Exception {clients.inMemory().withClient("client").secret(passwordEncoder().encode("secret")).authorizedGrantTypes("password", "refresh_token").scopes("read", "write").accessTokenValiditySeconds(60 * 60 * 24).refreshTokenValiditySeconds(60 * 60 * 24 * 7);}@Overridepublic void configure(AuthorizationServerEndpointsConfigurer endpoints) {endpoints.authenticationManager(authenticationManager);}@Beanpublic PasswordEncoder passwordEncoder() {return new BCryptPasswordEncoder();}
}

结语

在前端和后端之间传输时,各种加密方式的选择取决于具体的安全需求、系统架构和数据的敏感程度。HTTPS提供了基础的网络层加密,而对称和非对称加密则为数据提供了额外的保护,哈希算法确保了密码存储的安全。


http://www.mrgr.cn/news/9128.html

相关文章:

  • 无人机技术的最新进展及未来趋势
  • 掌握CSS的@import:跨文件样式的艺术
  • 【unity小技巧】获取免费开源的人物模型,并为obj fbx人物模型绑定骨骼、动画——mixamo的使用介绍
  • 中仕公考怎么样?2025年国考现在准备来得及吗?
  • 【Rust光年纪】构建跨语言桥梁:深度解析Rust FFI绑定生成器及其应用
  • 聚鼎科技:怎么做装饰画更受大众好评
  • 广告接入相关
  • 【C语言进阶】深入C语言指针:基础到进阶的跨越
  • 五、前后端分离通用权限系统(5)
  • idea debug 各个步骤含义
  • 在线BLOG网
  • AI创业的77个方向
  • LeetCode 热题100-69 有效的括号
  • 创意无限,思维升级:2024年思维导图软件新趋势与精选推荐
  • CentOS(7.x、8)上安装EMQX教程
  • flink读写案例合集
  • 【KivyMD 应用程序 1.1.1】MDBottomNavigation TabbedPanelBas选项卡底座
  • 【C++ Primer Plus习题】5.8
  • huggingface下载model
  • Go语言操作文件上传和下载应用教程