spring security简介
Spring security是基于Spring的应用程序提供声明式安全保护的安全性框架,Spring Security提供了完整的安全性解决方案,它能够在Web请求级别和方法调用级别处理身份认证和授权。
最初,Spring Security被称为Acegi Security。Acegi是一个强大的安全框架,但是它存在一个严重的问题:那就是需要大量的XML配置。典型的Acegi配置有几百行XML是很常见的。2.0版本,Acegi更名为Spring Security。3.0版本融入了Spring EL,这进一步简化了安全性的配置。
Spring Security从两个角度来解决安全性问题。它使用Servlet规范中的Filter保护Web请求并限制URL级别的访问。Spring Security还能够使用Spring AOP保护方法调用——借助于对象代理和使用通知,能够确保只有具备适当权限的用户才能够访问受保护的方法。
Spring Security的模块
Spring Security3.2分为11个模块,如下:
模块 | 描述 |
---|---|
ACL | 支持通过访问控制列表(access control list,ACL)为域对象提供安全性 |
切面(Aspects) | 一个很小的莫模块,当使用Spring Security注解时,时使用基于AspectJ的切面,而不是使用标准的Spring AOP |
CAS客户端(CAS Client) | 提供与jasig的中心认证服务(Central Authentication Service,CAS) 进行集成的功能 |
配置(Configuration) | 包含通过XML和Java的配置Spring Security的功能支持 |
核心(core) | 提供了Spring Security的基本库 |
加密(Cryptography) | 提供了加密和密码编码的功能 |
LDAP | 支持基于LDAP进行认证 |
OpernID | 支持使用OpenID进行集中式认证 |
Remoting | 提供对Spring Remoting的支持 |
标准库(Tag Library) | Spring Security的JSP标签库 |
Web | 提供了Spring Security基于Filter的Web安全性支持 |
我们通常只会用到CORE和configuration两个模块。
基于mave创建Spring Security的DEMO
1、创建一个SpringBoot的maven项目,在pom添加SpringSecurity的最小依赖:
<dependencies>
<!-- ... other dependency elements ... -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
不用指定版本号,由于Spring Boot提供了Maven BOM来管理依赖版本。若是非SpringBoot项目,则添加下面的依赖:
<dependencies>
<!-- ... other dependency elements ... -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
</dependencies>
这有可能引起jar包依赖问题,解决的方案方法是使用Spring Security的BOM,以确保在整个项目中使用一致的Spring Security版本:
<dependencyManagement>
<dependencies>
<!-- ... other dependency elements ... -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>5.2.11.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2、Spring 3.2就引入了Java配置方案,而不需要通过XML来配置安全功能了,如下是Spring Security最简单的java配置:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().authenticated()
.and()
.formLogin()
.and()
.httpBasic();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("user").password(new BCryptPasswordEncoder().encode("password")).roles("USER")
.and()
.withUser("admin").password(new BCryptPasswordEncoder().encode("admin")).roles("USER","ADMIN");
}
}
@EnableWebSecurity注解将会启用Web安全功能,它本身并没有什么用处。
我们扩展了WebSecurityConfigurerAdapter类,并重写了它的两个配置方法:
configure(HttpSecurity) 配置如何通过拦截器保护请求。
configure(AuthenticationManagerBuilder) 配置user-detail服务。AuthenticationManagerBuilder使用了构造这风格来构建认证的配置,这里我们使用了基于内存的用户存储并使用两个用户来配置内存用户存储。另外,借助passwordEncoder()方法指定了一个密码转码器,我们用到了BCryptPasswordEncoder,这是spring推荐使用的。必须要添加这个密码转换器,不然登录认证的时候会报错。
3、编写一个controller层用于测试:
@RestController
@RequestMapping("")
public class SpringSecurityController {
@RequestMapping("hello")
public String Hello(){
return "hello world";
}
}
4、启动项目进行测试,启动前在别忘了在application.properties文件中添加端口号:
server.port=8080
然后启动这个Spring Boot的web项目,在浏览器中输入http://localhost:8080/hello,这时还没有认证,会被拦截到Spring Security默认的一个登录页面,长得像这样:
输入admin/admin登录成功后,会重定向到http://localhost:8080/hello页面: