Spring Security 简明教程
Spring Security - Environment Setup
本 chapters 将指导你如何准备开发环境以开始使用 Spring Framework 和 Spring Security。它还将教你如何在设置 Spring Framework 之前在你的计算机上设置 JDK、Maven 和 Eclipse -
This chapter will guide you on how to prepare a development environment to start your work with Spring Framework and Spring Security. It will also teach you how to set up JDK, Maven and Eclipse on your machine before you set up Spring Framework −
Step 1 - Setup Java Development Kit (JDK)
你可以从 Oracle 的 Java 网站下载 SDK 的最新版本 - Java SE Downloads. 你将找到下载的文件中安装 JDK 的说明,请按照给定的说明进行安装和配置设置。最后设置 PATH 和 JAVA_HOME 环境变量以引用包含 java 和 javac 的目录,通常分别为 java_install_dir/bin 和 java_install_dir。
You can download the latest version of SDK from Oracle’s Java site − Java SE Downloads. You will find instructions for installing JDK in downloaded files, follow the given instructions to install and configure the setup. Finally set PATH and JAVA_HOME environment variables to refer to the directory that contains java and javac, typically java_install_dir/bin and java_install_dir respectively.
如果你正在运行 Windows 并已将 JDK 安装在 C:\Program Files\Java\jdk-21 中,则需要在 C:\autoexec.bat 文件中放置以下行。
If you are running Windows and have installed the JDK in C:\Program Files\Java\jdk-21, you would have to put the following line in your C:\autoexec.bat file.
set PATH=C:\Program Files\Java\jdk-21;%PATH%
set JAVA_HOME=C:\Program Files\Java\jdk-21
或者,在 Windows NT/2000/XP 中,你必须右键单击我的电脑,选择属性 → 高级 → 环境变量。然后,你将不得不更新 PATH 值并单击确定按钮。
Alternatively, on Windows NT/2000/XP, you will have to right-click on My Computer, select Properties → Advanced → Environment Variables. Then, you will have to update the PATH value and click the OK button.
在 Unix(Solaris、Linux 等)中,如果 SDK 安装在 /usr/local/jdk-21 中,并且你使用 C shell,则必须将以下内容放入 .cshrc 文件中。
On Unix (Solaris, Linux, etc.), if the SDK is installed in /usr/local/jdk-21 and you use the C shell, you will have to put the following into your .cshrc file.
setenv PATH /usr/local/jdk-21/bin:$PATH
setenv JAVA_HOME /usr/local/jdk-21
或者,如果你使用诸如 Borland JBuilder、Eclipse、IntelliJ IDEA 或 Sun ONE Studio 这样的集成开发环境 (IDE),则必须编译并运行一个简单程序来确认 IDE 知道你在何处安装了 Java。否则,你必须按照 IDE 文档中给出的内容执行正确的设置。
Alternatively, if you use an Integrated Development Environment (IDE) like Borland JBuilder, Eclipse, IntelliJ IDEA, or Sun ONE Studio, you will have to compile and run a simple program to confirm that the IDE knows where you have installed Java. Otherwise, you will have to carry out a proper setup as given in the document of the IDE.
Step 2 - Setup Spring Tool Suite
本教程中的所有示例都是使用 Spring Tool Suite 编写的。因此,我们建议你在你的计算机上安装 Spring Tool Suite 的最新版本。
All the examples in this tutorial have been written using Spring Tool Suite. So we would suggest you should have the latest version of Spring Tool Suite installed on your machine.
要安装 Spring Tools IDE,请从 https://spring.io/tools 下载最新的 Spring Tools 二进制文件。下载安装程序后,将二进制分发包解压到合适的位置。例如在 Windows 上的 C:\sts 中,或 Linux/Unix 上的 /usr/local/sts 中,最后相应地设置 PATH 变量。
To install Spring Tools IDE, download the latest Spring Tools binaries from https://spring.io/tools. Once you download the installation, unpack the binary distribution into a convenient location. For example, in C:\sts on Windows, or /usr/local/sts on Linux/Unix and finally set PATH variable appropriately.
可以通过在 Windows 机器上执行以下命令来启动 String Tool Suite,或者你也可以双击 eclipse.exe
String Tool Suite can be started by executing the following commands on Windows machine, or you can simply double-click on eclipse.exe
%C:\sts\SpringToolSuite4.exe
可以通过在 Unix(Solaris、Linux 等)机器上执行以下命令来启动 SpringToolSuite4 −
SpringToolSuite4 can be started by executing the following commands on Unix (Solaris, Linux, etc.) machine −
$/usr/local/sts/SpringToolSuite4
成功启动后,如果一切正常,它应该显示以下结果 −
After a successful startup, if everything is fine then it should display the following result −
Step 3 - Download Maven Archive
从 https://maven.apache.org/download.cgi 下载 Maven 3.9.8。
Download Maven 3.9.8 from https://maven.apache.org/download.cgi.
OS |
Archive name |
Windows |
apache-maven-3.9.8-bin.zip |
Linux |
apache-maven-3.9.8-bin.tar.gz |
Mac |
apache-maven-3.9.8-bin.tar.gz |
Step 4 - Extract the Maven Archive
将存档解压到你要安装 Maven 3.9.8 的目录中。存档将创建子目录 apache-maven-3.9.8。
Extract the archive, to the directory you wish to install Maven 3.9.8. The subdirectory apache-maven-3.9.8 will be created from the archive.
OS |
Location (can be different based on your installation) |
Windows |
C:\Program Files\Apache\apache-maven-3.9.8 |
Linux |
/usr/local/apache-maven |
Mac |
/usr/local/apache-maven |
Step 5 - Set Maven Environment Variables
将 M2_HOME、M2、MAVEN_OPTS 添加到环境变量。
Add M2_HOME, M2, MAVEN_OPTS to environment variables.
OS |
Output |
Windows |
Set the environment variables using system properties. M2_HOME=C:\Program Files\Apache\apache-maven-3.9.8 M2=%M2_HOME%\bin MAVEN_OPTS=-Xms256m -Xmx512m |
Linux |
Open command terminal and set environment variables. export M2_HOME=/usr/local/apache-maven/apache-maven-3.9.8 export M2=$M2_HOME/bin export MAVEN_OPTS=-Xms256m -Xmx512m |
Mac |
Open command terminal and set environment variables. export M2_HOME=/usr/local/apache-maven/apache-maven-3.9.8 export M2=$M2_HOME/bin export MAVEN_OPTS=-Xms256m -Xmx512m |
Step 6 - Add Maven bin Directory Location to System Path
现在将 M2 变量附加到系统路径。
Now append M2 variable to System Path.
OS |
Output |
Windows |
Append the string ;%M2% to the end of the system variable, Path. |
Linux |
export PATH=$M2:$PATH |
Mac |
export PATH=$M2:$PATH |
Step 7 - Verify Maven Installation
现在打开控制台并执行以下 mvn 命令。
Now open console and execute the following mvn command.
OS |
Task |
Command |
Windows |
Open Command Console |
c:> mvn --version |
Linux |
Open Command Terminal |
$ mvn --version |
Mac |
Open Terminal |
machine:~ joseph$ mvn --version |
最后,验证上述命令的输出,其应如下所示 −
Finally, verify the output of the above commands, which should be as follows −
OS |
Output |
Windows |
Apache Maven 3.9.8 (36645f6c9b5079805ea5009217e36f2cffd34256) Maven home: C:\Program Files\Apache\apache-maven-3.9.8 Java version: 21.0.2, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk-21 Default locale: en_IN, platform encoding: UTF-8 OS name: "windows 11", version: "10.0", arch: "amd64", family: "windows" |
Linux |
Apache Maven 3.9.8 (36645f6c9b5079805ea5009217e36f2cffd34256) Java version: 21.0.2 Java home: /usr/local/java-current/jre |
Mac |
Apache Maven 3.9.8 (36645f6c9b5079805ea5009217e36f2cffd34256) Java version: 21.0.2 Java home: /Library/Java/Home/jre |