Skip to content

Commit 81eb762

Browse files
committed
add junit test
1 parent 8fc9ee9 commit 81eb762

File tree

4 files changed

+91
-22
lines changed

4 files changed

+91
-22
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,41 @@
11
package com.codingapi.springboot.security;
22

3+
import lombok.extern.slf4j.Slf4j;
34
import org.springframework.boot.SpringApplication;
45
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.security.core.GrantedAuthority;
8+
import org.springframework.security.core.authority.SimpleGrantedAuthority;
9+
import org.springframework.security.core.userdetails.User;
10+
import org.springframework.security.core.userdetails.UserDetailsService;
11+
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
12+
import org.springframework.security.crypto.password.PasswordEncoder;
13+
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
514

15+
import java.util.ArrayList;
16+
import java.util.List;
17+
18+
@Slf4j
619
@SpringBootApplication
720
public class SecurityJwtApplication {
821

922
public static void main(String[] args) {
1023
SpringApplication.run(SecurityJwtApplication.class, args);
1124
}
25+
26+
@Bean
27+
public UserDetailsService myUserDetailsService(PasswordEncoder passwordEncoder) {
28+
InMemoryUserDetailsManager inMemoryUserDetailsManager = new InMemoryUserDetailsManager();
29+
List<GrantedAuthority> authorities = new ArrayList<>();
30+
authorities.add(new SimpleGrantedAuthority("ADMIN"));
31+
inMemoryUserDetailsManager.createUser(new User("admin", passwordEncoder.encode("123456"),authorities));
32+
return inMemoryUserDetailsManager;
33+
}
34+
35+
36+
@Bean
37+
public PasswordEncoder passwordEncoder() {
38+
return new BCryptPasswordEncoder();
39+
}
40+
1241
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,61 @@
11
package com.codingapi.springboot.security;
22

3+
import com.alibaba.fastjson.JSONObject;
4+
import lombok.extern.slf4j.Slf4j;
35
import org.junit.jupiter.api.Test;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
48
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.http.MediaType;
10+
import org.springframework.test.web.servlet.MockMvc;
11+
import org.springframework.test.web.servlet.MvcResult;
512

13+
import java.nio.charset.StandardCharsets;
14+
15+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
16+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
17+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
18+
import static org.junit.jupiter.api.Assertions.*;
19+
20+
@AutoConfigureMockMvc
621
@SpringBootTest
22+
@Slf4j
723
public class SecurityJwtApplicationTest {
824

25+
@Autowired
26+
private MockMvc mockMvc;
27+
28+
@Test
29+
void login() throws Exception {
30+
JSONObject json = new JSONObject();
31+
json.put("username","admin");
32+
json.put("password","123456");
33+
mockMvc.perform(post("/user/login").content(json.toJSONString().getBytes(StandardCharsets.UTF_8)).contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
34+
}
935

1036
@Test
11-
void test() {
37+
void noToken() throws Exception {
38+
mockMvc.perform(get("/api/hello").contentType(MediaType.APPLICATION_JSON)).andExpect(result -> {
39+
String body = result.getResponse().getContentAsString();
40+
JSONObject jsonObject = JSONObject.parseObject(body);
41+
log.info("body:{}",jsonObject);
42+
assertEquals(jsonObject.getString("errCode"),"token.error","token authentication error");
43+
});
44+
}
45+
46+
@Test
47+
void haveToken() throws Exception {
48+
49+
JSONObject json = new JSONObject();
50+
json.put("username","admin");
51+
json.put("password","123456");
52+
MvcResult mvcResult = mockMvc.perform(post("/user/login").content(json.toJSONString().getBytes(StandardCharsets.UTF_8)).contentType(MediaType.APPLICATION_JSON)).andReturn();
53+
JSONObject loginData = JSONObject.parseObject(mvcResult.getResponse().getContentAsString());
1254

55+
mockMvc.perform(get("/api/hello").header("Authorization",loginData.getJSONObject("data").getString("token")).contentType(MediaType.APPLICATION_JSON)).andExpect(result -> {
56+
String body = result.getResponse().getContentAsString();
57+
assertEquals(body,"hello","token authentication error");
58+
});
1359
}
1460

1561
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.codingapi.springboot.security.controller;
2+
3+
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
import org.springframework.web.bind.annotation.RestController;
6+
7+
@RestController
8+
@RequestMapping("/api")
9+
public class DemoController {
10+
11+
@GetMapping("/hello")
12+
public String hello(){
13+
return "hello";
14+
}
15+
}

springboot-starter-security-jwt/src/test/java/com/codingapi/springboot/security/jwt/TokenTest.java

-21
This file was deleted.

0 commit comments

Comments
 (0)