首页 » SEO优化 » phpbaav技巧_SpringBoot集成FastDFSNginx整合基于Token的防盗链

phpbaav技巧_SpringBoot集成FastDFSNginx整合基于Token的防盗链

duote123 2024-12-05 0

扫一扫用手机浏览

文章目录 [+]

// EnableFastdfsClient.java@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Import(FastdfsAutoConfiguration.class)@Documentedpublic @interface EnableFastdfsClient {}

下面是干系的自动配置类:

// FastdfsAutoConfiguration.java@Configuration@EnableConfigurationProperties(FastdfsProperties.class)public class FastdfsAutoConfiguration { @Autowired private FastdfsProperties fastdfsProperties; @Bean @ConditionalOnMissingBean(FastdfsClientService.class) public FastdfsClientService fastdfsClientService() throws Exception { return new FastdfsClientService(fastdfsProperties); }}

创建干系的工厂类:

phpbaav技巧_SpringBoot集成FastDFSNginx整合基于Token的防盗链

// StorageClientFactory.java// 用于创建连接工具的工厂类public class StorageClientFactory implements PooledObjectFactory<StorageClient> { @Override public PooledObject<StorageClient> makeObject() throws Exception { TrackerClient client = new TrackerClient(); TrackerServer server = client.getConnection(); return new DefaultPooledObject<>(new StorageClient(server, null)); } @Override public void destroyObject(PooledObject<StorageClient> p) throws Exception { p.getObject().getTrackerServer().close(); } @Override public boolean validateObject(PooledObject<StorageClient> p) { return false; } @Override public void activateObject(PooledObject<StorageClient> p) throws Exception { } @Override public void passivateObject(PooledObject<StorageClient> p) throws Exception { }}

Properties类用来映射application.properties或者application.yml配置文件:

phpbaav技巧_SpringBoot集成FastDFSNginx整合基于Token的防盗链
(图片来自网络侵删)

// FastdfsProperties.java@ConfigurationProperties(prefix = \"大众fastdfs\公众)public class FastdfsProperties { // 连接超时时间 // 网络超时时间 // 字符集编码 // 是否利用Token // Token加密密钥 // 跟踪器IP地址,多个利用分号隔开 // 连接池的连接工具最大个数 // 连接池的最大空闲工具个数 // 连接池的最小空闲工具个数 // Nginx做事器IP,多个利用分号分割 // 获取连接工具时可忍受的等待时长(毫秒) private String connectTimeout = \公众5\公众; private String networkTimeout = \"大众30\"大众; private String charset = \"大众UTF-8\"大众; private String httpAntiStealToken = \"大众false\"大众; private String httpSecretKey = \"大众\"大众; private String httpTrackerHttpPort = \"大众\公众; private String trackerServers = \"大众\"大众; private String connectionPoolMaxTotal = \公众18\"大众; private String connectionPoolMaxIdle = \"大众18\"大众; private String connectionPoolMinIdle = \"大众2\"大众; private String nginxServers = \"大众\"大众; // 须要创建干系的Setter和Getter方法}

在Service类中封装方法, 下面仅展示3个常用的方法:

