首页 文章 精选 留言 我的

精选列表

搜索[web开发],共10000篇文章
优秀的个人博客,低调大师

Eclipse下Maven新建项目、自动打依赖jar包(包含普通项目和Web项目)

当我们无法从本地仓库找到需要的构件的时候,就会从远程仓库下载构件至本地仓库。一般地,对于每个人来说,书房只有一个,但外面的书店有很多,类似第,对于Maven来说,每个用户只有一个本地仓库,但可以配置访问很多远程仓库。 Eclipse *版本 Eclipse *下载 强烈推荐书籍:Maven实战 许晓斌著。目前是第一版 注意:最新版本的Eclipse代号Mars,已经直接集成了Maven,所以无需安装m2Eclipse插件。 Eclipse下新建Maven项目 1、下载Maven安装包 进入Maven官网的下载页面:http://maven.apache.org/download.cgi,如下图所示: 选择当前最新版本:"apache-maven-3.3.9-bin.zip",下载到本地,解压缩到本地磁盘下。 2、配置 2.1 环境变量的配置 2.2 修改默认的本地仓库位置 进入Maven安装目录下的conf子目录中,打开settings.xml进行配置修改。 <!-- localRepository | The path to the local repository maven will use to store artifacts. | | Default: ${user.home}/.m2/repository <localRepository>/path/to/local/repo</localRepository> --> <localRepository>D:/SoftWare/maven/repository</localRepository> Maven默认的本地仓库位置是当前用户工作目录下的".m2/repository",使用过程中这个目录里的文件会比较多,占用空间越来越大。一般建议更换到其它磁盘目录下。如下配置,就把默认的本地仓库更改到自己创建的目录。 如下图所示的localRepository标签中设置,就把默认的本地仓库更改到D:/SoftWare/maven/repository这个目录(这个目录结构需要自己创建好)。 2.3修改默认的中央仓库镜像 Maven默认的中央仓库里的文件不全。所以,都需要自行添加其它的镜像地址。在settings.xml文件中的“<mirrors>”标签里添加如下内容: 其实,是如下 http://mirrors.ibiblio.org/maven2/ 注意啦!有时候,上面这样配置,不稳定,我这里给大家分享一个稳定的setting.xml !!!(优先) <?xml version="1.0" encoding="UTF-8"?> <!--Licensed to the Apache Software Foundation (ASF) under oneor more contributor license agreements. See the NOTICE filedistributed with this work for additional informationregarding copyright ownership. The ASF licenses this fileto you under the Apache License, Version 2.0 (the"License"); you may not use this file except in compliancewith the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing,software distributed under the License is distributed on an"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANYKIND, either express or implied. See the License for thespecific language governing permissions and limitationsunder the License.--> <!--| This is the configuration file for Maven. It can be specified at two levels:|| 1. User Level. This settings.xml file provides configuration for a single user,| and is normally provided in ${user.home}/.m2/settings.xml.|| NOTE: This location can be overridden with the CLI option:|| -s /path/to/user/settings.xml|| 2. Global Level. This settings.xml file provides configuration for all Maven| users on a machine (assuming they're all using the same Maven| installation). It's normally provided in| ${maven.home}/conf/settings.xml.|| NOTE: This location can be overridden with the CLI option:|| -gs /path/to/global/settings.xml|| The sections in this sample file are intended to give you a running start at| getting the most out of your Maven installation. Where appropriate, the default| values (values used when the setting is not specified) are provided.||--><settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"><!-- localRepository| The path to the local repository maven will use to store artifacts.|| Default: ${user.home}/.m2/repository<localRepository>/path/to/local/repo</localRepository>--> <localRepository>D:/SoftWare/maven/repository</localRepository><!-- interactiveMode| This will determine whether maven prompts you when it needs input. If set to false,| maven will use a sensible default value, perhaps based on some other setting, for| the parameter in question.|| Default: true<interactiveMode>true</interactiveMode>--> <!-- offline| Determines whether maven should attempt to connect to the network when executing a build.| This will have an effect on artifact downloads, artifact deployment, and others.|| Default: false<offline>false</offline>--> <!-- pluginGroups| This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.| when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers| "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.|--><pluginGroups><!-- pluginGroup| Specifies a further group identifier to use for plugin lookup.<pluginGroup>com.your.plugins</pluginGroup>--></pluginGroups> <!-- proxies| This is a list of proxies which can be used on this machine to connect to the network.| Unless otherwise specified (by system property or command-line switch), the first proxy| specification in this list marked as active will be used.|--><proxies><!-- proxy| Specification for one proxy, to be used in connecting to the network.|<proxy><id>optional</id><active>true</active><protocol>http</protocol><username>proxyuser</username><password>proxypass</password><host>proxy.host.net</host><port>80</port><nonProxyHosts>local.net|some.host.com</nonProxyHosts></proxy>--></proxies> <!-- servers| This is a list of authentication profiles, keyed by the server-id used within the system.| Authentication profiles can be used whenever maven must make a connection to a remote server.|--><servers><!-- server| Specifies the authentication information to use when connecting to a particular server, identified by| a unique name within the system (referred to by the 'id' attribute below).|| NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are| used together.|<server><id>deploymentRepo</id><username>repouser</username><password>repopwd</password></server>--> <!-- Another sample, using keys to authenticate.<server><id>siteServer</id><privateKey>/path/to/private/key</privateKey><passphrase>optional; leave empty if not used.</passphrase></server>--></servers> <!-- mirrors| This is a list of mirrors to be used in downloading artifacts from remote repositories.|| It works like this: a POM may declare a repository to use in resolving certain artifacts.| However, this repository may have problems with heavy traffic at times, so people have mirrored| it to several places.|| That repository definition will have a unique id, so we can create a mirror reference for that| repository, to be used as an alternate download site. The mirror site will be the preferred| server for that repository.|--><mirrors><!-- mirror| Specifies a repository mirror site to use instead of a given repository. The repository that| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.|<mirror><id>mirrorId</id><mirrorOf>repositoryId</mirrorOf><name>Human Readable Name for this Mirror.</name><url>http://my.repository.com/repo/path</url></mirror>--><mirror><id>osc</id><mirrorOf>central</mirrorOf><url>http://maven.aliyun.com/nexus/content/repositories/central</url></mirror><mirror><id>osc_thirdparty</id><mirrorOf>thirdparty</mirrorOf><url>http://maven.aliyun.com/nexus/content/repositories/central</url></mirror></mirrors> <!-- profiles| This is a list of profiles which can be activated in a variety of ways, and which can modify| the build process. Profiles provided in the settings.xml are intended to provide local machine-| specific paths and repository locations which allow the build to work in the local environment.|| For example, if you have an integration testing plugin - like cactus - that needs to know where| your Tomcat instance is installed, you can provide a variable here such that the variable is| dereferenced during the build process to configure the cactus plugin.|| As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles| section of this document (settings.xml) - will be discussed later. Another way essentially| relies on the detection of a system property, either matching a particular value for the property,| or merely testing its existence. Profiles can also be activated by JDK version prefix, where a| value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.| Finally, the list of active profiles can be specified directly from the command line.|| NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact| repositories, plugin repositories, and free-form properties to be used as configuration| variables for plugins in the POM.||--><profiles><!-- profile| Specifies a set of introductions to the build process, to be activated using one or more of the| mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>| or the command line, profiles have to have an ID that is unique.|| An encouraged best practice for profile identification is to use a consistent naming convention| for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.| This will make it more intuitive to understand what the set of introduced profiles is attempting| to accomplish, particularly when you only have a list of profile id's for debug.|| This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.<profile><id>jdk-1.4</id> <activation><jdk>1.4</jdk></activation> <repositories><repository><id>jdk14</id><name>Repository for JDK 1.4 builds</name><url>http://www.myhost.com/maven/jdk14</url><layout>default</layout><snapshotPolicy>always</snapshotPolicy></repository></repositories></profile>--> <!--| Here is another profile, activated by the system property 'target-env' with a value of 'dev',| which provides a specific path to the Tomcat instance. To use this, your plugin configuration| might hypothetically look like:|| ...| <plugin>| <groupId>org.myco.myplugins</groupId>| <artifactId>myplugin</artifactId>|| <configuration>| <tomcatLocation>${tomcatPath}</tomcatLocation>| </configuration>| </plugin>| ...|| NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to| anything, you could just leave off the <value/> inside the activation-property.|<profile><id>env-dev</id> <activation><property><name>target-env</name><value>dev</value></property></activation> <properties><tomcatPath>/path/to/tomcat/instance</tomcatPath></properties></profile>--><profile><id>jdk-1.4</id><activation><jdk>1.4</jdk></activation><repositories> <repository><id>nexus</id><name>local private nexus</name><url>http://maven.aliyun.com/nexus/content/repositories/central</url><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots></repository></repositories><pluginRepositories> <pluginRepository><id>nexus</id><name>local private nexus</name><url>http://maven.aliyun.com/nexus/content/repositories/central</url><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots></pluginRepository></pluginRepositories></profile><profile><id>osc</id><activation><activeByDefault>true</activeByDefault></activation><repositories><repository><id>osc</id><url>http://maven.aliyun.com/nexus/content/repositories/central</url></repository><repository><id>osc_thirdparty</id><url>http://maven.aliyun.com/nexus/content/repositories/central</url></repository></repositories><pluginRepositories><pluginRepository><id>osc</id><url>http://maven.aliyun.com/nexus/content/repositories/central</url></pluginRepository> </pluginRepositories></profile></profiles> <!-- activeProfiles| List of profiles that are active for all builds.|<activeProfiles><activeProfile>alwaysActiveProfile</activeProfile><activeProfile>anotherAlwaysActiveProfile</activeProfile></activeProfiles>--></settings> 保存所做的修改,同时还需要把这个settings.xml文件复制一份到“D:\SoftWare\maven”目录下。 3、eclipse里的Maven插件安装 Eclipse默认不支持Maven。需要给它添加m2eclipse插件。下面是具体的操作步骤。 3.1、插件安装 打开Eclipse的Help->Install New Software,如下图所示: 选择“Add..”按钮,又会弹出如下对话框: 这个对话框就是用于添加一个插件地址的。在“Name”对应的输入框里输入该操作的一个标识名。在“Location”对应的输入框里输入这个插件的安装地址。注:Maven的Eclipse插件地址为:http://download.eclipse.org/technology/m2e/releases。输入后,如下图所示: Name: m2e Location: http://download.eclipse.org/technology/m2e/releases 输入完成后,点击右下角的“OK”按钮,就会弹出如下对话框: MARS版本 我这里,就,已经安装过了。 这一步,有可能会 注意:安装maven时一定要注意版本匹配问题:下面就是我安装时遇到的问题 首先安装maven时遇到Missing requirement: m2e logback configuration 1.7.0.20160603-1933 (org.eclipse.m2e.logback.configur。。。的问题。 这是由于eclipse和maven插件版本不匹配,我的eclipse版本为如下,只能安装m2e -http://download.eclipse.org/technology/m2e/releases/1.3的版本,高版本就报错 推荐用MARS版本!!! 注意,有些版本,我已测试过,如,NEON版本 会发生上述的错误。 这里需要选择想要安装的插件的详细内容。选中“Maven Integration for Eclipse”前面的复选框。如下图所示: 选择完成后,点击右下方的“Next>”按钮进行安装。安装成功后,会提示需要重启Eclipse来生效这次配置。重启之后,这个Eclipse就可以使用m2eclipse插件功能了。 需要一段时间 自动重启 3.2、Eclipse下对maven进行配置 插件安装好之后,还需要进行一些配置才能够开始使用Maven的功能。 3.2.1、设置m2eclipse插件所关联的Maven程序 需要修改配置:首先选择Window->Preferences,弹出如下对话框 在这个对话框左边的导航栏中展开“Maven”目录结点,并选择“Installations”子节点,如下图所示: 这里需要关联上对应的Maven安装程序。具体操作是,在右边面板中选择“Add…”按钮,会弹出如下选择Maven安装目录的选择对话框: 通过这个对话框来选择Maven的具体安装目录。选择好之后就点击“确定”按钮。如下图所示: 3.2.2、设置自定义的本地仓库 选择如上图对话窗口左边菜单树 “Maven”节点下的“UserSettings”子节点,它默认的配置文件是“C:\Documents and Settings\csdn\.m2\settings.xml”,如下图所示: 这边需要修改为我们自定义仓库位置下的settings.xml文件。具体操作是,点击“Browse…”按钮来选择上一步骤修改的本地仓库目录下的settings.xml文件,如下图所示: 在上图中选择D:\SoftWare\apache-maven-3.3.9\conf目录下的“settings.xml”文件,点击“打开”按钮来确认刚才的选择,就会回到如下图的对话窗口中: 在上图中点击右下角的“OK”按钮来完成所有的配置修改。 4、Eclipse创建maven项目 在eclipse菜单栏中选择"File"->"News"->"Other"菜单项,就会打开如下对话窗口,在窗口中选择"Maven"节点下"Maven Project"条目,如下图所示,之后跟着下一步。。。就可以将项目创建好了。 我这里,选择other 想说的是,SBT是为scala专门而设计的,但是,一般很多人还是用的是maven。 使用maven-archetype-quickstart 或者 coccoon-22-archetype-webapp 或者 也许,这一步,会碰到如下问题。 解决办法: 或者 Catalog File: http://repo2.maven.org/maven2/archetype-catalog.xml Description: maven catalog 然后,再重新 File -> New -> Maven Project 这里,自己任意取, Group Id: com.zhouls.spark Artifact Id: SparkApps CoreException: Could not calculate build plan: Plugin org.apache.maven.plugins:maven-compiler-plugin:3.1 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-compiler-plugin:jar:3.1: UnresolvableModelException: Failure to transfer org.apache.maven.plugins:maven-plugins:pom:24 from https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. Original error: Could not transfer artifact org.apache.maven.plugins:maven-plugins:pom:24 from/to central (https://repo.maven.apache.org/maven2): timeout 解决方法: 1.找到maven库目录,进入:D:\SoftWare\maven\repository\org\apache\maven\plugins\maven-compiler-plugin\3.1 2. 若3.1这个目录下只有,"maven-compiler-plugin-3.1.pom.lastUpdated" 则需要到http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-compiler-plugin/3.1/,把里面的文件下载下来放到目录3.1下,如果该仓库中没有,那么可以去http://search.maven.org/这个地方下载下来,然后放置到对应的本地仓库中 可见,我这里是不需进行步骤2,直接到步骤3. 3.删除3.1下的maven-compiler-plugin-3.1.pom.lastUpdated文件或3.1目录下的所有文件 4.项目右键-->maven-->Update Dependencies 即,得到解决! 如果以上方法还是无法解决该问题:则打开settings.xml,尝试换个mirror镜像或者干脆采用默认的仓库,即注释掉 删除清空 再重新来一次 这里,我继续,上面的步骤,下来。用了我那个稳定的setting.xml文件,之后,中间不会出现什么问题!!! 把此处默认的jdk,J2SE-1.5,变成我们自己安装的jdk版本(jdk1.7或jdk1.8) 得到 对应,得到 这个pom.xml文件,里有我们工程实际时编写和运行时的依赖和支持。 进入,如下 选择,pom.xml 在修改pom.xml文件之前,Maven Dependencies如下 创建项目之后,pom.xml默认的内容如下 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zhouls.spark</groupId> <artifactId>SparkApps</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>SparkApps</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project> 这一步,最为关键,具体怎么修改pom.xml,是要看自己的需求。 http://mvnrepository.com/ 我这里,因为是spark1.5.2版本。 其他的,类似,这里不多赘述。 转】maven核心,pom.xml详解 在这过程中,最好是去复制,官网。为了避免自己手误或者格式不对!!!可以避免如下面的错误! 修改后的pom.xml内容是 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zhouls.spark</groupId> <artifactId>SparkApps</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>SparkApps</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <junit.version>4.10</junit.version> <spark.version>1.5.2</spark.version> <hadoop.version>2.6.0</hadoop.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-core_2.10</artifactId> <version>${spark.version}</version> </dependency> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-sql_2.10</artifactId> <version>${spark.version}</version> </dependency> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-hive_2.10</artifactId> <version>${spark.version}</version> </dependency> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-streaming_2.10</artifactId> <version>${spark.version}</version> </dependency> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-streaming-kafka_2.10</artifactId> <version>${spark.version}</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>${hadoop.version}</version> </dependency> </dependencies> <build> <sourceDirectory>src/main/java</sourceDirectory> <testSourceDirectory>src/main/test</testSourceDirectory> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass></mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2.1</version> <executions> <execution> <goals> <goal>exec</goal> </goals> </execution> </executions> <configuration> <executable>java</executable> <includeProjectDependencies>true</includeProjectDependencies> <includePluginDependencies>false</includePluginDependencies> <classpathScope>compile</classpathScope> <mainClass>com.zhouls.spark.SparkApps.cores.WordCount</mainClass> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> </project> 正在下载 立马生成好了。可见,Maven自动生成jar包,多么的方便! 其他的一一去解决! 对应地,生成, 我的这里,得到如下 至此,Eclipse下新建Maven项目、自动打依赖jar包。大功告成!!! Eclipse里创建Maven,比较有价值的错误总结 http://blog.csdn.net/afgasdg/article/details/12757433 http://blog.csdn.net/sunnylinner/article/details/52183156 http://www.cnblogs.com/liaojie970/p/5509760.html http://www.cnblogs.com/tenghoo/p/maven_error.html 感谢如下博主: http://www.cnblogs.com/ljdblog/p/5840858.html http://blog.csdn.net/qjyong/article/details/9098213 http://dead-knight.iteye.com/blog/1910783 关于Maven的进一步,深入学习,推荐 本文转自大数据躺过的坑博客园博客,原文链接:http://www.cnblogs.com/zlslch/p/5882567.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

