當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Security3源码分析-UsernamePasswordAuthenticationFilter分析
生活随笔
收集整理的這篇文章主要介紹了
Spring Security3源码分析-UsernamePasswordAuthenticationFilter分析
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
UsernamePasswordAuthenticationFilter過濾器對應的類路徑為org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
實際上這個Filter類的doFilter是父類AbstractAuthenticationProcessingFilter的
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)throws IOException, ServletException {HttpServletRequest request = (HttpServletRequest) req;HttpServletResponse response = (HttpServletResponse) res;//判斷form-login標簽是否包含login-processing-url屬性//如果沒有采用默認的url:j_spring_security_check//如果攔截的url不需要認證,直接跳過if (!requiresAuthentication(request, response)) {chain.doFilter(request, response);return;}if (logger.isDebugEnabled()) {logger.debug("Request is to process authentication");}Authentication authResult;try {//由子類完成認證authResult = attemptAuthentication(request, response);if (authResult == null) {// return immediately as subclass has indicated that it hasn't completed authenticationreturn;}//session策略處理認證信息//sessionStrategy是通過session-management標簽中定義的//session管理策略構造的SessionAuthenticationStrategy//具體的session管理比較復雜,部分后面單個篇幅講解sessionStrategy.onAuthentication(authResult, request, response);}catch (AuthenticationException failed) {// Authentication failed//認證失敗處理unsuccessfulAuthentication(request, response, failed);return;}// Authentication successif (continueChainBeforeSuccessfulAuthentication) {chain.doFilter(request, response);}//認證成功處理//1.向SecurityContext中設置Authentication認證信息//2.如果有remember me服務,則查找請求參數中是否包含_spring_security_remember_me,如果該參數值為true、yes、on、1則執行remember me功能:添加cookie、入庫。為下次請求時自動登錄做準備//3.發布認證成功事件//4.執行跳轉successfulAuthentication(request, response, authResult);}
子類UsernamePasswordAuthenticationFilter的認證方法attemptAuthentication
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {//只處理post提交的請求if (postOnly && !request.getMethod().equals("POST")) {throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());}//獲取用戶名、密碼數據String username = obtainUsername(request);String password = obtainPassword(request);if (username == null) {username = "";}if (password == null) {password = "";}username = username.trim();//構造未認證的UsernamePasswordAuthenticationTokenUsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);// Place the last username attempted into HttpSession for viewsHttpSession session = request.getSession(false);//如果session不為空,添加username到session中if (session != null || getAllowSessionCreation()) {request.getSession().setAttribute(SPRING_SECURITY_LAST_USERNAME_KEY, TextEscapeUtils.escapeEntities(username));}// Allow subclasses to set the "details" property//設置details,這里就是設置org.springframework.security.web.//authentication.WebAuthenticationDetails實例到details中setDetails(request, authRequest);//通過AuthenticationManager:ProviderManager完成認證任務return this.getAuthenticationManager().authenticate(authRequest);}
這里的authenticationManager變量也是通過解析form-login標簽,構造bean時注入的,具體解析類為:org.springframework.security.config.http.AuthenticationConfigBuilder
代碼片段為:
void createFormLoginFilter(BeanReference sessionStrategy, BeanReference authManager) {Element formLoginElt = DomUtils.getChildElementByTagName(httpElt, Elements.FORM_LOGIN);if (formLoginElt != null || autoConfig) {FormLoginBeanDefinitionParser parser = new FormLoginBeanDefinitionParser("/j_spring_security_check",AUTHENTICATION_PROCESSING_FILTER_CLASS, requestCache, sessionStrategy);parser.parse(formLoginElt, pc);formFilter = parser.getFilterBean();formEntryPoint = parser.getEntryPointBean();}if (formFilter != null) {formFilter.getPropertyValues().addPropertyValue("allowSessionCreation", new Boolean(allowSessionCreation));//設置authenticationManager的bean依賴formFilter.getPropertyValues().addPropertyValue("authenticationManager", authManager);// Id is required by login page filterformFilterId = pc.getReaderContext().generateBeanName(formFilter);pc.registerBeanComponent(new BeanComponentDefinition(formFilter, formFilterId));injectRememberMeServicesRef(formFilter, rememberMeServicesId);}}
繼續看ProviderManager代碼。實際上authenticate方法由ProviderManager的父類定義,并且authenticate方法內調用子類的doAuthentication方法,記得這是設計模式中的模板模式
public Authentication doAuthentication(Authentication authentication) throws AuthenticationException {Class<? extends Authentication> toTest = authentication.getClass();AuthenticationException lastException = null;Authentication result = null;//循環ProviderManager中的providers,由具體的provider執行認證操作for (AuthenticationProvider provider : getProviders()) {System.out.println("AuthenticationProvider: " + provider.getClass().getName());if (!provider.supports(toTest)) {continue;}logger.debug("Authentication attempt using " + provider.getClass().getName());try {result = provider.authenticate(authentication);if (result != null) {//復制detailscopyDetails(authentication, result);break;}} catch (AccountStatusException e) {// SEC-546: Avoid polling additional providers if auth failure is due to invalid account statuseventPublisher.publishAuthenticationFailure(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) {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}"));}//由注入進來的org.springframework.security.authentication.DefaultAuthenticationEventPublisher完成事件發布任務eventPublisher.publishAuthenticationFailure(lastException, authentication);throw lastException;}
ProviderManager類中的providers由哪些provider呢?如果看完authentication-manager標簽解析的講解,應該知道注入到providers中的provider分別為:
org.springframework.security.authentication.dao.DaoAuthenticationProvider
org.springframework.security.authentication.AnonymousAuthenticationProvider
其他的provider根據特殊情況,再添加到providers中的,如remember me功能的provider
org.springframework.security.authentication.RememberMeAuthenticationProvider
可以看出來,ProviderManager僅僅是管理provider的,具體的authenticate認證任務由各自provider來完成。
現在來看DaoAuthenticationProvider的認證處理,實際上authenticate由父類AbstractUserDetailsAuthenticationProvider完成。代碼如下
public Authentication authenticate(Authentication authentication) throws AuthenticationException {…………//獲取登錄的用戶名String username = (authentication.getPrincipal() == null) ? "NONE_PROVIDED" : authentication.getName();boolean cacheWasUsed = true;//如果配置了緩存,從緩存中獲取UserDetails實例UserDetails user = this.userCache.getUserFromCache(username);if (user == null) {cacheWasUsed = false;try {//如果UserDetails為空,則由具體子類DaoAuthenticationProvider//根據用戶名、authentication獲取UserDetailsuser = retrieveUser(username, (UsernamePasswordAuthenticationToken) authentication);} catch (UsernameNotFoundException notFound) {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 {//一些認證檢查(賬號是否可用、是否過期、是否被鎖定)preAuthenticationChecks.check(user);//額外的密碼檢查(salt、passwordEncoder)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;}}//檢查賬號是否過期postAuthenticationChecks.check(user);//添加UserDetails到緩存中if (!cacheWasUsed) {this.userCache.putUserInCache(user);}Object principalToReturn = user;if (forcePrincipalAsString) {principalToReturn = user.getUsername();}//返回成功認證后的Authenticationreturn createSuccessAuthentication(principalToReturn, authentication, user);}
繼續看DaoAuthenticationProvider的retrieveUser方法
protected final UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication)throws AuthenticationException {UserDetails loadedUser;try {//最關鍵的部分登場了//UserDetailService就是authentication-provider標簽中定義的//屬性user-service-refloadedUser = this.getUserDetailsService().loadUserByUsername(username);}catch (DataAccessException repositoryProblem) {throw new AuthenticationServiceException(repositoryProblem.getMessage(), repositoryProblem);}if (loadedUser == null) {throw new AuthenticationServiceException("UserDetailsService returned null, which is an interface contract violation");}return loadedUser;}
實際上,只要實現UserDetailsService接口的loadUserByUsername方法,就完成了登錄認證的工作
<authentication-manager alias="authenticationManager"><authentication-provider user-service-ref="userDetailsManager"/> </authentication-manager>
很多教程上說配置JdbcUserDetailsManager這個UserDetailsService,實際上該類的父類
JdbcDaoImpl方法loadUserByUsername代碼如下:
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {//根據username從數據庫中查詢User數據List<UserDetails> users = loadUsersByUsername(username);if (users.size() == 0) {throw new UsernameNotFoundException(messages.getMessage("JdbcDaoImpl.notFound", new Object[]{username}, "Username {0} not found"), username);}UserDetails user = users.get(0); // contains no GrantedAuthority[]Set<GrantedAuthority> dbAuthsSet = new HashSet<GrantedAuthority>();//添加授權信息if (enableAuthorities) {dbAuthsSet.addAll(loadUserAuthorities(user.getUsername()));}//是否使用了Groupif (enableGroups) {dbAuthsSet.addAll(loadGroupAuthorities(user.getUsername()));}List<GrantedAuthority> dbAuths = new ArrayList<GrantedAuthority>(dbAuthsSet);addCustomAuthorities(user.getUsername(), dbAuths);if (dbAuths.size() == 0) {throw new UsernameNotFoundException(messages.getMessage("JdbcDaoImpl.noAuthority",new Object[] {username}, "User {0} has no GrantedAuthority"), username);}return createUserDetails(username, user, dbAuths);}//usersByUsernameQuery查詢語句可配置//直接從數據庫中查詢該username對應的數據,并構造User對象protected List<UserDetails> loadUsersByUsername(String username) {return getJdbcTemplate().query(usersByUsernameQuery, new String[] {username}, new RowMapper<UserDetails>() {public UserDetails mapRow(ResultSet rs, int rowNum) throws SQLException {String username = rs.getString(1);String password = rs.getString(2);boolean enabled = rs.getBoolean(3);return new User(username, password, enabled, true, true, true, AuthorityUtils.NO_AUTHORITIES);}});}……protected UserDetails createUserDetails(String username, UserDetails userFromUserQuery,List<GrantedAuthority> combinedAuthorities) {String returnUsername = userFromUserQuery.getUsername();if (!usernameBasedPrimaryKey) {returnUsername = username;}//根據用戶名、密碼、enabled、授權列表構造UserDetails實例Userreturn new User(returnUsername, userFromUserQuery.getPassword(), userFromUserQuery.isEnabled(),true, true, true, combinedAuthorities);}
其他的provider,如
RememberMeAuthenticationProvider、AnonymousAuthenticationProvider的認證處理都很簡單,首先判斷是否支持Authentication,不支持直接返回null,支持也不處理直接返回該Authentication
這里需要強調一下,DaoAuthenticationProvider只支持UsernamePasswordAuthenticationToken這個Authentication。如果對其他的Authentication,DaoAuthenticationProvider是不做處理的
轉載于:https://my.oschina.net/snakerflow/blog/194515
總結
以上是生活随笔為你收集整理的Spring Security3源码分析-UsernamePasswordAuthenticationFilter分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Server Hard drive mo
- 下一篇: 关于写博客的原因以及一点个人说明。