一、创建工程

新建Maven工程springsecurity-demo,并编辑pom.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/>
</parent>

<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<name>springsecurity-demo</name>
<artifactId>springsecurity-demo</artifactId>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

</project>

二、实现代码

1.创建启动类

在项目的src/main/java下创建io.binghe.demo包,并在包下创建DemoApplication类,代码如下:

package io.binghe.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* @author binghe
* @version 1.0.0
* @description 项目启动类
*/
@SpringBootApplication
@EnableAutoConfiguration
public class DemoApplication {
public static void main(String[] args){
SpringApplication.run(DemoApplication.class, args);
}
}

2.在io.binghe.demo包下创建ServletInitializer类,代码如下:

package io.binghe.demo;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

/**
* @author binghe
* @version 1.0.0
* @description Servlet初始化类
*/
public class ServletInitializer extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DemoApplication.class);
}
}

3.在io.binghe.demo包下创建SpringSecurityConfig类,同于配置SpringSecurity,代码如下:

package io.binghe.demo;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/**
* @author binghe
* @version 1.0.0
* @description SpringSecurity配置类
*/
@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/").permitAll()
.anyRequest().authenticated()
.and()
.logout().permitAll()
.and()
.formLogin();
http.csrf().disable();
}

@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/js/**", "/css/**", "/images/**");
}
}

4.创建controller类,新建io.binghe.demo.controller包,在包下创建DemoController类,代码如下:

package io.binghe.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* @author binghe
* @version 1.0.0
* @description 测试使用的Controller
*/
@RestController
public class DemoController {

@RequestMapping(value = "/")
public String home(){
return "hello string boot";
}

@RequestMapping(value = "/hello")
public String hello(){
return "hello world";
}
}

三、测试

1.启动工程

运行DemoApplication的main方法

2.测试工程

在浏览器分别访问http://localhost:8080和http://localhost:8080/hello。可以看到可以直接访问http://localhost:8080,而访问http://localhost:8080/hello则需要输入用户名和密码。说明SpringBoot整合SpringSecurity成功。

附图如下:

访问http://localhost:8080效果如下:

SpringSecurity之——SpringBoot整合SpringSecurity_权限管理

访问http://localhost:8080/hello效果如下:

SpringSecurity之——SpringBoot整合SpringSecurity_SpringSecurity_02