《Web安全之机器学习入门》一 1.4 人工智能在安全领域的应用

1.4 人工智能在安全领域的应用 人工智能在安全领域的应用还属于起步阶段,各大安全公司以及互联网巨头都投入大量的人力物力,试图使用人工智能的技术来颠覆安全这个行业,目前在黄反鉴定、恶意链接、业务风控领域、病毒分析、APT检测方面都取得了不错的进展,典型案例的就是2015年的一次数据挖掘比赛。2015年,微软在Kaggle上发起了一个恶意代码分类的比赛,并提供了超过500G的原始数据。有意思的是,取得第一名的队伍三个人都不是搞安全出身的,所采用的方法与我们常见的方法存在很大不同,展现了机器学习在安全领域的巨大潜力。早期的反病毒软件大都单一的采用特征匹配的方法,简单的利用特征串完成检测。随着恶意代码技术的发展,恶意代码开始在传播过程中进行变形以躲避查杀,此时同一个恶意代码的变种数量急剧提升,形态较本体也发生了较大的变化,反病毒软件已经很

优秀的个人博客,低调大师

如何把一个web集群由HTTP转换为HTTPS(LVS+HAProxy+SSL)

一、环境介绍 接到通知,要求网站由http改为使用https,目前我的网站前端架构如下图所示: 假设我们有两台物理机,每台物理机上面有很多的tomcat容器,前端使用的是haproxy进行的http层负载均衡,再前端我们使用了LVS负载均衡,整个LVS使用的是DR模型。 刚开始我打算把tomcat改成https,设置成之后再设置haproxy的时候,发现haproxy不能再使用负载均衡了,因为SSL是在第四层的,所以这个方案就结束了,下面我就尝试在haproxy层设定SSL,到后端还使用普通的连接。 二、设置步骤 1、概述 如果你的应用使用SSL证书,则需要决定如何在负载均衡器上使用它们。 单服务器的简单配置通常是考虑客户端SSL连接如何被接收请求的服务器解码。由于负载均衡器处在客户端和更多服务器之间,SSL连接解码就成了需要关注的焦点。 2、有两种主要的策略 第一种是我们选择的模式,在haproxy这里设定SSL,这样我们可以继续使用七层负载均衡。SSL连接终止在负载均衡器haproxy ----->解码SSL连接并发送非加密连接到后端应用tomcat,这意味着负载均衡器负责解码SSL连接,这与SSL穿透相反,它是直接向代理服务器发送SSL连接的。 第二种使用SSL穿透,SSL连接在每个tomcat服务器终止,将CPU负载都分散到tomcat服务器。然而,这样做会让你失去增加或修改HTTP报头的能力,因为连接只是简单地从负载均衡器路由到tomcat服务器,这意味着应用服务器会失去获取 X-Forwarded-* 报头的能力,这个报头包含了客户端IP地址、端口和使用的协议。 有两种策略的组合做法,那就是第三种,SSL连接在负载均衡器处终止,按需求调整,然后作为新的SSL连接代理到后台服务器。这可能会提供最大的安全性和发送客户端信息的能力。这样做的代价是更多的CPU能耗和稍复杂一点的配置。 选择哪个策略取决于你及应用的需求。SSL终端为我所见过最典型的策略,但SSL穿透可能会更安全。 3、使用HAProxy作为SSL终端 首先,我们将介绍最典型的解决方案 - SSL 终端。正如前面提到的,我们需要让负载均衡器处理SSL连接。这就意味着要将SSL证书放在负载均衡服务器上。 记住,在生产环境里使用(而不是自签名)的SSL证书,是不会需要你自己来生成或签名 - 你只需要创建证书签名请求 (csr) 并把它交给那个你向它购买证书的机构即可。 首先, 我们创建一份自签名的证书作为示范,并在本地使用同一份证书。 opensslgenrsa-out/etc/haproxy/wzlinux.key2048 opensslreq-new-key/etc/haproxy/wzlinux.key-out/etc/haproxy/wzlinux.csr >CountryName(2lettercode)[AU]:CN >StateorProvinceName(fullname)[Some-State]:Shanghai >LocalityName(eg,city)[]:Shanghai >OrganizationName(eg,company)[InternetWidgitsPtyLtd]:wzlinux >OrganizationalUnitName(eg,section)[]: >CommonName(e.g.serverFQDNorYOURname)[]:www.wzlinux.com >EmailAddress[]: >Pleaseenterthefollowing'extra'attributestobesentwithyourcertificaterequest >Achallengepassword[]: >Anoptionalcompanyname[]: cd/etc/haproxy opensslx509-req-days3655-inwzlinux.csr-signkeywzlinux.key-outwzlinux.crt 这就生成了wzlinux.csr,wzlinux.key和wzlinux.crt文件了。 接着,在创建了证书之后,我们需要创建pem文件。pem文件本质上只是将证书、密钥及证书认证中心证书(可有可无)拼接成一个文件。在我们的例子中,我们只是简单地将证书及密钥文件并以这个顺序拼接在一样来创建wzlinux.pem 文件。这是HAProxy读取SSL证书首选的方式。 catwzlinux.crtwzlinux.key|teewzlinux.pem 当购买真正的证书 时,你不一定会获取拼接后的文件。你可以要自己拼接它们。然而,很多机构也会提供一份拼接好的文件给你。如果你没有获取到拼接后的文件,则它可能不是一个 pem 文件,而是 bundle、cert、cert、key文件或一些相同概念但名称类似的文件。 无论如何,只要我们得到了HAProxy使用的pem文件,我们只需经过简单配置就是可以处理SSL连接了。 下面我们将要配置haproxy来安装SSL证书,配置文件如下 #--------------------------------------------------------------------- #Exampleconfigurationforapossiblewebapplication.Seethe #fullconfigurationoptionsonline. # #http://haproxy.1wt.eu/download/1.4/doc/configuration.txt # #--------------------------------------------------------------------- #--------------------------------------------------------------------- #Globalsettings #--------------------------------------------------------------------- global #tohavethesemessagesendupin/var/log/haproxy.logyouwill #needto: # #1)configuresyslogtoacceptnetworklogevents.Thisisdone #byaddingthe'-r'optiontotheSYSLOGD_OPTIONSin #/etc/sysconfig/syslog # #2)configurelocal2eventstogotothe/var/log/haproxy.log #file.Alinelikethefollowingcanbeaddedto #/etc/sysconfig/syslog # #local2.*/var/log/haproxy.log # log127.0.0.1local2warning chroot/var/lib/haproxy pidfile/var/run/haproxy.pid maxconn400000 userhaproxy grouphaproxy daemon tune.ssl.default-dh-param2048 #nbproc3 #turnonstatsunixsocket statssocket/var/lib/haproxy/stats #--------------------------------------------------------------------- #commondefaultsthatallthe'listen'and'backend'sectionswill #useifnotdesignatedintheirblock #--------------------------------------------------------------------- defaults modehttp logglobal optionhttplog optiondontlognull optionhttp-server-close optionforwardforexcept127.0.0.0/8 optionredispatch optionhttpclose retries3 timeouthttp-request10s timeoutqueue1m timeoutconnect10s timeoutclient1m timeoutserver1m timeouthttp-keep-alive10s timeoutcheck10s statsenable statshide-version statsuri/haproxy?status statsrealmHaproxy\Statistics statsauthadmin:asd870719 #statsadminifTRUE #--------------------------------------------------------------------- #mainfrontendwhichproxystothebackends #--------------------------------------------------------------------- #frontendmain*:5000 #aclurl_staticpath_beg-i/static/images/javascript/stylesheets #aclurl_staticpath_end-i.jpg.gif.png.css.js #use_backendstaticifurl_static #default_backendapp frontendwzlinux_ssl bind*:80 bind*:443sslcrt/etc/haproxy/wzlinux.pem modehttp default_backendwzlinux #--------------------------------------------------------------------- #staticbackendforservingupimages,stylesheetsandsuch #--------------------------------------------------------------------- #backendstatic #balanceroundrobin #serverstatic127.0.0.1:4331check backendwzlinux modehttp balanceroundrobin optionforwardfor #optionhttpchkHEAD/HTTP/1.1\r\nHost:localhost serverwzlinux0110.0.0.9:8080checkinter15000rise2fall4 serverwzlinux0210.0.0.9:8081checkinter15000rise2fall4 serverwzlinux0310.0.0.9:8082checkinter15000rise2fall4 serverwzlinux0410.0.0.9:8083checkinter15000rise2fall4 serverwzlinux0510.0.0.9:8084checkinter15000rise2fall4 serverwzlinux0610.0.0.9:8085checkinter15000rise2fall4 serverwzlinux0710.0.0.9:8086checkinter15000rise2fall4 #http-requestset-headerX-Forwarded-Port%[dst_port] #http-requestadd-headerX-Forwarded-Protohttpsif{ssl_fc} 因为 SSL 连接在负载均衡器上终止了,我们依然来发送正常的 HTTP 请求到后台服务器。 只接受SSL连接 如果你想让网站只接受SSL连接,你可以添加向前端配置加上redirect导向: frontendwzlinux_ssl bind*:80 bind*:443sslcrt/etc/haproxy/wzlinux.pem redirectschemehttpsif!{ssl_fc} modehttp default_backendwzlinux 上面,我们添加了 redirect 导向,如果连接不是通过SSL连接的,它将http重定向到https。 4、使用HAProxy实现SSL穿透 使用SSL穿透,我们将让后台服务器处理SSL连接,而非负载均衡器来处理。 负载均衡器的工作就只是简单地将请求转发到配置好的后台服务器。因为连接还保持加密状态,HAProxy只能将它转发给其他服务器,其他事情就没法做了。 在这个配置中,我们需要在前端和后台配置中同时使用TCP模式而不是HTTP模式。HAProxy只会把连接当作信息流来转发到其他服务器,而不会使用在HTTP请求上才能使用的功能。 首先,我们调整一下前端配置: frontendwzlinux_ssl bind*:80 bind*:443 optiontcplog modetcp default_backendwzlinux 这里依然同时绑定80和443端口,以保证正常的HTTP连接和SSL连接都能工作。 正如上述提到的,转发一个安全连接而服务器而不作任何解码,我们需要使用TCP模式(mode tcp)。这也意味着我们需要设置tcp日志而不是默认的http日志(option tcplog)。 接着,我们要调整后台end配置。注意,我们还要将这个更改成TCP模式,并删除一些directives以避免因为修改/增加HTTP报头功能所带来的冲突: backendwzlinux modetcp balanceroundrobin optionssl-hello-chk serverwzlinux0110.0.0.9:8080checkinter15000rise2fall4 serverwzlinux0210.0.0.9:8081checkinter15000rise2fall4 serverwzlinux0310.0.0.9:8082checkinter15000rise2fall4 serverwzlinux0410.0.0.9:8083checkinter15000rise2fall4 serverwzlinux0510.0.0.9:8084checkinter15000rise2fall4 serverwzlinux0610.0.0.9:8085checkinter15000rise2fall4 serverwzlinux0710.0.0.9:8086checkinter15000rise2fall4 正如你所看到的,这里设置成了mode tcp - 前端和后台配置都需要设置成这个模式。 我们还删除了option forwardfor和http-request选项 - 这些不能用于TCP模式,而且我们也不能向已加密的请求添加报头,还有一些前面的默认配置也删去关于http的配置,这里不再演示。 为了检查正确与否,我们可以使用ssl-hello-chk来检查连接及它处理SSL(特别是SSLv3)连接的能力。 在这个例子中,我虚构了两个接受SSL证书的后台服务器。如果你有阅读过edition SSL certificates,你会看到如何将它们集成到 Apache 或 Nginx 来创建一个网络服务器后台,以处理SSL通信。使用SSL 穿越,不需要给HAProxy创建或使用SSL证书。后台服务器都能够处理SSL连接,如同只有一台服务器且没有使用负载均衡器那样。 在关于如何设定lvs分发这里不再进行设定演示,大家可以查看我有关LVS的文章。

