正在显示
6 个修改的文件
包含
106 行增加
和
172 行删除
| @@ -50,7 +50,7 @@ public class RabbitController { | @@ -50,7 +50,7 @@ public class RabbitController { | ||
| 50 | @GetMapping("/test/consumer") | 50 | @GetMapping("/test/consumer") |
| 51 | public void consumer() throws IOException, TimeoutException | 51 | public void consumer() throws IOException, TimeoutException |
| 52 | { | 52 | { |
| 53 | - directUtils.directConsumer("Q_zicheng", "E_zicheng", "R_zicheng"); | 53 | + |
| 54 | } | 54 | } |
| 55 | 55 | ||
| 56 | @PostMapping("/product/old") | 56 | @PostMapping("/product/old") |
| @@ -5,7 +5,7 @@ import com.rabbitmq.http.client.domain.UserPermissions; | @@ -5,7 +5,7 @@ import com.rabbitmq.http.client.domain.UserPermissions; | ||
| 5 | import com.sunyo.wlpt.message.bus.service.domain.BusServer; | 5 | import com.sunyo.wlpt.message.bus.service.domain.BusServer; |
| 6 | import com.sunyo.wlpt.message.bus.service.domain.UserInfo; | 6 | import com.sunyo.wlpt.message.bus.service.domain.UserInfo; |
| 7 | import com.sunyo.wlpt.message.bus.service.domain.VirtualHost; | 7 | import com.sunyo.wlpt.message.bus.service.domain.VirtualHost; |
| 8 | -import com.sunyo.wlpt.message.bus.service.utils.EncryptionUtils; | 8 | +import com.sunyo.wlpt.message.bus.service.utils.AESUtils; |
| 9 | 9 | ||
| 10 | import java.io.IOException; | 10 | import java.io.IOException; |
| 11 | import java.net.URISyntaxException; | 11 | import java.net.URISyntaxException; |
| @@ -25,9 +25,7 @@ public class ClientUtils { | @@ -25,9 +25,7 @@ public class ClientUtils { | ||
| 25 | // 该服务器超级用户的用户名称 | 25 | // 该服务器超级用户的用户名称 |
| 26 | String superUsername = busServer.getSuperUsername(); | 26 | String superUsername = busServer.getSuperUsername(); |
| 27 | // 该服务器超级用户的用户密码 | 27 | // 该服务器超级用户的用户密码 |
| 28 | - String base = EncryptionUtils.decryptBase64(busServer.getSuperPassword()); | ||
| 29 | - String[] split = base.split("\\."); | ||
| 30 | - String superPassword = split[split.length - 1]; | 28 | + String superPassword = AESUtils.decrypt(busServer.getSuperPassword()); |
| 31 | 29 | ||
| 32 | // 服务器的客户端端口号 | 30 | // 服务器的客户端端口号 |
| 33 | String clientPort = busServer.getClientPort().toString(); | 31 | String clientPort = busServer.getClientPort().toString(); |
| @@ -4,15 +4,13 @@ import com.rabbitmq.client.*; | @@ -4,15 +4,13 @@ import com.rabbitmq.client.*; | ||
| 4 | import com.sunyo.wlpt.message.bus.service.domain.XmlData; | 4 | import com.sunyo.wlpt.message.bus.service.domain.XmlData; |
| 5 | import com.sunyo.wlpt.message.bus.service.exception.CustomExceptionType; | 5 | import com.sunyo.wlpt.message.bus.service.exception.CustomExceptionType; |
| 6 | import com.sunyo.wlpt.message.bus.service.response.ResultJson; | 6 | import com.sunyo.wlpt.message.bus.service.response.ResultJson; |
| 7 | -import com.sunyo.wlpt.message.bus.service.utils.EncryptionUtils; | 7 | +import com.sunyo.wlpt.message.bus.service.utils.AESUtils; |
| 8 | import com.sunyo.wlpt.message.bus.service.utils.IdUtils; | 8 | import com.sunyo.wlpt.message.bus.service.utils.IdUtils; |
| 9 | import lombok.extern.slf4j.Slf4j; | 9 | import lombok.extern.slf4j.Slf4j; |
| 10 | import org.springframework.beans.factory.annotation.Autowired; | 10 | import org.springframework.beans.factory.annotation.Autowired; |
| 11 | -import org.springframework.beans.factory.annotation.Value; | ||
| 12 | import org.springframework.data.redis.core.StringRedisTemplate; | 11 | import org.springframework.data.redis.core.StringRedisTemplate; |
| 13 | import org.springframework.stereotype.Component; | 12 | import org.springframework.stereotype.Component; |
| 14 | 13 | ||
| 15 | -import javax.annotation.PostConstruct; | ||
| 16 | import java.io.IOException; | 14 | import java.io.IOException; |
| 17 | import java.util.concurrent.TimeUnit; | 15 | import java.util.concurrent.TimeUnit; |
| 18 | import java.util.concurrent.TimeoutException; | 16 | import java.util.concurrent.TimeoutException; |
| @@ -29,60 +27,6 @@ public class DirectUtils { | @@ -29,60 +27,6 @@ public class DirectUtils { | ||
| 29 | @Autowired | 27 | @Autowired |
| 30 | private StringRedisTemplate redisTemplate; | 28 | private StringRedisTemplate redisTemplate; |
| 31 | 29 | ||
| 32 | - @Value("${spring.rabbitmq.host}") | ||
| 33 | - private String host; | ||
| 34 | - | ||
| 35 | - @Value("${spring.rabbitmq.port}") | ||
| 36 | - private int port; | ||
| 37 | - | ||
| 38 | - @Value("${spring.rabbitmq.username}") | ||
| 39 | - private String username; | ||
| 40 | - | ||
| 41 | - @Value("${spring.rabbitmq.password}") | ||
| 42 | - private String password; | ||
| 43 | - | ||
| 44 | - @Value("${spring.rabbitmq.virtual-host}") | ||
| 45 | - private String vHost; | ||
| 46 | - | ||
| 47 | - /** | ||
| 48 | - * @return 链接 rabbitmq | ||
| 49 | - * @throws IOException IO异常 | ||
| 50 | - * @throws TimeoutException 超时异常 | ||
| 51 | - */ | ||
| 52 | - @PostConstruct | ||
| 53 | - public Connection getConnection() throws IOException, TimeoutException | ||
| 54 | - { | ||
| 55 | - //定义连接工厂 | ||
| 56 | - ConnectionFactory factory = new ConnectionFactory(); | ||
| 57 | - //设置服务地址 | ||
| 58 | - factory.setHost(host); | ||
| 59 | - //端口,amqp协议 端口 类似与mysql的3306 | ||
| 60 | - factory.setPort(port); | ||
| 61 | - //设置账号信息,用户名、密码、vhost | ||
| 62 | - factory.setVirtualHost(vHost); | ||
| 63 | - factory.setUsername(username); | ||
| 64 | - factory.setPassword(password); | ||
| 65 | - // 通过工程获取连接 | ||
| 66 | - Connection connection = factory.newConnection(); | ||
| 67 | - return connection; | ||
| 68 | - } | ||
| 69 | - | ||
| 70 | - public Connection getConnection(String hostIp, int hostPort, String vHostName) throws Exception | ||
| 71 | - { | ||
| 72 | - //定义连接工厂 | ||
| 73 | - ConnectionFactory factory = new ConnectionFactory(); | ||
| 74 | - //设置服务地址 | ||
| 75 | - factory.setHost(hostIp); | ||
| 76 | - //端口 | ||
| 77 | - factory.setPort(hostPort); | ||
| 78 | - //设置账号信息,用户名、密码、vhost | ||
| 79 | - factory.setVirtualHost(vHostName); | ||
| 80 | - factory.setUsername(username); | ||
| 81 | - factory.setPassword(password); | ||
| 82 | - // 通过工程获取连接 | ||
| 83 | - return factory.newConnection(); | ||
| 84 | - } | ||
| 85 | - | ||
| 86 | /** | 30 | /** |
| 87 | * 链接 RabbitMQ | 31 | * 链接 RabbitMQ |
| 88 | * | 32 | * |
| @@ -96,8 +40,6 @@ public class DirectUtils { | @@ -96,8 +40,6 @@ public class DirectUtils { | ||
| 96 | */ | 40 | */ |
| 97 | public static Connection getConnection(String hostIp, int hostPort, String vHostName, String userName, String password) throws IOException, TimeoutException | 41 | public static Connection getConnection(String hostIp, int hostPort, String vHostName, String userName, String password) throws IOException, TimeoutException |
| 98 | { | 42 | { |
| 99 | - String base = EncryptionUtils.decryptBase64(password); | ||
| 100 | - String[] split = base.split("\\."); | ||
| 101 | //定义连接工厂 | 43 | //定义连接工厂 |
| 102 | ConnectionFactory factory = new ConnectionFactory(); | 44 | ConnectionFactory factory = new ConnectionFactory(); |
| 103 | //设置服务地址 | 45 | //设置服务地址 |
| @@ -107,7 +49,7 @@ public class DirectUtils { | @@ -107,7 +49,7 @@ public class DirectUtils { | ||
| 107 | //设置账号信息,用户名、密码、vhost | 49 | //设置账号信息,用户名、密码、vhost |
| 108 | factory.setVirtualHost(vHostName); | 50 | factory.setVirtualHost(vHostName); |
| 109 | factory.setUsername(userName); | 51 | factory.setUsername(userName); |
| 110 | - factory.setPassword(split[split.length - 1]); | 52 | + factory.setPassword(AESUtils.decrypt(password)); |
| 111 | // 通过工程获取连接 | 53 | // 通过工程获取连接 |
| 112 | return factory.newConnection(); | 54 | return factory.newConnection(); |
| 113 | } | 55 | } |
| @@ -132,79 +74,21 @@ public class DirectUtils { | @@ -132,79 +74,21 @@ public class DirectUtils { | ||
| 132 | } | 74 | } |
| 133 | } | 75 | } |
| 134 | 76 | ||
| 135 | - | ||
| 136 | - /** | ||
| 137 | - * DirectExchange的 消息生产者 | ||
| 138 | - * | ||
| 139 | - * @param exchangeName 交换机名称 | ||
| 140 | - * @param routingKeyName 路由键名称 | ||
| 141 | - * @param msg 发送的消息 | ||
| 142 | - * @throws IOException | ||
| 143 | - * @throws TimeoutException | ||
| 144 | - */ | ||
| 145 | - public void directProducer(String exchangeName, String routingKeyName, String msg) throws IOException, TimeoutException | ||
| 146 | - { | ||
| 147 | - // 1、创建ConnectionFactory | ||
| 148 | - Connection connection = getConnection(); | ||
| 149 | - // 2、 通过Connection创建一个新的Channel | ||
| 150 | - Channel channel = connection.createChannel(); | ||
| 151 | - // 3、开启消息的确认机制(confirm:保证消息能够发送到 exchange) | ||
| 152 | - channel.confirmSelect(); | ||
| 153 | - // 4、避免消息被重复消费 | ||
| 154 | - AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder() | ||
| 155 | - // 指定消息是否需要持久化,1:需要持久化;2:不需要持久化 | ||
| 156 | - .deliveryMode(1) | ||
| 157 | - // 设置全局唯一消息机制id(雪花id) | ||
| 158 | - .messageId(IdUtils.generateId()) | ||
| 159 | - .build(); | ||
| 160 | - // 5、开启 return 机制(保证消息,从 Exchange 分发到 Queue ) | ||
| 161 | - channel.addReturnListener(new ReturnListener() { | ||
| 162 | - @Override | ||
| 163 | - public void handleReturn(int replyCode, String replyText, String exchange, String routingKey, AMQP.BasicProperties properties, byte[] body) throws IOException | ||
| 164 | - { | ||
| 165 | - // 当消息没有从 Exchange 分发到 Queue 时,才会执行 | ||
| 166 | - log.error(new String(body, "UTF8") + "->没有从 Exchange 分发到Queue中"); | ||
| 167 | - } | ||
| 168 | - }); | ||
| 169 | - | ||
| 170 | - // 6、发送消息,并指定 mandatory 参数为true | ||
| 171 | - channel.basicPublish(exchangeName, routingKeyName, true, properties, msg.getBytes()); | ||
| 172 | - log.info("消息生产者,目标交换机:{};路由键:{};发送信息:{}", exchangeName, routingKeyName, msg); | ||
| 173 | - // 7、添加一个异步 confirm 确认监听,用于发送消息到Broker端之后,回送消息的监听 | ||
| 174 | - channel.addConfirmListener(new ConfirmListener() { | ||
| 175 | - // 发送成功 | ||
| 176 | - @Override | ||
| 177 | - public void handleAck(long deliveryTag, boolean multiple) throws IOException | ||
| 178 | - { | ||
| 179 | - log.info("消息发送成功,标识:{};是否是批量:{}", deliveryTag, multiple); | ||
| 180 | - } | ||
| 181 | - | ||
| 182 | - // 发送失败 | ||
| 183 | - @Override | ||
| 184 | - public void handleNack(long deliveryTag, boolean multiple) throws IOException | ||
| 185 | - { | ||
| 186 | - log.error("消息发送失败,标识:{};是否是批量:{}", deliveryTag, multiple); | ||
| 187 | - } | ||
| 188 | - }); | ||
| 189 | - // finally,关闭连接 | ||
| 190 | - closeConnectionAndChanel(channel, connection); | ||
| 191 | - } | ||
| 192 | - | ||
| 193 | - | ||
| 194 | /** | 77 | /** |
| 195 | * DirectExchange的 消息消费者 | 78 | * DirectExchange的 消息消费者 |
| 196 | * | 79 | * |
| 197 | * @throws IOException IO异常 | 80 | * @throws IOException IO异常 |
| 198 | * @throws TimeoutException 超时异常 | 81 | * @throws TimeoutException 超时异常 |
| 199 | */ | 82 | */ |
| 200 | - public void directConsumer(String queueName, String exchangeName, String routingKeyName) throws IOException, TimeoutException | 83 | + public void directConsumer(XmlData xmlData) throws IOException, TimeoutException |
| 201 | { | 84 | { |
| 202 | // 1、创建ConnectionFactory | 85 | // 1、创建ConnectionFactory |
| 203 | - Connection connection = getConnection(); | 86 | + Connection connection = getConnection(xmlData.getServerIp(), xmlData.getServerPort(), |
| 87 | + xmlData.getVirtualHostName(), xmlData.getSuperUsername(), xmlData.getSuperPassword()); | ||
| 204 | // 2、 通过Connection创建一个新的Channel | 88 | // 2、 通过Connection创建一个新的Channel |
| 205 | Channel channel = connection.createChannel(); | 89 | Channel channel = connection.createChannel(); |
| 206 | // 3、设置绑定关系(队列、交换机名称、路由键名称) | 90 | // 3、设置绑定关系(队列、交换机名称、路由键名称) |
| 207 | - channel.queueBind(queueName, exchangeName, routingKeyName); | 91 | +// channel.queueBind(queueName, exchangeName, routingKeyName); |
| 208 | // 一次只接受一条未确认的消息 | 92 | // 一次只接受一条未确认的消息 |
| 209 | channel.basicQos(1); | 93 | channel.basicQos(1); |
| 210 | // 4、开启监听Queue | 94 | // 4、开启监听Queue |
| @@ -239,7 +123,7 @@ public class DirectUtils { | @@ -239,7 +123,7 @@ public class DirectUtils { | ||
| 239 | } | 123 | } |
| 240 | }; | 124 | }; |
| 241 | // 消费消息 | 125 | // 消费消息 |
| 242 | - channel.basicConsume(queueName, false, consumer); | 126 | + channel.basicConsume(xmlData.getQueueName(), false, consumer); |
| 243 | } | 127 | } |
| 244 | 128 | ||
| 245 | 129 |
| @@ -8,9 +8,8 @@ import com.sunyo.wlpt.message.bus.service.domain.*; | @@ -8,9 +8,8 @@ import com.sunyo.wlpt.message.bus.service.domain.*; | ||
| 8 | import com.sunyo.wlpt.message.bus.service.response.ResultJson; | 8 | import com.sunyo.wlpt.message.bus.service.response.ResultJson; |
| 9 | import com.sunyo.wlpt.message.bus.service.service.BusServerService; | 9 | import com.sunyo.wlpt.message.bus.service.service.BusServerService; |
| 10 | import com.sunyo.wlpt.message.bus.service.service.VirtualHostService; | 10 | import com.sunyo.wlpt.message.bus.service.service.VirtualHostService; |
| 11 | -import com.sunyo.wlpt.message.bus.service.utils.EncryptionUtils; | 11 | +import com.sunyo.wlpt.message.bus.service.utils.AESUtils; |
| 12 | import lombok.extern.slf4j.Slf4j; | 12 | import lombok.extern.slf4j.Slf4j; |
| 13 | -import org.springframework.beans.factory.annotation.Value; | ||
| 14 | import org.springframework.stereotype.Component; | 13 | import org.springframework.stereotype.Component; |
| 15 | 14 | ||
| 16 | import javax.annotation.Resource; | 15 | import javax.annotation.Resource; |
| @@ -32,21 +31,6 @@ public class RabbitUtils { | @@ -32,21 +31,6 @@ public class RabbitUtils { | ||
| 32 | @Resource | 31 | @Resource |
| 33 | private BusServerService busServerService; | 32 | private BusServerService busServerService; |
| 34 | 33 | ||
| 35 | - @Value("${spring.rabbitmq.host}") | ||
| 36 | - private String host; | ||
| 37 | - | ||
| 38 | - @Value("${spring.rabbitmq.port}") | ||
| 39 | - private int port; | ||
| 40 | - | ||
| 41 | - @Value("${spring.rabbitmq.username}") | ||
| 42 | - private String username; | ||
| 43 | - | ||
| 44 | - @Value("${spring.rabbitmq.password}") | ||
| 45 | - private String password; | ||
| 46 | - | ||
| 47 | - @Value("${spring.rabbitmq.virtual-host}") | ||
| 48 | - private String vHost; | ||
| 49 | - | ||
| 50 | /** | 34 | /** |
| 51 | * 关闭连接与通道 | 35 | * 关闭连接与通道 |
| 52 | * | 36 | * |
| @@ -70,37 +54,15 @@ public class RabbitUtils { | @@ -70,37 +54,15 @@ public class RabbitUtils { | ||
| 70 | /** | 54 | /** |
| 71 | * 获取 rabbitMq 的连接,重载 | 55 | * 获取 rabbitMq 的连接,重载 |
| 72 | */ | 56 | */ |
| 73 | - public Connection getConnection() throws IOException, TimeoutException | ||
| 74 | - { | ||
| 75 | - // 定义连接工厂 | ||
| 76 | - ConnectionFactory factory = new ConnectionFactory(); | ||
| 77 | - // 设置服务地址 | ||
| 78 | - factory.setHost(host); | ||
| 79 | - // 端口,amqp协议 端口 类似与mysql的3306 | ||
| 80 | - factory.setPort(port); | ||
| 81 | - // 设置账号信息,用户名、密码、vhost | ||
| 82 | - factory.setVirtualHost(vHost); | ||
| 83 | - factory.setUsername(username); | ||
| 84 | - factory.setPassword(password); | ||
| 85 | - // 通过工程获取连接 | ||
| 86 | - Connection connection = factory.newConnection(); | ||
| 87 | - return connection; | ||
| 88 | - } | ||
| 89 | - | ||
| 90 | - /** | ||
| 91 | - * 获取 rabbitMq 的连接,重载 | ||
| 92 | - */ | ||
| 93 | public Connection getConnection(String serverIp, Integer serverPort, String virtualHostName, String superUsername, String superPassword) | 57 | public Connection getConnection(String serverIp, Integer serverPort, String virtualHostName, String superUsername, String superPassword) |
| 94 | throws IOException, TimeoutException | 58 | throws IOException, TimeoutException |
| 95 | { | 59 | { |
| 96 | - String base = EncryptionUtils.decryptBase64(superPassword); | ||
| 97 | - String[] split = base.split("\\."); | ||
| 98 | ConnectionFactory factory = new ConnectionFactory(); | 60 | ConnectionFactory factory = new ConnectionFactory(); |
| 99 | factory.setHost(serverIp); | 61 | factory.setHost(serverIp); |
| 100 | factory.setPort(serverPort); | 62 | factory.setPort(serverPort); |
| 101 | factory.setVirtualHost(virtualHostName); | 63 | factory.setVirtualHost(virtualHostName); |
| 102 | factory.setUsername(superUsername); | 64 | factory.setUsername(superUsername); |
| 103 | - factory.setPassword(split[split.length - 1]); | 65 | + factory.setPassword(AESUtils.decrypt(superPassword)); |
| 104 | Connection connection = factory.newConnection(); | 66 | Connection connection = factory.newConnection(); |
| 105 | return connection; | 67 | return connection; |
| 106 | } | 68 | } |
| @@ -7,7 +7,7 @@ import com.sunyo.wlpt.message.bus.service.mapper.BusServerMapper; | @@ -7,7 +7,7 @@ import com.sunyo.wlpt.message.bus.service.mapper.BusServerMapper; | ||
| 7 | import com.sunyo.wlpt.message.bus.service.mapper.UserInfoMapper; | 7 | import com.sunyo.wlpt.message.bus.service.mapper.UserInfoMapper; |
| 8 | import com.sunyo.wlpt.message.bus.service.rabbit.utils.RabbitUtils; | 8 | import com.sunyo.wlpt.message.bus.service.rabbit.utils.RabbitUtils; |
| 9 | import com.sunyo.wlpt.message.bus.service.service.*; | 9 | import com.sunyo.wlpt.message.bus.service.service.*; |
| 10 | -import com.sunyo.wlpt.message.bus.service.utils.EncryptionUtils; | 10 | +import com.sunyo.wlpt.message.bus.service.utils.AESUtils; |
| 11 | import org.springframework.stereotype.Service; | 11 | import org.springframework.stereotype.Service; |
| 12 | import org.springframework.transaction.annotation.Propagation; | 12 | import org.springframework.transaction.annotation.Propagation; |
| 13 | import org.springframework.transaction.annotation.Transactional; | 13 | import org.springframework.transaction.annotation.Transactional; |
| @@ -107,9 +107,9 @@ public class BusServerServiceImpl implements BusServerService { | @@ -107,9 +107,9 @@ public class BusServerServiceImpl implements BusServerService { | ||
| 107 | @Override | 107 | @Override |
| 108 | public int insertSelective(BusServer server) | 108 | public int insertSelective(BusServer server) |
| 109 | { | 109 | { |
| 110 | - // 超级用户的密码,设计规则(超级用户名+.+超级用户密码) | ||
| 111 | - String key = server.getSuperUsername() + "." + server.getSuperPassword(); | ||
| 112 | - server.setSuperPassword(EncryptionUtils.encryptBase64(key)); | 110 | + // 超级用户的密码,使用AES加密 |
| 111 | + String superPassword = server.getSuperPassword(); | ||
| 112 | + server.setSuperPassword(AESUtils.encrypt(superPassword)); | ||
| 113 | return busServerMapper.insertSelective(server); | 113 | return busServerMapper.insertSelective(server); |
| 114 | } | 114 | } |
| 115 | 115 |
| 1 | +package com.sunyo.wlpt.message.bus.service.utils; | ||
| 2 | + | ||
| 3 | +import lombok.extern.slf4j.Slf4j; | ||
| 4 | + | ||
| 5 | +import javax.crypto.Cipher; | ||
| 6 | +import javax.crypto.SecretKey; | ||
| 7 | +import javax.crypto.spec.SecretKeySpec; | ||
| 8 | +import java.util.Base64; | ||
| 9 | + | ||
| 10 | + | ||
| 11 | +/** | ||
| 12 | + * @author 子诚 | ||
| 13 | + * Description:对称加密->AES | ||
| 14 | + * 时间:2020/8/24 17:47 | ||
| 15 | + */ | ||
| 16 | +@Slf4j | ||
| 17 | +public class AESUtils { | ||
| 18 | + | ||
| 19 | +// public static void main(String[] args) | ||
| 20 | +// { | ||
| 21 | +// String encrypt = encrypt("vmvnv1v2"); | ||
| 22 | +// System.out.println(encrypt); | ||
| 23 | +// } | ||
| 24 | + | ||
| 25 | + /** | ||
| 26 | + * 自定义的默认的秘钥(128比特位,即长度为16) | ||
| 27 | + */ | ||
| 28 | + private static final String KEY = "1234shangyou4321"; | ||
| 29 | + | ||
| 30 | + /** | ||
| 31 | + * 定义AES加密逻辑,加密 | ||
| 32 | + */ | ||
| 33 | + public static String encrypt(String content) | ||
| 34 | + { | ||
| 35 | + String encrypt = encrypt(content, KEY); | ||
| 36 | + return encrypt; | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + /** | ||
| 40 | + * 定义AES解密逻辑,解密 | ||
| 41 | + * 与加密逻辑对应 | ||
| 42 | + */ | ||
| 43 | + public static String decrypt(String content) | ||
| 44 | + { | ||
| 45 | + String decrypt = decrypt(content, KEY); | ||
| 46 | + return decrypt; | ||
| 47 | + } | ||
| 48 | + | ||
| 49 | + /** | ||
| 50 | + * AES加密 | ||
| 51 | + * | ||
| 52 | + * @param content | ||
| 53 | + * @return | ||
| 54 | + */ | ||
| 55 | + public static String encrypt(String content, String securityKey) | ||
| 56 | + { | ||
| 57 | + try { | ||
| 58 | + SecretKey key = new SecretKeySpec(securityKey.getBytes("UTF-8"), "AES"); | ||
| 59 | + Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); | ||
| 60 | + cipher.init(Cipher.ENCRYPT_MODE, key); | ||
| 61 | + return new String(Base64.getEncoder().encode(cipher.doFinal(content.getBytes("utf-8")))); | ||
| 62 | + } catch (Exception e) { | ||
| 63 | + if (log.isDebugEnabled()) { | ||
| 64 | + e.printStackTrace(); | ||
| 65 | + } | ||
| 66 | + } | ||
| 67 | + return content; | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + /** | ||
| 71 | + * AES解密 | ||
| 72 | + * | ||
| 73 | + * @param content | ||
| 74 | + * @return | ||
| 75 | + */ | ||
| 76 | + public static String decrypt(String content, String securityKey) | ||
| 77 | + { | ||
| 78 | + try { | ||
| 79 | + SecretKey key = new SecretKeySpec(securityKey.getBytes("UTF-8"), "AES"); | ||
| 80 | + Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); | ||
| 81 | + cipher.init(Cipher.DECRYPT_MODE, key); | ||
| 82 | + return new String(cipher.doFinal(Base64.getDecoder().decode(content)), "utf-8"); | ||
| 83 | + } catch (Exception e) { | ||
| 84 | + if (log.isDebugEnabled()) { | ||
| 85 | + e.printStackTrace(); | ||
| 86 | + } | ||
| 87 | + } | ||
| 88 | + return content; | ||
| 89 | + } | ||
| 90 | +} |
-
请 注册 或 登录 后发表评论