基于Spring boot创建了最简单Spring Security配置环境,一切都在pom中
以jar包形式运行
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.mose</groupId>
<artifactId>spring_security</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>spring_security Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring-boot.version>1.5.4.RELEASE</spring-boot.version>
<junit.version>4.12</junit.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<dependencies>
<!-- Start web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- End web -->
<!-- Start Spring Security -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- End Spring Security -->
<!-- Start Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- End Test -->
</dependencies>
<build>
<finalName>spring_security</finalName>
</build>
</project>
以war包形式运行
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.mose</groupId>
<artifactId>spring_security</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>spring_security Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring-boot.version>1.5.4.RELEASE</spring-boot.version>
<junit.version>4.12</junit.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<dependencies>
<!-- Start web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- End web -->
<!-- Start Deploy In Tomcat Server -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- End Deploy In Tomcat Server -->
<!-- Start Spring Security -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- End Spring Security -->
<!-- Start Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- End Test -->
</dependencies>
<build>
<finalName>spring_security</finalName>
</build>
</project>
**创建Security配置类
/**
* Description:Spring Security的java configuration
*
* @Author: 瓦力
* @Date: 2017/7/19 13:47
*/
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
/**
* Description:配置Spring Security
* 1.开启对任何地址("/**")的访问控制,要求必须句别"ROLE_USER"的角色
* 2.开启默认form表单形式的用户登入,访问地址为"/login",登录成功后自动跳转到用户前一次的访问地址
* 3.关闭csrf限制,该功能以后再讲,默认为开启状态<br>
*
* @param http
* @return
*
* @Author: 瓦力
* @Date: 2017/7/19 13:47
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/**").hasRole("USER").and().formLogin().and().csrf().disable();
}
}
@EnableWebSecurity设置开启Spring Security的java configuration方式
WebSecurityConfigurerAdapter
提供了Spring Security的基本配置,只需要根据要求重写其中的方法。
启动后记录根据控制台输出的密码
在浏览器输入http://localhost:8080/test任意地址后,页面自动跳转到http://localhost:8080/login
输入用户名(user)和密码,即可跳转到前次输入的地址。
以上就是Spring Security最简单的配置。
代码示例:https://github.com/wexgundam/spring.security/tree/master/ch01