成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Spring Security 實戰干貨:Spring Security中的單元測試

開發 前端
今天組里的新人迷茫的問我:哥,Spring Security弄的我單元測試跑不起來,總是401,你看看咋解決。沒問題,有寫單元測試的覺悟,寫的代碼質量肯定有保證,對代碼質量重視的態度,這種忙一定要幫!

[[395171]]

今天組里的新人迷茫的問我:哥,Spring Security弄的我單元測試跑不起來,總是401,你看看咋解決。沒問題,有寫單元測試的覺悟,寫的代碼質量肯定有保證,對代碼質量重視的態度,這種忙一定要幫!

Spring Security 測試環境

要想在單元測試中使用Spring Security,你需要在Spring Boot項目中集成:

  1. <dependency> 
  2.             <groupId>org.springframework.security</groupId> 
  3.             <artifactId>spring-security-test</artifactId> 
  4.             <scope>test</scope> 
  5.         </dependency> 

 

這樣測試的上下文配置就能和Spring Security結合起來了,接下來教你幾招。

Spring Security 測試

所有的測試都是在Spring Boot Test下進行的,也就是@SpringBootTest注解的支持下。

@WithMockUser

@WithMockUser注解可以幫我們在Spring Security安全上下文中模擬一個默認名稱為user,默認密碼為password,默認角色為USER的用戶。當你的測試方法使用了該注解后,你就能通過:

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

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

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

當然你可以將@WithMockUser標記到整個測試類上,這樣每個測試都將使用指定該用戶。

@WithAnonymousUser

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

@WithUserDetails

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

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

當我們執行單元測試時,將通過UserDetailsService 的loadUserByUsername方法查找用戶名為felord的用戶并加載到安全上下文中。

自定義注解

其實我們還可以模擬@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.  
  8.    String value() default "user"
  9.  
  10.    String username() default ""
  11.  
  12.    String[] roles() default { "USER" }; 
  13.   
  14.    String[] authorities() default {}; 
  15.   
  16.    String password() default "password"
  17.   
  18.    @AliasFor(annotation = WithSecurityContext.class) 
  19.    TestExecutionEvent setupBefore() default TestExecutionEvent.TEST_METHOD; 
  20.  

關鍵就在于@WithSecurityContext注解,我們只需要實現factory就行了,也就是:

  1. public interface WithSecurityContextFactory<A extends Annotation> { 
  2.   
  3.    SecurityContext createSecurityContext(A annotation); 

這里如法炮制就行,沒什么難度就不演示了。

總結

 

今天介紹了當你的應用中集成了Spring Security時如何單元測試,我們可以使用提供的模擬用戶的注解,也可以模擬加載用戶,甚至你可以根據自己的需要來定制化。其實如果你使用了JWT的話還有種野路子,你可以在Spring MVC Mock測試中加入對應的請求頭或者參數,也能順利進行。

 

責任編輯:武曉燕 來源: 碼農小胖哥
相關推薦

2021-04-19 07:33:04

WebSecuritySpringHttpSecurit

2021-01-28 09:50:29

分布式對象SharedObjec

2021-05-31 07:18:46

SpringSecurity信息

2013-06-04 09:49:04

Spring單元測試軟件測試

2019-11-22 09:40:40

SpringJava編程語言

2021-09-01 12:03:49

Spring單元測試

2021-08-29 18:36:57

項目

2025-06-30 01:33:00

2021-06-07 14:06:19

Spring SecuCSRF防御

2022-11-26 00:00:02

2020-09-16 08:07:54

權限粒度Spring Secu

2023-04-10 11:41:15

2022-05-05 10:40:36

Spring權限對象

2022-08-30 08:50:07

Spring權限控制

2009-06-18 14:18:23

Spring secu

2022-01-26 00:05:00

接口Spring管理器

2020-06-17 08:31:10

權限控制Spring Secu

2021-07-27 10:49:10

SpringSecurity權限

2022-08-30 08:36:13

Spring權限控制

2022-08-15 08:45:21

Spring權限控制
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 欧美黄 片免费观看 | 人人九九精 | 国产免费一区二区三区最新6 | 日韩精品视频在线免费观看 | 男女羞羞视频大全 | 97国产在线视频 | 激情六月丁香 | 亚洲人人舔人人 | 特级做a爱片免费69 精品国产鲁一鲁一区二区张丽 | 中文字幕免费在线 | 中文字幕精品一区久久久久 | 日韩欧美二区 | 国产精品自产av一区二区三区 | 夜夜骑首页 | 91亚洲精品在线观看 | 在线视频一区二区 | 一区二区三区四区免费观看 | 免费的av网站 | 午夜视频一区二区三区 | 在线播放国产一区二区三区 | 成年人在线观看 | 国产成人精品一区二区三区 | 精品国产乱码久久久久久88av | 久久久久一区二区三区四区 | 久草视 | 成人在线视频网站 | 91精品国产综合久久婷婷香蕉 | 在线a视频 | 亚洲 欧美 日韩 精品 | 久久精品久久久久久 | 午夜视频精品 | 国产综合在线视频 | 日韩免费视频 | 欧美视频在线免费 | 亚洲黄色高清视频 | 久久网站免费视频 | 韩日一区二区三区 | 亚洲97| 亚洲激精日韩激精欧美精品 | 精品国产91亚洲一区二区三区www | 中文在线视频 |