// FastdfsClientSerivce.javapublic class FastdfsClientService { // SpringBoot加载的配置文件 // 连接池配置项 // 转换后的配置条款 // 连接池 // Nginx做事器地址 private FastdfsProperties fdfsProp; private GenericObjectPoolConfig config; private Properties prop; private GenericObjectPool<StorageClient> pool; private String[] nginxServers; private Logger logger; public FastdfsClientService(FastdfsProperties fdfsProp) throws Exception { this.fdfsProp = fdfsProp; this.logger = LoggerFactory.getLogger(getClass()); init(); create(); info(); } / 初始化全局客户端 / private void init() throws Exception { this.prop = new Properties(); this.logger.info(\"大众FastDFS: reading config file...\"大众); this.logger.info(\"大众FastDFS: fastdfs.connect_timeout_in_seconds=\公众 + this.fdfsProp.getConnectTimeout()); this.logger.info(\"大众FastDFS: fastdfs.network_timeout_in_seconds=\"大众 + this.fdfsProp.getNetworkTimeout()); this.logger.info(\"大众FastDFS: fastdfs.charset=\公众 + this.fdfsProp.getCharset()); this.logger.info(\公众FastDFS: fastdfs.http_anti_steal_token=\"大众 + this.fdfsProp.getHttpAntiStealToken()); this.logger.info(\公众FastDFS: fastdfs.http_secret_key=\"大众 + this.fdfsProp.getHttpSecretKey()); this.logger.info(\公众FastDFS: fastdfs.http_tracker_http_port=\"大众 + this.fdfsProp.getHttpTrackerHttpPort()); this.logger.info(\"大众FastDFS: fastdfs.tracker_servers=\公众 + this.fdfsProp.getTrackerServers()); this.logger.info(\"大众FastDFS: fastdfs.connection_pool_max_total=\"大众 + this.fdfsProp.getConnectionPoolMaxTotal()); this.logger.info(\"大众FastDFS: fastdfs.connection_pool_max_idle=\"大众 + this.fdfsProp.getConnectionPoolMaxIdle()); this.logger.info(\"大众FastDFS: fastdfs.connection_pool_min_idle=\"大众 + this.fdfsProp.getConnectionPoolMinIdle()); this.logger.info(\"大众FastDFS: fastdfs.nginx_servers=\公众 + this.fdfsProp.getNginxServers()); this.prop.put(\"大众fastdfs.connect_timeout_in_seconds\公众, this.fdfsProp.getConnectTimeout()); this.prop.put(\"大众fastdfs.network_timeout_in_seconds\公众, this.fdfsProp.getNetworkTimeout()); this.prop.put(\"大众fastdfs.charset\公众, this.fdfsProp.getCharset()); this.prop.put(\公众fastdfs.http_anti_steal_token\公众, this.fdfsProp.getHttpAntiStealToken()); this.prop.put(\公众fastdfs.http_secret_key\公众, this.fdfsProp.getHttpSecretKey()); this.prop.put(\"大众fastdfs.http_tracker_http_port\"大众, this.fdfsProp.getHttpTrackerHttpPort()); this.prop.put(\"大众fastdfs.tracker_servers\"大众, this.fdfsProp.getTrackerServers()); ClientGlobal.initByProperties(this.prop); } / 显示初始化信息 / private void info() { this.logger.info(\"大众FastDFS parameter: ConnectionPoolMaxTotal ==> \"大众 + this.pool.getMaxTotal()); this.logger.info(\公众FastDFS parameter: ConnectionPoolMaxIdle ==> \"大众 + this.pool.getMaxIdle()); this.logger.info(\"大众FastDFS parameter: ConnectionPoolMinIdle ==> \"大众 + this.pool.getMinIdle()); this.logger.info(\"大众FastDFS parameter: NginxServer ==> \"大众 + Arrays.toString(this.nginxServers)); this.logger.info(ClientGlobal.configInfo()); } / 创建连接池 / private void create() { this.config = new GenericObjectPoolConfig(); this.logger.info(\"大众FastDFS Client: Creating connection pool...\"大众); this.config.setMaxTotal(Integer.parseInt(this.fdfsProp.getConnectionPoolMaxTotal())); this.config.setMaxIdle(Integer.parseInt(this.fdfsProp.getConnectionPoolMaxIdle())); this.config.setMinIdle(Integer.parseInt(this.fdfsProp.getConnectionPoolMinIdle())); StorageClientFactory factory = new StorageClientFactory(); this.pool = new GenericObjectPool<StorageClient>(factory, this.config); this.nginxServers = this.fdfsProp.getNginxServers().split(\"大众,\"大众); } / Nginx做事器负载均衡算法 @param servers 做事器地址 @param address 客户端IP地址 @return 可用的做事器地址 / private String getNginxServer(String[] servers, String address) { int size = servers.length; int i = address.hashCode(); int index = abs(i % size); return servers[index]; } / 带有防盗链的下载 @param fileGroup 文件组名 @param remoteFileName 远程文件名称 @param clientIpAddress 客户端IP地址 @return 完全的URL地址 / public String autoDownloadWithToken(String fileGroup, String remoteFileName, String clientIpAddress) throws Exception { int ts = (int) (System.currentTimeMillis() / 1000); String token = ProtoCommon.getToken(remoteFileName, ts, ClientGlobal.getG_secret_key()); String nginx = this.getNginxServer(this.nginxServers, clientIpAddress); return \"大众http://\"大众 + nginx + \公众/\公众 + fileGroup + \"大众/\"大众 + remoteFileName + \"大众?token=\公众 + token + \"大众&ts=\"大众 + ts; } / 上传文件,适宜上传图片 @param buffer 字节数组 @param ext 扩展名 @return 文件组名和ID / public String[] autoUpload(byte[] buffer, String ext) throws Exception { String[] upload = this.upload(buffer, ext, null); return upload; } / 不带防盗链的下载,如果开启防盗链会导致该方法抛出非常 @param fileGroup 文件组名 @param remoteFileName 远程文件ID @param clientIpAddress 客户端IP地址,根据客户端IP来分配Nginx做事器 @return 完全的URL地址 / public String autoDownloadWithoutToken(String fileGroup, String remoteFileName, String clientIpAddress) throws Exception { if (ClientGlobal.getG_anti_steal_token()) { this.logger.error(\"大众FastDFS Client: You've turned on Token authentication.\公众); throw new Exception(\"大众You've turned on Token authentication.\公众); } String nginx = this.getNginxServer(this.nginxServers, clientIpAddress); return \"大众http://\公众 + nginx + fileGroup + \公众/\公众 + remoteFileName; } // 后面还有好多方法,就不一一展示了}

