java编写登录接口
时间: 2023-08-12 15:03:26 浏览: 50
好的,下面是一个简单的Java登录接口示例,仅供参考:
```
import java.util.HashMap;
import java.util.Map;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class LoginController {
private static final Map<String, String> userMap = new HashMap<>(); //模拟用户信息
static {
userMap.put("user1", "password1");
userMap.put("user2", "password2");
userMap.put("user3", "password3");
}
@PostMapping("/login")
public ResponseEntity<String> login(@RequestBody LoginRequest loginRequest) {
String username = loginRequest.getUsername();
String password = loginRequest.getPassword();
if (userMap.containsKey(username) && userMap.get(username).equals(password)) {
return new ResponseEntity<>("Login Successful", HttpStatus.OK);
} else {
return new ResponseEntity<>("Invalid Username or Password", HttpStatus.UNAUTHORIZED);
}
}
public static class LoginRequest {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
}
```
这个接口使用了Spring Boot框架,其中包括一个`/login`的POST请求,用于接收包含用户名和密码的JSON请求体,并返回登录结果。在实际应用中,你需要更改`userMap`来匹配你自己的用户信息。