Spring Security是一个可以为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组能够在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减小了为企业系统安全控制编写大量重复代码的工做。java
为了方便理解Spring Security认证流程,特地画了以下的类图,包含相关的核心认证类 算法
核心验证器spring
该对象提供了认证方法的入口,接收一个Authentiaton
对象做为参数;数据库
public interface AuthenticationManager {
Authentication authenticate(Authentication authentication) throws AuthenticationException;
}
复制代码
它是 AuthenticationManager
的一个实现类,提供了基本的认证逻辑和方法;它包含了一个 List<AuthenticationProvider>
对象,经过 AuthenticationProvider 接口来扩展出不一样的认证提供者(当Spring Security
默认提供的实现类不能知足需求的时候能够扩展AuthenticationProvider
覆盖supports(Class<?> authentication)
方法);编程
AuthenticationManager
接收 Authentication
对象做为参数,并经过 authenticate(Authentication)
方法对其进行验证;AuthenticationProvider
实现类用来支撑对 Authentication
对象的验证动做;UsernamePasswordAuthenticationToken
实现了 Authentication
主要是将用户输入的用户名和密码进行封装,并供给 AuthenticationManager
进行验证;验证完成之后将返回一个认证成功的 Authentication
对象;安全
Authentication
对象中的主要方法bash
public interface Authentication extends Principal, Serializable {
//#1.权限结合,可以使用AuthorityUtils.commaSeparatedStringToAuthorityList("admin,ROLE_ADMIN")返回字符串权限集合
Collection<? extends GrantedAuthority> getAuthorities();
//#2.用户名密码认证时能够理解为密码
Object getCredentials();
//#3.认证时包含的一些信息。
Object getDetails();
//#4.用户名密码认证时可理解时用户名
Object getPrincipal();
#5.是否被认证,认证为true
boolean isAuthenticated();
#6.设置是否能被认证
void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException;
复制代码
ProviderManager
是AuthenticationManager
的实现类,提供了基本认证明现逻辑和流程;app
public Authentication authenticate(Authentication authentication)
throws AuthenticationException {
//#1.获取当前的Authentication的认证类型
Class<? extends Authentication> toTest = authentication.getClass();
AuthenticationException lastException = null;
Authentication result = null;
boolean debug = logger.isDebugEnabled();
//#2.遍历全部的providers使用supports方法判断该provider是否支持当前的认证类型,不支持的话继续遍历
for (AuthenticationProvider provider : getProviders()) {
if (!provider.supports(toTest)) {
continue;
}
if (debug) {
logger.debug("Authentication attempt using "
+ provider.getClass().getName());
}
try {
#3.支持的话调用provider的authenticat方法认证
result = provider.authenticate(authentication);
if (result != null) {
#4.认证经过的话从新生成Authentication对应的Token
copyDetails(authentication, result);
break;
}
}
catch (AccountStatusException e) {
prepareException(e, authentication);
// SEC-546: Avoid polling additional providers if auth failure is due to
// invalid account status
throw e;
}
catch (InternalAuthenticationServiceException e) {
prepareException(e, authentication);
throw e;
}
catch (AuthenticationException e) {
lastException = e;
}
}
if (result == null && parent != null) {
// Allow the parent to try.
try {
#5.若是#1 没有验证经过,则使用父类型AuthenticationManager进行验证
result = parent.authenticate(authentication);
}
catch (ProviderNotFoundException e) {
// ignore as we will throw below if no other exception occurred prior to
// calling parent and the parent
// may throw ProviderNotFound even though a provider in the child already
// handled the request
}
catch (AuthenticationException e) {
lastException = e;
}
}
#6. 是否擦出敏感信息
if (result != null) {
if (eraseCredentialsAfterAuthentication
&& (result instanceof CredentialsContainer)) {
// Authentication is complete. Remove credentials and other secret data
// from authentication
((CredentialsContainer) result).eraseCredentials();
}
eventPublisher.publishAuthenticationSuccess(result);
return result;
}
// Parent was null, or didn't authenticate (or throw an exception).
if (lastException == null) {
lastException = new ProviderNotFoundException(messages.getMessage(
"ProviderManager.providerNotFound",
new Object[] { toTest.getName() },
"No AuthenticationProvider found for {0}"));
}
prepareException(lastException, authentication);
throw lastException;
}
复制代码
ProviderManager
经过 AuthenticationProvider
扩展出更多的验证提供的方式;而 AuthenticationProvider
自己也就是一个接口,从类图中咱们能够看出它的实现类AbstractUserDetailsAuthenticationProvider
和AbstractUserDetailsAuthenticationProvider
的子类DaoAuthenticationProvider
。DaoAuthenticationProvider
是Spring Security
中一个核心的Provider
,对全部的数据库提供了基本方法和入口。框架
DaoAuthenticationProvider
主要作了如下事情ide
#1.可直接返回BCryptPasswordEncoder,也能够本身实现该接口使用本身的加密算法核心方法String encode(CharSequence rawPassword);和boolean matches(CharSequence rawPassword, String encodedPassword);
复制代码
private PasswordEncoder passwordEncoder;
2. 实现了 `AbstractUserDetailsAuthenticationProvider` 两个抽象方法,
1. 获取用户信息的扩展点
```java
protected final UserDetails retrieveUser(String username,
UsernamePasswordAuthenticationToken authentication)
throws AuthenticationException {
UserDetails loadedUser;
try {
loadedUser = this.getUserDetailsService().loadUserByUsername(username);
}
复制代码
主要是经过注入`UserDetailsService`接口对象,并调用其接口方法 `loadUserByUsername(String username)` 获取获得相关的用户信息。`UserDetailsService`接口很是重要。
2. 实现 additionalAuthenticationChecks 的验证方法(主要验证密码);
复制代码
AbstractUserDetailsAuthenticationProvider
为DaoAuthenticationProvider
提供了基本的认证方法;
public Authentication authenticate(Authentication authentication)
throws AuthenticationException {
Assert.isInstanceOf(UsernamePasswordAuthenticationToken.class, authentication,
messages.getMessage(
"AbstractUserDetailsAuthenticationProvider.onlySupports",
"Only UsernamePasswordAuthenticationToken is supported"));
// Determine username
String username = (authentication.getPrincipal() == null) ? "NONE_PROVIDED"
: authentication.getName();
boolean cacheWasUsed = true;
UserDetails user = this.userCache.getUserFromCache(username);
if (user == null) {
cacheWasUsed = false;
try {
#1.获取用户信息由子类实现即DaoAuthenticationProvider
user = retrieveUser(username,
(UsernamePasswordAuthenticationToken) authentication);
}
catch (UsernameNotFoundException notFound) {
logger.debug("User '" + username + "' not found");
if (hideUserNotFoundExceptions) {
throw new BadCredentialsException(messages.getMessage(
"AbstractUserDetailsAuthenticationProvider.badCredentials",
"Bad credentials"));
}
else {
throw notFound;
}
}
Assert.notNull(user,
"retrieveUser returned null - a violation of the interface contract");
}
try {
#2.前检查由DefaultPreAuthenticationChecks类实现(主要判断当前用户是否锁定,过时,冻结User接口)
preAuthenticationChecks.check(user);
#3.子类实现
additionalAuthenticationChecks(user,
(UsernamePasswordAuthenticationToken) authentication);
}
catch (AuthenticationException exception) {
if (cacheWasUsed) {
// There was a problem, so try again after checking
// we're using latest data (i.e. not from the cache)
cacheWasUsed = false;
user = retrieveUser(username,
(UsernamePasswordAuthenticationToken) authentication);
preAuthenticationChecks.check(user);
additionalAuthenticationChecks(user,
(UsernamePasswordAuthenticationToken) authentication);
}
else {
throw exception;
}
}
#4.检测用户密码是否过时对应#2 的User接口
postAuthenticationChecks.check(user);
if (!cacheWasUsed) {
this.userCache.putUserInCache(user);
}
Object principalToReturn = user;
if (forcePrincipalAsString) {
principalToReturn = user.getUsername();
}
return createSuccessAuthentication(principalToReturn, authentication, user);
}
复制代码
AbstractUserDetailsAuthenticationProvider
主要实现了AuthenticationProvider
的接口方法authenticate
并提供了相关的验证逻辑;
UserDetails
AbstractUserDetailsAuthenticationProvider
定义了一个抽象的方法复制代码
protected abstract UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException;
2. 三步验证工做
1. preAuthenticationChecks
2. additionalAuthenticationChecks(抽象方法,子类实现)
3. postAuthenticationChecks
3. 将已经过验证的用户信息封装成 UsernamePasswordAuthenticationToken 对象并返回;该对象封装了用户的身份信息,以及相应的权限信息,相关源码以下,
```java
protected Authentication createSuccessAuthentication(Object principal,
UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken(
principal, authentication.getCredentials(),
authoritiesMapper.mapAuthorities(user.getAuthorities()));
result.setDetails(authentication.getDetails());
return result;
}
复制代码
UserDetailsService
是一个接口,提供了一个方法
public interface UserDetailsService {
UserDetails loadUserByUsername(String username) throws UsernameNotFoundException;
}
复制代码
经过用户名 username 调用方法 loadUserByUsername 返回了一个UserDetails接口对象(对应AbstractUserDetailsAuthenticationProvider
的三步验证方法);
public interface UserDetails extends Serializable {
#1.权限集合
Collection<? extends GrantedAuthority> getAuthorities();
#2.密码
String getPassword();
#3.用户民
String getUsername();
#4.用户是否过时
boolean isAccountNonExpired();
#5.是否锁定
boolean isAccountNonLocked();
#6.用户密码是否过时
boolean isCredentialsNonExpired();
#7.帐号是否可用(可理解为是否删除)
boolean isEnabled();
}
复制代码
Spring 为UserDetailsService
默认提供了一个实现类 org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl
该实现类主要是提供基于JDBC
对 User 进行增、删、查、改的方法
public class JdbcUserDetailsManager extends JdbcDaoImpl implements UserDetailsManager,
GroupManager {
// ~ Static fields/initializers
// =====================================================================================
// UserDetailsManager SQL
#1.定义了一些列对数据库操做的语句
public static final String DEF_CREATE_USER_SQL = "insert into users (username, password, enabled) values (?,?,?)";
public static final String DEF_DELETE_USER_SQL = "delete from users where username = ?";
public static final String DEF_UPDATE_USER_SQL = "update users set password = ?, enabled = ? where username = ?";
public static final String DEF_INSERT_AUTHORITY_SQL = "insert into authorities (username, authority) values (?,?)";
public static final String DEF_DELETE_USER_AUTHORITIES_SQL = "delete from authorities where username = ?";
public static final String DEF_USER_EXISTS_SQL = "select username from users where username = ?";
public static final String DEF_CHANGE_PASSWORD_SQL = "update users set password = ? where username = ?";
复制代码
该实现类主要是提供基于内存
对 User 进行增、删、查、改的方法 `public class InMemoryUserDetailsManager implements UserDetailsManager { protected final Log logger = LogFactory.getLog(getClass()); #1.用MAP 存储 private final Map<String, MutableUserDetails> users = new HashMap<String, MutableUserDetails>();
private AuthenticationManager authenticationManager;
public InMemoryUserDetailsManager() {
}
public InMemoryUserDetailsManager(Collection<UserDetails> users) {
for (UserDetails user : users) {
createUser(user);
}
}`
复制代码
UserDetailsService
接口做为桥梁,是DaoAuthenticationProvier
与特定用户信息来源进行解耦的地方,UserDetailsService
由UserDetails
和UserDetailsManage
r所构成;UserDetails
和UserDetailsManager
各司其责,一个是对基本用户信息进行封装,一个是对基本用户信息进行管理;
特别注意
,UserDetailsService
、UserDetails
以及UserDetailsManager
都是可被用户自定义的扩展点,咱们能够继承这些接口提供本身的读取用户来源和管理用户的方法,好比咱们能够本身实现一个 与特定 ORM 框架,好比 Mybatis 或者 Hibernate,相关的UserDetailsService
和UserDetailsManager
;