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