Compare commits
No commits in common. "5399b6eb75eb81c46849c2aa22fade86b97cd75c" and "ed74e3ed80e400e04d45326712ec8485a2b94198" have entirely different histories.
5399b6eb75
...
ed74e3ed80
13
pom.xml
13
pom.xml
|
|
@ -43,23 +43,24 @@
|
|||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
<version>3.5.5</version>
|
||||
<version>3.5.4</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mybatis</groupId>
|
||||
<artifactId>mybatis-spring</artifactId>
|
||||
<version>3.0.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
<version>8.0.33</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
<version>8.0.33</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
|||
|
|
@ -4,8 +4,6 @@ import com.gao.finalhw.model.User;
|
|||
import com.gao.finalhw.pojo.LoginRequest;
|
||||
import com.gao.finalhw.response.ServerResponseEntity;
|
||||
import com.gao.finalhw.service.UserService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
|
@ -13,19 +11,16 @@ import org.springframework.web.bind.annotation.RestController;
|
|||
|
||||
@RestController
|
||||
public class UserController {
|
||||
private final Logger logger = LoggerFactory.getLogger(UserController.class);
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
@PostMapping("/login")
|
||||
public ServerResponseEntity<User> login(@RequestBody LoginRequest loginRequest) {
|
||||
logger.info("username: {},password: {}",loginRequest.getUsername(),loginRequest.getPassword());
|
||||
User user = userService.getUserByUsernameAndPassword(loginRequest.getUsername(), loginRequest.getPassword());
|
||||
logger.info("user: {}",user.toString());
|
||||
if (user != null) {
|
||||
// 隐藏密码
|
||||
user.setPassword("null");
|
||||
// 如果验证成功
|
||||
return ServerResponseEntity.success("1", user);
|
||||
return ServerResponseEntity.success("login success", user);
|
||||
} else {
|
||||
// 如果验证失败
|
||||
return ServerResponseEntity.fail("username or password error.");
|
||||
|
|
|
|||
|
|
@ -14,6 +14,8 @@ import org.apache.ibatis.annotations.Select;
|
|||
@Mapper
|
||||
public interface UserMapper extends BaseMapper<User> {
|
||||
|
||||
@Select("SELECT * FROM user WHERE user_name = #{username} AND password = #{password}")
|
||||
User findByUsernameAndPassword(String username, String password);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -76,12 +76,11 @@ public class ServerResponseEntity <T> implements Serializable {
|
|||
ServerResponseEntity<T> serverResponseEntity = new ServerResponseEntity<>();
|
||||
serverResponseEntity.setCode(code);
|
||||
serverResponseEntity.setData(data);
|
||||
serverResponseEntity.setMsg(ResponseEnum.OK.getMsg());
|
||||
serverResponseEntity.getMsg();
|
||||
serverResponseEntity.setTimestamp(Instant.now().getEpochSecond());
|
||||
return serverResponseEntity;
|
||||
}
|
||||
|
||||
|
||||
public static <T> ServerResponseEntity<T> showFailMsg(String msg) {
|
||||
logger.error(msg);
|
||||
ServerResponseEntity<T> serverResponseEntity = new ServerResponseEntity<>();
|
||||
|
|
|
|||
|
|
@ -1,28 +1,16 @@
|
|||
package com.gao.finalhw.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.gao.finalhw.mapper.UserMapper;
|
||||
import com.gao.finalhw.model.User;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
private final Logger logger = LoggerFactory.getLogger(UserService.class);
|
||||
@Autowired
|
||||
private UserMapper userMapper;
|
||||
|
||||
public User getUserByUsernameAndPassword(String username, String password) {
|
||||
logger.info("start getUserByUsernameAndPassword");
|
||||
|
||||
QueryWrapper<User> wrapper = new QueryWrapper<>();
|
||||
wrapper.eq("user_name", username) // 假设字段名为 'username'
|
||||
.eq("password", password); // 假设字段名为 'password'
|
||||
|
||||
User result = userMapper.selectOne(wrapper);
|
||||
logger.info("query result: {}",result.toString());
|
||||
return result;
|
||||
return userMapper.findByUsernameAndPassword(username, password);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -135,7 +135,7 @@
|
|||
url: '/login',
|
||||
type: 'POST',
|
||||
contentType: 'application/json',
|
||||
data: JSON.stringify({username: email, password: password}),
|
||||
data: JSON.stringify({email: email, password: password}),
|
||||
success: function(response) {
|
||||
// 处理响应
|
||||
console.log(response);
|
||||
|
|
|
|||
Reference in New Issue