?????? JHipster單體應(yīng)用的登錄處理吕朵,集成了spring security。之前對 spring security不太了解键菱,這次遇到一些問題谬墙,把自己的一些了解記錄一下。
? ? ? ? 整體上單體不使用oauth的認證相對簡單不少经备,在SecurityConfiguration中做下配置:
.and()
.rememberMe()
.rememberMeServices(rememberMeServices)
.rememberMeParameter("remember-me")
.key(jHipsterProperties.getSecurity().getRememberMe().getKey())
.and()
.formLogin()
.loginProcessingUrl("/api/authentication")
.successHandler(ajaxAuthenticationSuccessHandler)
.failureHandler(ajaxAuthenticationFailureHandler)
.usernameParameter("j_username")
.passwordParameter("j_password")
.permitAll()
? ? ? ? 瀏覽器訪問url會經(jīng)過 AbstractAuthenticationProcessingFilter 其中會對url進行匹配處理(根據(jù)loginProcessingUrl值【猜測】)拭抬,所以登錄頁面的提交url是“api/authentication”(其中 用戶名、密碼 parameter名是"j_username"和"j_password") 如果匹配到是進行認證的url請求侵蒙,會調(diào)用 UsernamePasswordAuthenticationFilter(AbstractAuthenticationProcessingFilter的子類) 的 attemptAuthentication方法造虎,如果用戶名/密碼正確,會將用戶信息放在SecurityContextHolder.getContext()(默認使用ThreadLocal實現(xiàn))纷闺, 如果 使用了 rememberMe 特性算凿,后續(xù)的每次請求 會在 RememberMeAuthenticationFilter中 獲取 SecurityContextHolder.getContext()份蝴,獲取到用戶信息 就轉(zhuǎn)到后面的正常處理。