为了在IDEA中利用便捷的配置提示功能,我们须要创建元数据文件(resources/spring-configuration-metadata.json):

{ \公众groups\"大众: [ { \"大众name\"大众: \公众fastdfs\"大众, \公众type\公众: \公众com.bluemiaomiao.properties.FastdfsProperties\公众, \"大众sourceType\"大众: \"大众com.bluemiaomiao.properties.FastdfsProperties\"大众 } ], \"大众properties\"大众: [ { \公众name\公众: \"大众connectTimeout\公众, \"大众type\"大众: \公众java.lang.String\公众, \"大众sourceType\"大众: \"大众com.bluemiaomiao.properties.FastdfsProperties\"大众, \"大众defaultValue\"大众: \"大众5\"大众 }, { \公众name\公众: \"大众networkTimeout\"大众, \公众type\"大众: \"大众java.lang.String\"大众, \"大众sourceType\"大众: \"大众com.bluemiaomiao.properties.FastdfsProperties\"大众, \"大众defaultValue\公众: \公众30\公众 }, { \"大众name\公众: \"大众charset\"大众, \"大众type\公众: \"大众java.lang.String\"大众, \"大众defaultValue\"大众: \"大众UTF-8\"大众 }, { \"大众name\公众: \"大众httpAntiStealToken\"大众, \"大众type\公众: \公众java.lang.String\"大众, \"大众sourceType\"大众: \"大众com.bluemiaomiao.properties.FastdfsProperties\公众, \公众defaultValue\公众: \公众false\"大众 }, { \"大众name\"大众: \公众httpSecretKey\"大众, \"大众type\公众: \"大众java.lang.String\"大众, \"大众sourceType\"大众: \公众com.bluemiaomiao.properties.FastdfsProperties\"大众 }, { \"大众name\"大众: \公众httpTrackerHttpPort\"大众, \"大众type\"大众: \公众java.lang.Integer\"大众, \"大众sourceType\公众: \公众com.bluemiaomiao.properties.FastdfsProperties\"大众 }, { \"大众name\"大众: \公众trackerServers\"大众, \"大众type\"大众: \公众java.lang.String\公众, \"大众sourceType\公众: \公众com.bluemiaomiao.properties.FastdfsProperties\公众 }, { \"大众name\"大众: \"大众connectionPoolMaxTotal\"大众, \"大众type\公众: \公众java.lang.Integer\公众, \"大众sourceType\"大众: \公众com.bluemiaomiao.properties.FastdfsProperties\公众, \公众defaultValue\公众: \公众18\公众 }, { \公众name\"大众: \公众connectionPoolMaxIdle\"大众, \公众type\"大众: \"大众java.lang.Integer\"大众, \"大众sourceType\公众: \公众com.bluemiaomiao.properties.FastdfsProperties\"大众, \公众defaultValue\"大众: \"大众18\公众 }, { \公众name\"大众: \"大众connectionPoolMinIdle\公众, \公众type\公众: \"大众java.lang.Integer\"大众, \"大众sourceType\"大众: \公众com.bluemiaomiao.properties.FastdfsProperties\"大众, \"大众defaultValue\"大众: \公众2\"大众 }, { \"大众name\公众: \"大众nginxServers\公众, \"大众type\公众: \"大众java.lang.String\"大众, \"大众sourceType\公众: \"大众com.bluemiaomiao.properties.FastdfsProperties\公众 } ], \"大众hints\"大众: [ { \"大众name\"大众: \"大众http_anti_steal_token\公众, \公众values\"大众: [ { \"大众value\"大众: \公众false\"大众 }, { \"大众value\"大众: \公众true\公众 } ] } ]}将自定义starter添加到项目创建SpringBoot项目,勾选Web选项,版本选择1.5.20进入场景启动器的项目目录实行mvn clean install 将其安装到本地在POM.xml文件中添加依赖:

<dependency> <groupId>com.bluemiaomiao</groupId> <artifactId>fastdfs-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version></dependency>

记得开启IDEA的自动导入功能

创建配置文件application.properties

fastdfs.nginx-servers=192.168.80.2:8000,192.168.80.3:8000,192.168.80.4:8000fastdfs.tracker-servers=192.168.80.2:22122,192.168.80.3:22122,192.168.80.4:22122fastdfs.http-secret-key=2scPwMPctXhbLVOYB0jyuyQzytOofmFCBIYe65n56PPYVWrntxzLIDbPdvDDLJM8QHhKxSGWTcr+9VdG3yptkwfastdfs.http-anti-steal-token=truefastdfs.http-tracker-http-port=8080fastdfs.network-timeout=30fastdfs.connect-timeout=5fastdfs.connection-pool-max-idle=18fastdfs.connection-pool-min-idle=2fastdfs.connection-pool-max-total=18fastdfs.charset=UTF-8

或者利用application.yml

fastdfs: charset: UTF-8 connect-timeout: 5 http-secret-key: 2scPwMPctXhbLVOYB0jyuyQzytOofmFCBIYe65n56PPYVWrntxzLIDbPdvDDLJM8QHhKxSGWTcr+9VdG3yptkw network-timeout: 30 http-anti-steal-token: true http-tracker-http-port: 8080 connection-pool-max-idle: 20 connection-pool-max-total: 20 connection-pool-min-idle: 2 nginx-servers: 192.168.80.2:8000,192.168.80.3:8000,192.168.80.4:8000 tracker-servers: 192.168.80.2:22122,192.168.80.3:22122,192.168.80.4:22122创建掌握器类测试方法

// controllers.DownloadController.java@Controller@RequestMapping(value = \公众/download\公众)public class DownloadController { @Autowired private FastdfsClientService service; @ResponseBody @RequestMapping(value = \"大众/image\公众) public String image() throws Exception { // 之前上传过的数据,实际运用处景该当利用SQL数据库来存储 return service.autoDownloadWithToken(\公众group1\"大众, \"大众M00/00/00/wKhQA1ysjSGAPjXbAAVFOL7FJU4.tar.gz\"大众, \公众192.168.80.1\"大众); }}项目主页:https://github.com/bluemiaomiao/fastdfs-spring-boot-starter海内项目主页:https://gitee.com/bluemiaomiao/fastdfs-spring-boot-starter

标签:

相关文章

php版74技巧_PHP 74类型属性实例详解

概述PHP 7.4为了增强类型新增加了类型化的类属性,并对PHP的类型系统进行了重大改进。当然这些变革都是可选功能,是完备向前对老...

SEO优化 2024-12-07 阅读0 评论0

php拜访值技巧_PHP 访问文件

定义和用法fopen( 函数打开文件或者 URL。如果打开失落败,本函数返回 FALSE。语法fopen(filename,mo...

SEO优化 2024-12-07 阅读0 评论0