Spring Security 入门教程
Spring Security 简介
Spring Security 介绍
Spring Security是基于spring的应用程序提供声明式安全保护的安全性框架,它提供了完整的安全性解决方案,能够在web请求级别和方法调用级别处理身份证验证和授权.它充分使用了依赖注入和面向切面的技术。
Spring Security 集成方式
- 使用配置文件集成。
- 使用数据库,根据spring security默认实现代码设计数据库。
- 支持插入filter,我们可以插入自己的filter来灵活使用
- 修改源码
Spring Security 验证方式
- HTTP BASIC认证头(基于IETF RFC的标准)
- HTTP摘要认证头(基于IETF RFC的标准)
- HTTP X.509客户端证书交换(基于IETF RFC的标准)
- LDAP(一种非常常见的跨平台认证需求,特别是在大型环境中)
- 基于表单的身份验证(用于简单的用户界面需求)
- OpenID身份验证
- 基于预先建立的请求头的认证(例如Computer Associates Siteminder)域登录
- JA-SIG中心认证服务(也称为CAS,这是一种流行的开源单点登录系统)
- 远程方法调用(RMI)和(Spring远程协议)的透明认证上下文传播
- 自动“记住我”身份验证(所以你可以勾选一个框,以避免在预定时间段内重新验证)
- 匿名身份验证(允许每个未经身份验证的呼叫自动承担特定的安全身份)
- 运行身份验证(如果一个调用应该使用不同的安全身份继续运行,这是有用的)
- Java认证和授权服务(JAAS)
- JEE容器自动化(因此,如果需要,你仍然可以使用容器管理身份验证)
- Java开源单点登录(JOSSO)
- OpenNMS网络管理平台
- 自己的认证系统
Spring Security 认证方式
配置内存
首先建立一个用户服务,配置所有用户和权限信息。然后交给认证管理器管理,认证管理器会将认证的任务交给一个或多个认证提供者。
基于数据库
这个是最常用的是用户认证,因为很多应用都是采用数据库存储用户数据。Spring Security提供了<jdbc-usr-service>.如果只指定了 data-source-ref得数据源,那么spring security会自己为我们写sql语句从数据库中查找用户和权限信息。但一般情况下,提供的查询语句并不能和我们的数据库对应上,所以我们需要自己写sql语句。
集成 Srping Security 框架
解决安全性
- web请求级别:使用servlet过滤器保护web请求并限制URL级别的访问。
- 方法调用级别:使用Spring AOP保护方法调用,确保具有适当权限的用户采用访问安全保护的方法。
Spring Security 集成项目示例
项目集成 Spring Security 框架
添加pom.xml 依赖
引入Spring Security 需要的依赖:
<!-- spring boot security 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
创建 Spring Security 配置类
创建WebSercurityConfig 类,继承 WebSercurityConfigurerAdapter 类:
package com.peraglobal.security.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/login").permitAll() //允许所有用户访问"/login"
.anyRequest().authenticated() // 其他地址的访问均需验证权限
.and()
.formLogin()
.loginPage("/login") // 指定登录页是"/login"
.defaultSuccessUrl("/") // 登录成功后默认跳转到"/hello"
.permitAll()
.and()
.logout()
.logoutSuccessUrl("/login") // 退出登录后的默认url是"/home"
.permitAll();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("123").roles("USER");
auth
.inMemoryAuthentication()
.withUser("admin").password("123").roles("USER");
}
}
说明:
- 通过@EnableWebSecurity注解开启Spring Security的功能
- 继承WebSecurityConfigurerAdapter,并重写它的方法来设置一些web安全的细节
configure(HttpSecurity http)方法,通过authorizeRequests()定义哪些URL需要被保护、哪些不需要被保护。例如以上代码指定了/和/home不需要任何认证就可以访问,其他的路径都必须通过身份验证。
- 通过formLogin()定义当需要用户登录时候,转到的登录页面。
- configureGlobal(AuthenticationManagerBuilder auth)方法,在内存中创建了一个用户,该用户的名称为admin,密码为123,用户角色为USER。
创建控制器类
package com.peraglobal.security.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping("/index")
public String index() {
return "index";
}
@RequestMapping("/hello")
public String hello() {
return "hello";
}
@RequestMapping("/login")
public String login() {
return "login";
}
}
创建 index.html 文件
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Spring Security入门</title>
</head>
<body>
<h1>欢迎使用Spring Security!</h1>
<p>点击 <a th:href="@{/hello}">这里</a> 打个招呼吧</p>
</body>
</html>
创建 hello.html 文件
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Hello World!</title>
</head>
<body>
<h1 th:inline="text">Hello [[${#httpServletRequest.remoteUser}]]!</h1>
<form th:action="@{/logout}" method="post">
<input type="submit" value="注销"/>
</form>
</body>
</html>
创建 login.html 文件
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Spring Security Example </title>
</head>
<body>
<div th:if="${param.error}">
用户名或密码错
</div>
<div th:if="${param.logout}">
您已注销成功
</div>
<form th:action="@{/login}" method="post">
<div><label> 用户名 : <input type="text" name="username"/> </label></div>
<div><label> 密 码 : <input type="password" name="password"/> </label></div>
<div><input type="submit" value="登录"/></div>
</form>
</body>
</html>
演示验证
进入系统自动加载到登录页面,验证通过则进入系统,失败则返回到登录页面。
注意:spring boot 版本是基于 1.4.7.RELEASE