您的位置:首页 > 编程语言 > Java开发

Spring Security技术栈开发企业级认证与授权(一)环境搭建

2018-03-19 14:37 633 查看
Spring Security
是一个能够为基于
Spring
的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可以在
Spring
应用上下文中配置的
Bean
,充分利用了
Spring IoC
DI
(控制反转
Inversion of Control
,
DI:Dependency Injection
依赖注入)和
AOP
(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作。

本篇博客主要记录的是学习利用
Spring Security
技术栈开发企业级认证与授权。这篇博客的主要内容是搭建环境。

一、项目组织结构

下图展示了项目的组织结构,其中
lemon-security
为聚合项目,打包方式为
pom
,其他四个项目为子模块,都是
lemon-security
的子模块,打包方式均为
jar




下面简要说明各个项目的基本作用:

项目作用
lemon-security
聚合项目,主要控制整个项目所需依赖的版本
lemon-security-core
认证与授权的核心模块
lemon-security-browser
浏览器作为客户端的认证与授权模块,依赖
lemon-security-core
模块
lemon-security-app
移动端作为客户端的认证与授权模块,依赖
lemon-security-core
模块
lemon-security-demo
案例模块,依赖
lemon-security-browser
lemon-security-app
模块

二、依赖

lemon-security
pom
依赖


<?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"> <modelVersion>4.0.0</modelVersion>

<groupId>com.lemon.security</groupId>
<artifactId>lemon-security</artifactId>
<version>1.0.0-SNAPSHOT</version>
<modules>
<module>../lemon-security-core</module>
<module>../lemon-security-browser</module>
<module>../lemon-security-app</module>
<module>../lemon-security-demo</module>
</modules>
<packaging>pom</packaging>
<description>Spring Security技术栈开发企业级认证与授权POM项目</description>

<properties>
<lemon.security.version>1.0.0-SNAPSHOT</lemon.security.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>Brussels-SR7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>

</project>


这个
pom
文件中主要涉及到的就是版本控制,这里引进了
Spring
平台的版本控制,和
Spring Cloud
版本控制,整个项目是基于
Spring Boot
进行开发的。

lemon-security-core
pom
依赖


<?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"> <modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.lemon.security</groupId>
<artifactId>lemon-security</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../lemon-security</relativePath>
</parent>

<artifactId>lemon-security-core</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<description>Spring Security技术栈开发企业级认证与授权CORE项目</description>

<dependencies>
<!-- APP安全认证的重要依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>

<!-- session存储依赖,暂时用不到,先注释掉 -->
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-data-redis</artifactId>-->
<!--</dependency>-->

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

<!-- 第三方登录用到的重要依赖 -->
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-config</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-core</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-security</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-web</artifactId>
</dependency>

<!-- 工具依赖 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.1</version>
</dependency>

<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
</dependency>

</dependencies>
</project>


lemon-security-browser
pom
依赖


<?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"> <modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.lemon.security</groupId>
<artifactId>lemon-security</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../lemon-security</relativePath>
</parent>

<artifactId>lemon-security-browser</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<description>Spring Security技术栈开发企业级认证与授权浏览器项目</description>

<dependencies>
<dependency>
<groupId>com.lemon.security</groupId>
<artifactId>lemon-security-core</artifactId>
<version>${lemon.security.version}</version>
</dependency>

<!-- 浏览器端Session管理的重要依赖 -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
</dependency>

</dependencies>

</project>


lemon-security-app
pom
依赖


<?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"> <modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.lemon.security</groupId>
<artifactId>lemon-security</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../lemon-security</relativePath>
</parent>

<artifactId>lemon-security-app</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<description>Spring Security技术栈开发企业级认证与授权移动端项目</description>

<dependencies>
<dependency>
<groupId>com.lemon.security</groupId>
<artifactId>lemon-security-core</artifactId>
<version>${lemon.security.version}</version>
</dependency>
</dependencies>

</project>


lemon-security-demo
pom
依赖


<?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"> <modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.lemon.security</groupId>
<artifactId>lemon-security</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../lemon-security</relativePath>
</parent>

<artifactId>lemon-security-demo</artifactId>
<version>${lemon.security.version}</version>
<packaging>jar</packaging>
<description>Spring Security技术栈开发企业级认证与授权案例项目</description>

<dependencies>
<!-- 首先学习的是浏览器端的安全开发 -->
<dependency>
<groupId>com.lemon.security</groupId>
<artifactId>lemon-security-browser</artifactId>
<version>${lemon.security.version}</version>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>

</dependencies>

<build>
<plugins>
<!-- spring boot应用打包插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.10.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>


三、编写
Spring Boot
应用入口

在包
com.lemon.security.web.application
下编写
MainApplication.java
如下:

package com.lemon.security.web.application;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

/**
* @author lemon
* @date 2018/3/18 下午5:44
*/
@SpringBootApplication
@ComponentScan(basePackages = {"com.lemon.security"})
public class MainApplication {

public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}


再在包
com.lemon.security.web.controller
下编写一个
DemoController.java
如下:

package com.lemon.security.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
* @author lemon
* @date 2018/3/18 下午5:46
*/
@Controller
public class DemoController {

@GetMapping("/hello")
@ResponseBody
public String hello() {
return "Hello Spring Security";
}
}


当然,还要写一个
Spring Boot
的配置文件,内容如下:

spring:
profiles:
active: dev

---
spring:
profiles: dev
# 数据库配置
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/spring-security?characterEncoding=utf-8&useSSL=false
username: root
password: 123456
# 配置Druid连接池
type: com.alibaba.druid.pool.DruidDataSource
server:
port: 8080


需要将上面的数据库连接设置修改为自己的即可。

这时候运行
Spring Boot
Main
方法,会发现报了如下的错误:

Caused by: java.lang.IllegalArgumentException: No Spring Session store is configured: set the 'spring.session.store-type' property


这是由于
lemon-security-core
中加入了
Spring Session
的依赖,而没有配置
Session
的存储方式导致出错,我们在
Spring Boot
的配置文件加入下面的内容即可,内容如下:

spring:
session:
store-type: none


完整的配置文件为:

spring:
profiles:
active: dev

---
spring:
profiles: dev
# 数据库配置
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.25.133:3306/spring-security?characterEncoding=utf-8&useSSL=false
username: root
password: caifutong122819
# 配置Druid连接池
type: com.alibaba.druid.pool.DruidDataSource
# 配置session存储方式,暂时关掉该功能
session:
store-type: none
server:
port: 8080


这时候重新启动应用就可以正常启动了,到浏览器运行
http://localhost:8080/hello
发现需要输入用户名和密码才可以访问
DemoController
hello
方法,这是由于在
Spring Boot
环境下
Spring Security
的默认配置,也就是需要经过验证在可以访问方法。如图所示:



由于是前期环境,暂时可以关闭权限验证功能,这需要在配置文件中设置一下即可:

# 首先将权限验证关闭
security:
basic:
enabled: false


这时候重新启动应用就可以正常启动了,到浏览器运行
http://localhost:8080/hello
发现浏览器上显示了
Hello Spring Security


Spring Security技术栈开发企业级认证与授权系列文章列表:

Spring Security技术栈开发企业级认证与授权(一)环境搭建

Spring Security技术栈开发企业级认证与授权(二)使用Spring MVC开发RESTful API

Spring Security技术栈开发企业级认证与授权(三)表单校验以及自定义校验注解开发

Spring Security技术栈开发企业级认证与授权(四)RESTful API服务异常处理

Spring Security技术栈开发企业级认证与授权(五)使用Filter、Interceptor和AOP拦截REST服务

Spring Security技术栈开发企业级认证与授权(六)使用REST方式处理文件服务

Spring Security技术栈开发企业级认证与授权(七)使用Swagger自动生成API文档

示例代码下载地址:

项目已经上传到码云,欢迎下载,内容所在文件夹为
chapter001
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