SpringBoot整合JWT实现登录认证( 二 )


import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
@RestController
public class LoginController {
private UserService userService;
@Autowired
public LoginController(UserService userService) {
this.userService = userService;
}
@PostMapping("/login")
public Result register(User user){
HashMap<String, String> map = new HashMap<>();
map.put("username",user.getUserName());
String token = JWTUtils.getToken(map);
HashMap<String, Object> data = https://www.isolves.com/it/cxkf/kj/2022-07-08/new HashMap<>();
data.put("token",token);
return ResultUtils.getresult(200,"登录成功!",data);
}
@GetMapping("/main")
public Result tomain(){
return ResultUtils.getresult(200,"访问成功",null);
}
}
 
2.6使用Postman测试
- 未登录前访问127.0.0.1:8888/main
- 先登录再访问127.0.0.1:8888/main

【SpringBoot整合JWT实现登录认证】


推荐阅读