一直以来都想好好写一写spring security 系列文章,往往提笔又不知何处下笔,又赖于spring security体系强大又过于繁杂,且spring security 与auth2.0结合的时候又很难理解,最近项目告一段落,闲来无事好好对spring security 进行总结和回顾。java
主要介绍一些在Spring Security中常见且核心的Java类,这些类之间的关系构建起了整个spring security框架,所以首先来熟知这些类是很是有必要的。web
SecurityContextHolder它持有的是安全上下文(security context)的信息。当前操做的用户是谁,该用户是否已经被认证,他拥有哪些角色权等等,这些都被保存在SecurityContextHolder中。SecurityContextHolder默认使用ThreadLocal 策略来存储认证信息。看到ThreadLocal 也就意味着,这是一种与线程绑定的策略。在web环境下,Spring Security在用户登陆时自动绑定认证信息到当前线程,在用户退出时,自动清除当前线程的认证信息。
获取当前用户的信息redis
Object principal =SecurityContextHolder.getContext().getAuthentication().getPrincipal(); if (principal instanceof UserDetails) { String username = ((UserDetails)principal).getUsername(); } else { String username = principal.toString(); }
getAuthentication()返回了认证信息,getPrincipal()返回了身份信息,UserDetails即是Spring对身份信息封装的一个接口。spring
安全上下文,主要持有Authentication对象,若是用户未鉴权,那Authentication对象将会是空的。
public interface SecurityContext extends Serializable { // ~ Methods // ======================================================================================================== /** * Obtains the currently authenticated principal, or an authentication request token. * * @return the <code>Authentication</code> or <code>null</code> if no authentication * information is available */ Authentication getAuthentication(); /** * Changes the currently authenticated principal, or removes the authentication * information. * * @param authentication the new <code>Authentication</code> token, or * <code>null</code> if no further authentication information should be stored */ void setAuthentication(Authentication authentication); }
鉴权对象,该对象主要包含了用户的详细信息(UserDetails)和用户鉴权时所须要的信息,如用户提交的用户名密码、Remember-me Token,或者digest hash值等,按不一样鉴权方式使用不一样的Authentication实现。
public interface Authentication extends Principal, Serializable { // ~ Methods // ======================================================================================================== /** * Set by an <code>AuthenticationManager</code> to indicate the authorities that the * principal has been granted. Note that classes should not rely on this value as * being valid unless it has been set by a trusted <code>AuthenticationManager</code>. * <p> * Implementations should ensure that modifications to the returned collection array * do not affect the state of the Authentication object, or use an unmodifiable * instance. * </p> * * @return the authorities granted to the principal, or an empty collection if the * token has not been authenticated. Never null. */ Collection<? extends GrantedAuthority> getAuthorities(); /** * The credentials that prove the principal is correct. This is usually a password, * but could be anything relevant to the <code>AuthenticationManager</code>. Callers * are expected to populate the credentials. * * @return the credentials that prove the identity of the <code>Principal</code> */ Object getCredentials(); /** * Stores additional details about the authentication request. These might be an IP * address, certificate serial number etc. * * @return additional details about the authentication request, or <code>null</code> * if not used */ Object getDetails(); /** * The identity of the principal being authenticated. In the case of an authentication * request with username and password, this would be the username. Callers are * expected to populate the principal for an authentication request. * <p> * The <tt>AuthenticationManager</tt> implementation will often return an * <tt>Authentication</tt> containing richer information as the principal for use by * the application. Many of the authentication providers will create a * {@code UserDetails} object as the principal. * * @return the <code>Principal</code> being authenticated or the authenticated * principal after authentication. */ Object getPrincipal(); /** * Used to indicate to {@code AbstractSecurityInterceptor} whether it should present * the authentication token to the <code>AuthenticationManager</code>. Typically an * <code>AuthenticationManager</code> (or, more often, one of its * <code>AuthenticationProvider</code>s) will return an immutable authentication token * after successful authentication, in which case that token can safely return * <code>true</code> to this method. Returning <code>true</code> will improve * performance, as calling the <code>AuthenticationManager</code> for every request * will no longer be necessary. * <p> * For security reasons, implementations of this interface should be very careful * about returning <code>true</code> from this method unless they are either * immutable, or have some way of ensuring the properties have not been changed since * original creation. * * @return true if the token has been authenticated and the * <code>AbstractSecurityInterceptor</code> does not need to present the token to the * <code>AuthenticationManager</code> again for re-authentication. */ boolean isAuthenticated(); /** * See {@link #isAuthenticated()} for a full description. * <p> * Implementations should <b>always</b> allow this method to be called with a * <code>false</code> parameter, as this is used by various classes to specify the * authentication token should not be trusted. If an implementation wishes to reject * an invocation with a <code>true</code> parameter (which would indicate the * authentication token is trusted - a potential security risk) the implementation * should throw an {@link IllegalArgumentException}. * * @param isAuthenticated <code>true</code> if the token should be trusted (which may * result in an exception) or <code>false</code> if the token should not be trusted * * @throws IllegalArgumentException if an attempt to make the authentication token * trusted (by passing <code>true</code> as the argument) is rejected due to the * implementation being immutable or implementing its own alternative approach to * {@link #isAuthenticated()} */ void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException; }
一、 Authentication是spring security包中的接口,直接继承自Principal类,而Principal是位于java.security包中的。能够见得,Authentication在spring security中是最高级别的身份/认证的抽象。
二、 由这个顶级接口,咱们能够获得用户拥有的权限信息列表,密码,用户细节信息,用户身份信息,认证信息。
三、getAuthorities(),权限信息列表,默认是GrantedAuthority接口的一些实现类,一般是表明权限信息的一系列字符串。
四、getCredentials(),密码信息,用户输入的密码字符串,在认证事后一般会被移除,用于保障安全。
五、getDetails(),细节信息,web应用中的实现接口一般为 WebAuthenticationDetails,它记录了访问者的ip地址和sessionId的值。
六、getPrincipal(),敲黑板!!!最重要的身份信息,大部分状况下返回的是UserDetails接口的实现类,也是框架中的经常使用接口之一。数据库
该接口表示了当前用户所拥有的权限(或者角色)信息。这些信息由受权负责对象AccessDecisionManager来使用,并决定最终用户是否能够访问某资源(URL或方法调用或域对象)。鉴权时并不会使用到该对象。
这个接口规范了用户详细信息所拥有的字段,譬如用户名、密码、帐号是否过时、是否锁定等。在Spring Security中,获取当前登陆的用户的信息,通常状况是须要在这个接口上面进行扩展,用来对接本身系统的用户
public interface UserDetails extends Serializable { // ~ Methods // ======================================================================================================== /** * Returns the authorities granted to the user. Cannot return <code>null</code>. * * @return the authorities, sorted by natural key (never <code>null</code>) */ Collection<? extends GrantedAuthority> getAuthorities(); /** * Returns the password used to authenticate the user. * * @return the password */ String getPassword(); /** * Returns the username used to authenticate the user. Cannot return <code>null</code> * . * * @return the username (never <code>null</code>) */ String getUsername(); /** * Indicates whether the user's account has expired. An expired account cannot be * authenticated. * * @return <code>true</code> if the user's account is valid (ie non-expired), * <code>false</code> if no longer valid (ie expired) */ boolean isAccountNonExpired(); /** * Indicates whether the user is locked or unlocked. A locked user cannot be * authenticated. * * @return <code>true</code> if the user is not locked, <code>false</code> otherwise */ boolean isAccountNonLocked(); /** * Indicates whether the user's credentials (password) has expired. Expired * credentials prevent authentication. * * @return <code>true</code> if the user's credentials are valid (ie non-expired), * <code>false</code> if no longer valid (ie expired) */ boolean isCredentialsNonExpired(); /** * Indicates whether the user is enabled or disabled. A disabled user cannot be * authenticated. * * @return <code>true</code> if the user is enabled, <code>false</code> otherwise */ boolean isEnabled(); }
这个接口只提供一个接口loadUserByUsername(String username),这个接口很是重要,通常状况咱们都是经过扩展这个接口来显示获取咱们的用户信息,用户登录时传递的用户名和密码也是经过这里这查找出来的用户名和密码进行校验,可是真正的校验不在这里,而是由AuthenticationManager以及AuthenticationProvider负责的,须要强调的是,若是用户不存在,不该返回NULL,而要抛出异常UsernameNotFoundException
public interface UserDetailsService { // ~ Methods // ======================================================================================================== /** * Locates the user based on the username. In the actual implementation, the search * may possibly be case sensitive, or case insensitive depending on how the * implementation instance is configured. In this case, the <code>UserDetails</code> * object that comes back may have a username that is of a different case than what * was actually requested.. * * @param username the username identifying the user whose data is required. * * @return a fully populated user record (never <code>null</code>) * * @throws UsernameNotFoundException if the user could not be found or the user has no * GrantedAuthority */ UserDetails loadUserByUsername(String username) throws UsernameNotFoundException; }
一、用户名和密码被过滤器获取到,封装成Authentication,一般状况下是UsernamePasswordAuthenticationToken这个实现类。缓存
二、AuthenticationManager 身份管理器负责验证这个Authentication安全
三、认证成功后,AuthenticationManager身份管理器返回一个被填充满了信息的(包括上面提到的权限信息,身份信息,细节信息,但密码一般会被移除)Authentication实例。session
四、SecurityContextHolder安全上下文容器将第3步填充了信息的Authentication,经过SecurityContextHolder.getContext().setAuthentication()方法,设置到其中。app
初次接触Spring Security的朋友相信会被AuthenticationManager,ProviderManager ,AuthenticationProvider …这么多类似的Spring认证类搞得晕头转向,但只要稍微梳理一下就能够理解清楚它们的联系和设计者的用意。AuthenticationManager(接口)是认证相关的核心接口,也是发起认证的出发点,由于在实际需求中,咱们可能会容许用户使用用户名+密码登陆,同时容许用户使用邮箱+密码,手机号码+密码登陆,甚至,可能容许用户使用指纹登陆(还有这样的操做?没想到吧),因此说AuthenticationManager通常不直接认证,AuthenticationManager接口的经常使用实现类ProviderManager 内部会维护一个List<AuthenticationProvider>列表,存放多种认证方式,实际上这是委托者模式的应用(Delegate)。也就是说,核心的认证入口始终只有一个:AuthenticationManager,不一样的认证方式:用户名+密码(UsernamePasswordAuthenticationToken),邮箱+密码,手机号码+密码登陆则对应了三个AuthenticationProvider。这样一来就好理解多了?。
其中AuthenticationProvider下验证登录的关键源代码以下:框架
public Authentication authenticate(Authentication authentication) throws AuthenticationException { Class<? extends Authentication> toTest = authentication.getClass(); AuthenticationException lastException = null; Authentication result = null; boolean debug = logger.isDebugEnabled(); //依次进行验证 for (AuthenticationProvider provider : getProviders()) { if (!provider.supports(toTest)) { continue; } if (debug) { logger.debug("Authentication attempt using " + provider.getClass().getName()); } try { result = provider.authenticate(authentication); // 若是有Authentication信息,则直接返回 if (result != null) { 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 { 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; } } if (result != null) { if (eraseCredentialsAfterAuthentication && (result instanceof CredentialsContainer)) { //移除密码 ((CredentialsContainer) result).eraseCredentials(); } //发布登陆成功事件 eventPublisher.publishAuthenticationSuccess(result); return result; } //执行到此,说明没有认证成功,包装异常信息 if (lastException == null) { lastException = new ProviderNotFoundException(messages.getMessage( "ProviderManager.providerNotFound", new Object[] { toTest.getName() }, "No AuthenticationProvider found for {0}")); } prepareException(lastException, authentication); throw lastException; }
上面不断提到了UserDetails这个接口,它表明了最详细的用户信息,这个接口涵盖了一些必要的用户信息字段,咱们通常都须要对它进行必要的扩展。
它和Authentication接口很相似,好比它们都拥有username,authorities,区分他们也是本文的重点内容之一。Authentication的getCredentials()与UserDetails中的getPassword()须要被区分对待,前者是用户提交的密码凭证,后者是用户正确的密码,认证器其实就是对这二者的比对。Authentication中的getAuthorities()实际是由UserDetails的getAuthorities()传递而造成的。还记得Authentication接口中的getUserDetails()方法吗?其中的UserDetails用户详细信息即是通过了AuthenticationProvider以后被填充的。
UserDetailsService和AuthenticationProvider二者的职责经常被人们搞混,UserDetailsService只负责从特定的地方加载用户信息,能够是数据库、redis缓存、接口等
最后附上核心类图一张