优秀的个人博客,低调大师

AI 鸿蒙应用开发真实项目开发实录

鸿蒙上做 AI 应用有三条路:系统 Kit(Core Speech/Vision/NLP)开箱即用、MindSpore Lite 端侧自部署模型、云侧大模型 API。选型不看技术先进性,看四个约束:离线可用性、响应延迟、包体积、隐私合规。真实项目里通常是混合的——高频低延迟任务走端侧 Kit,复杂理解走云侧大模型,端侧兜底降级。把所有能力都押在云侧,弱网即不可用;全押端侧,模型精度和包体积双输。

优秀的个人博客,低调大师

Android开发笔记--Android开发时常用控件(二

MicrosoftInternetExplorer402DocumentNotSpecified7.8Normal0 RadioGroup,RadioButton(单选) CheckBox(多选) Toast(像一个遮罩层) 例1,<RadioGroupAndroid:id="@+id/genderGroup" Android:layout_width="wrap_content" Android:layout_height="wrap_content" Android:orientation="vertical"> <RadioButtonAndroid:id="@+id/female" Android:layout_width="wrap_content" Android:layout_height="wrap_content" Android:text="@string/female"/> <RadioButtonAndroid:id="@+id/male" Android:layout_width="wrap_content" Android:layout_height="wrap_content" Android:text="@string/male"/> </RadioGroup> 代码:1.控件对象声明 2.取得对象控件 3.设置监听器(只为RadioGroup注册一个) PublicvoidonCheckedChange(RadioGroupgroup,intcheckedId){ If(femaleButton.getId()==checkedId){ System.out.println("Female"); } Elseif(maleButton.getId()==checkedId){ System.out.println("Male"); } } 4.触发事件 //为RadioGroup设置 RadioGroup.setOnCheckedChangeListener(newRadioGroup.OnCheckedChangeListener()) 例2.<CheckBoxandroid:id="@+id/swim" Android:layout_width="wrap_content" Android:layout_height="wrap_content" Android:text="@string/swim"/> <CheckBoxandroid:id="@+id/run" Android:layout_width="wrap_content" Android:layout_height="wrap_content" Android:text="@string/run"/> <CheckBoxandroid:id="@+id/read" Android:layout_width="wrap_content" Android:layout_height="wrap_content" Android:text="@string/read"/> 代码:1.控件对象声明 2.取得对象控件 3.设置监听器(为每一个CheckBox注册监听器) PublicvoidonCheckedChanged(CompoundButtonbuttonView,booleanisChecked){ If(isChecked){ System.out.println("yes"); } Else{ System.out.println("NO"); } } 4.触发事件 CheckBox.setOnCheckedChangeLinstener(newCompoundButton.onCheckedChangeLinstener(){}) 例3.Toast Toast.makeText(this.class,displaystring,messageTime).show() 本文转自My_King1 51CTO博客,原文链接:http://blog.51cto.com/apprentice/1360574,如需转载请自行联系原作者

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

用户登录
用户注册
修改密码
忘记密码