av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

SpringSecurity實(shí)戰(zhàn)干貨:SpringSecurity中的單元測(cè)試

今天組里的新人迷茫的問(wèn)我:哥,Spring Security弄的我單元測(cè)試跑不起來(lái),總是401,你看看咋解決。沒(méi)問(wèn)題,有寫(xiě)單元測(cè)試的覺(jué)悟,寫(xiě)的代碼質(zhì)量肯定有保證,對(duì)代碼質(zhì)量重視的態(tài)度,這種忙一定要幫!

Spring Security 測(cè)試環(huán)境

要想在單元測(cè)試中使用Spring Security,你需要在Spring Boot項(xiàng)目中集成:

 
 
 
  1.             org.springframework.security
  2.             spring-security-test
  3.             test
  4.         

這樣測(cè)試的上下文配置就能和Spring Security結(jié)合起來(lái)了,接下來(lái)教你幾招。

Spring Security 測(cè)試

所有的測(cè)試都是在Spring Boot Test下進(jìn)行的,也就是@SpringBootTest注解的支持下。

@WithMockUser

@WithMockUser注解可以幫我們?cè)赟pring Security安全上下文中模擬一個(gè)默認(rèn)名稱為user,默認(rèn)密碼為password,默認(rèn)角色為USER的用戶。當(dāng)你的測(cè)試方法使用了該注解后,你就能通過(guò):

 
 
 
  1. Authentication authentication = SecurityContextHolder.getContext()
  2.            .getAuthentication();

獲取該模擬用戶的信息,也就“假裝”當(dāng)前登錄了用戶user。當(dāng)然你也可以根據(jù)需要來(lái)自定義用戶名、密碼、角色:

 
 
 
  1. @SneakyThrows
  2. @Test
  3. @WithMockUser(username = "felord",password = "felord.cn",roles = {"ADMIN"})
  4. void updatePassword() {
  5.     mockMvc.perform(post("/user/update/password")
  6.             .contentType(MediaType.APPLICATION_JSON)
  7.             .content("{\n" +
  8.                     "  \"newPassword\": \"12345\",\n" +
  9.                     "  \"oldPassword\": \"12345\"\n" +
  10.                     "}"))
  11.             .andExpect(ResultMatcher.matchAll(status().isOk()))
  12.             .andDo(print());
  13. }

當(dāng)然你可以將@WithMockUser標(biāo)記到整個(gè)測(cè)試類上,這樣每個(gè)測(cè)試都將使用指定該用戶。

@WithAnonymousUser

@WithAnonymousUser是用來(lái)模擬一種特殊的用戶,也被叫做匿名用戶。如果有測(cè)試匿名用戶的需要,可以直接使用該注解。其實(shí)等同于@WithMockUser(roles = {"ANONYMOUS"}),也等同于@WithMockUser(authorities = {"ROLE_ANONYMOUS"}),細(xì)心的你應(yīng)該能看出來(lái)差別。

@WithUserDetails

雖然@WithMockUser是一種非常方便的方式,但可能并非在所有情況下都湊效。有時(shí)候你魔改了一些東西使得安全上下文的驗(yàn)證機(jī)制發(fā)生了改變,比如你定制了UserDetails,這一類注解就不好用了。但是通過(guò)UserDetailsService 加載的用戶往往還是可靠的。于是@WithUserDetails就派上了用場(chǎng)。

 
 
 
  1. @SneakyThrows
  2. @Test
  3. @WithUserDetails("felord")
  4. void updatePassword() {
  5.     mockMvc.perform(post("/user/update/password")
  6.             .contentType(MediaType.APPLICATION_JSON)
  7.             .content("{\n" +
  8.                     "  \"newPassword\": \"12345\",\n" +
  9.                     "  \"oldPassword\": \"12345\"\n" +
  10.                     "}"))
  11.             .andExpect(ResultMatcher.matchAll(status().isOk()))
  12.             .andDo(print());
  13. }

當(dāng)我們執(zhí)行單元測(cè)試時(shí),將通過(guò)UserDetailsService 的loadUserByUsername方法查找用戶名為felord的用戶并加載到安全上下文中。

自定義注解

其實(shí)我們還可以模擬@WithMockUser

 
 
 
  1. @Target({ ElementType.METHOD, ElementType.TYPE })
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Inherited
  4. @Documented
  5. @WithSecurityContext(factory = WithMockUserSecurityContextFactory.class)
  6. public @interface WithMockUser {
  7.    String value() default "user";
  8.    String username() default "";
  9.    String[] roles() default { "USER" };
  10.  
  11.    String[] authorities() default {};
  12.  
  13.    String password() default "password";
  14.  
  15.    @AliasFor(annotation = WithSecurityContext.class)
  16.    TestExecutionEvent setupBefore() default TestExecutionEvent.TEST_METHOD;
  17. }

關(guān)鍵就在于@WithSecurityContext注解,我們只需要實(shí)現(xiàn)factory就行了,也就是:

 
 
 
  1. public interface WithSecurityContextFactory {
  2.  
  3.    SecurityContext createSecurityContext(A annotation);
  4. }

這里如法炮制就行,沒(méi)什么難度就不演示了。

總結(jié)

今天介紹了當(dāng)你的應(yīng)用中集成了Spring Security時(shí)如何單元測(cè)試,我們可以使用提供的模擬用戶的注解,也可以模擬加載用戶,甚至你可以根據(jù)自己的需要來(lái)定制化。其實(shí)如果你使用了JWT的話還有種野路子,你可以在Spring MVC Mock測(cè)試中加入對(duì)應(yīng)的請(qǐng)求頭或者參數(shù),也能順利進(jìn)行。


網(wǎng)站欄目:SpringSecurity實(shí)戰(zhàn)干貨:SpringSecurity中的單元測(cè)試
文章位置:http://uogjgqi.cn/article/djoipeh.html
掃二維碼與項(xiàng)目經(jīng)理溝通

我們?cè)谖⑿派?4小時(shí)期待你的聲音

解答本文疑問(wèn)/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流