1 RBAC
Role-Based Access Control(给予角色的权限控制访问)
2 认证管理

Basic认证:
一个请求到来时,浏览器弹出对话框,让用户输入用户名和密码,并用base64进行编码,实际是username:password进行base64编码。浏览器会在http报文头部加上base64编码的内容,服务器通过解析出这些信息,并进行认证通过才可以访问。
两个明显缺点:
1.无状态的,每次都必须带上认证信息
2.传输安全性不足,用的base64编码,基本上就属于铭文传输
Digest认证:
主要用来解决Basic认证带来的安全问题,采用了challenge-response的认证模式。当访问特定的资源时,浏览器依旧弹出对话框,让用户输入用户名密码。浏览器会对 用户名、密码、http请求方法、被请求资源的URI等组合后,进行MD5运算,之后把计算得到的摘要信息发送给服务器。服务器web容器获取到http报文头部相关的认证信息后,从中获取username,根据username获取数据库中对应的密码,同样对 用户名、密码、http请求方法、被请求资源的URI等组合进行MD5运算,将运算结果与报文头部中获取的比较,相同则认证通过
缺点:
认证的报文被攻击者拦截到,攻击者也可以获取到资源
X.509证书认证:
包含以下信息,X.509的版本号、证书持有人的公钥、证书的序列号(是由CA给与的)、主体信息、证书有效期、证书认证机构、发布的数字签名、签名的算法等等。已经被广泛应用在对电子邮件进行签名,对程序代码进行认证,以及对其他类型的数据进行认证等等。
LDAP认证:
轻量级的目录访问协议(Lightweight Directory Access Protocol)
Form认证
3 权限拦截

SecurityContextPersistenceFilter
LogoutFilter
AbstractAuthenticationProcessingFilter
DefaultLoginPageGeneratingFilter
BasicAuthenticationFilter
SecurityContextHolderAwareRequestFilter
RememberMeAuthenticationFilter
AnonymousAuthenticationFilter
ExceptionTranslationFilter
SessionManagementFilter
FilterSecurityInterceptor
FilterChainProxy
4 数据库管理

5 权限缓存
CachingUserDetailsService
EhCacheBasedUserCache
我们不仅会缓存userCache,还会缓存用户权限相关的一些信息。
我们也不仅会使用内存级别的cache和Ehcache,还会使用Redis,Memcache....
6 自定义决策
AbstractAccessDecisionManager
AccessDecisionVoter
RoleVoter
AffirmativeBased 一票通过
ConsensusBased 一半以上资源投票通过
UnanimousBased 全票通过
7 基于SpringBoot的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/**");
}
}
@SpringBootApplication // 用来替代@Configuration @EnableAutoConfiguration @ConmponentScan这三个注解
@RestController // 是@Controller和@ResponseBody的合体
@EnableAutoConfiguration // @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@RequestMapping("/")
public String home() {
return "hello spring boot";
}
@RequestMapping("/hello")
public String hello() {
return "hello world";
}
}
<?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.gzw</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>demo</name>
<description>Demo project for Spring Security</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<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-tomcat</artifactId>
<scope>provided</scope>
</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>
</plugins>
</build>
</project>
本文介绍Spring Security的各种认证方式,包括Basic认证、Digest认证、X.509证书认证及LDAP认证等,并详解权限拦截机制及自定义决策过程。同时提供基于Spring Boot的Spring Security快速配置示例。
407

被折叠的 条评论
为什么被折叠?



