1、新建项目目录 security01-jc
2、在 security01-jc 新建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.okttl</groupId>
- <artifactId>security</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>war</packaging>
- <name>security</name>
- <properties>
- <!-- jdk 版本 -->
- <jdk.version>1.7</jdk.version>
- <!-- springframework (spring 框架)版本 -->
- <springframework.version>4.1.6.RELEASE</springframework.version>
- <!-- spring security (spring 安全框架)版本 -->
- <security.version>4.0.1.RELEASE</security.version>
- <servlet.version>3.1.0</servlet.version>
- <jsp.version>2.3.2-b01</jsp.version>
- <jstl.version>1.2</jstl.version>
- </properties>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <!-- 不需要再定义所有子模块版本 -->
- <groupId>org.springframework</groupId>
- <artifactId>spring-framework-bom</artifactId>
- <version>${springframework.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
- <dependencies>
- <!-- springframework 框架的引入 -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- </dependency>
- <!-- spring mvc 的引用 -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- </dependency>
- <!-- spring 安全框架 -->
- <dependency>
- <groupId>org.springframework.security</groupId>
- <artifactId>spring-security-web</artifactId>
- <version>${security.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.security</groupId>
- <artifactId>spring-security-config</artifactId>
- <version>${security.version}</version>
- </dependency>
- <!-- Servlet 依赖 -->
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>javax.servlet-api</artifactId>
- <version>${servlet.version}</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>javax.servlet.jsp</groupId>
- <artifactId>javax.servlet.jsp-api</artifactId>
- <version>${jsp.version}</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>jstl</artifactId>
- <version>${jstl.version}</version>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>3.3</version>
- <configuration>
- <source>${jdk.version}</source>
- <target>${jdk.version}</target>
- <!-- java 的 Xlint 命令,all 启用所有告警-->
- <compilerArgument>-Xlint:all</compilerArgument>
- <showWarnings>true</showWarnings>
- <showDeprecation>true</showDeprecation>
- </configuration>
- </plugin>
- </plugins>
- </build>
- </project>
3、在security01-jc 目录内新建 src/main/java 目录,在此目录下新建包(java 开发人员都知道也就是目录) 和类我的目录结构如下 :
com.okttl.security
在此包下新建两个类SecurityConfig.java 和 SecurityWebApplicationInitializer.java
SecurityConfig.java 源码
- package com.okttl.security;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
- import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
- import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
- @EnableWebSecurity
- public class SecurityConfig extends WebSecurityConfigurerAdapter {
- @Autowired
- public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
- auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");
- }
- }
SecurityWebApplicationInitializer.java 源码
- package com.okttl.security;
- import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
- public class SecurityWebApplicationInitializer extends
- AbstractSecurityWebApplicationInitializer {
- public SecurityWebApplicationInitializer(){
- super(SecurityConfig.class);
- }
- }
4、在security01-jc/src/main 目录内新建webapp目录。 在此目录内新建index.jsp 文件
index.jsp 代码:
- <?xml version="1.0" encoding="UTF-8" ?>
- <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"
- xmlns:c="http://java.sun.com/jsp/jstl/core" version="2.0">
- <jsp:directive.page contentType="text/html" pageEncoding="UTF-8" />
- <jsp:output omit-xml-declaration="true" />
- <jsp:output doctype-root-element="HTML"
- doctype-system="about:legacy-compat" />
- <html>
- <head>
- <title>Hello Security</title>
- </head>
- <body>
- <div class="container">
- <h1>This is secured!</h1>
- <p>
- Hello <b><c:out value="${pageContext.request.remoteUser}"/></b>
- </p>
- </div>
- </body>
- </html>
- </jsp:root>
到此项目已经可以运行了。注意你的电脑上的jdk 要与 jdk.version 版本一致