-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement User Login and Logout Support in Doecean Framework with Mon…
…goDB (close #836)
- Loading branch information
Showing
16 changed files
with
439 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,52 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>run.mone</groupId> | ||
<artifactId>docean-plugin</artifactId> | ||
<version>1.6.0-jdk21-SNAPSHOT</version> | ||
</parent> | ||
<artifactId>docean-plugin-mongodb</artifactId> | ||
<version>1.5.0-jdk21-SNAPSHOT</version> | ||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>run.mone</groupId> | ||
<artifactId>docean-plugin-config</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.mongodb</groupId> | ||
<artifactId>mongo-java-driver</artifactId> | ||
<version>3.8.0</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>run.mone</groupId> | ||
<artifactId>catPlugin</artifactId> | ||
<version>1.6.0-jdk21-SNAPSHOT</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>dev.morphia.morphia</groupId> | ||
<artifactId>morphia-core</artifactId> | ||
<version>2.4.13</version> | ||
</dependency> | ||
|
||
|
||
</dependencies> | ||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" | ||
xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>run.mone</groupId> | ||
<artifactId>docean-plugin</artifactId> | ||
<version>1.6.0-jdk21-SNAPSHOT</version> | ||
</parent> | ||
<artifactId>docean-plugin-mongodb</artifactId> | ||
<version>1.5.0-jdk21-SNAPSHOT</version> | ||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>run.mone</groupId> | ||
<artifactId>docean-plugin-config</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.mongodb</groupId> | ||
<artifactId>mongo-java-driver</artifactId> | ||
<version>3.8.0</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>run.mone</groupId> | ||
<artifactId>catPlugin</artifactId> | ||
<version>1.6.0-jdk21-SNAPSHOT</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>dev.morphia.morphia</groupId> | ||
<artifactId>morphia-core</artifactId> | ||
<version>2.4.13</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.mapstruct</groupId> | ||
<artifactId>mapstruct</artifactId> | ||
<version>1.4.2.Final</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.mapstruct</groupId> | ||
<artifactId>mapstruct-processor</artifactId> | ||
<version>1.4.2.Final</version> | ||
</dependency> | ||
|
||
|
||
</dependencies> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
/** | ||
* 用户 | ||
* | ||
* @author mone | ||
* @author [email protected] | ||
*/ | ||
@Data | ||
@Builder | ||
|
@@ -55,6 +55,8 @@ public class User implements MongoBo{ | |
//版本(用于乐观锁) | ||
private int version; | ||
|
||
private String token; | ||
|
||
public User(String username, String password) { | ||
this.username = username; | ||
this.password = password; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
...docean-plugin/docean-plugin-mongodb/src/main/java/run/mone/controller/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package run.mone.controller; | ||
|
||
import com.xiaomi.youpin.docean.anno.Controller; | ||
import com.xiaomi.youpin.docean.anno.ModelAttribute; | ||
import com.xiaomi.youpin.docean.anno.RequestMapping; | ||
import com.xiaomi.youpin.docean.anno.RequestParam; | ||
import com.xiaomi.youpin.docean.mvc.ContextHolder; | ||
import com.xiaomi.youpin.docean.mvc.MvcContext; | ||
import run.mone.bo.User; | ||
import run.mone.mapper.UserMapper; | ||
import run.mone.service.UserService; | ||
import run.mone.vo.UserVo; | ||
|
||
import javax.annotation.Resource; | ||
|
||
/** | ||
* 用户管理控制器 | ||
* | ||
* @author [email protected] | ||
*/ | ||
@Controller | ||
@RequestMapping(path = "/user") | ||
public class UserController extends MongodbController<User> { | ||
|
||
@Resource | ||
private UserService userService; | ||
|
||
public UserController() { | ||
super(User.class); | ||
} | ||
|
||
/** | ||
* 根据用户名和密码查找用户 | ||
* | ||
* @param username 用户名 | ||
* @param password 密码 | ||
* @return 用户对象 | ||
*/ | ||
@RequestMapping(path = "/findByUsernameAndPassword", method = "get") | ||
public UserVo findUserByUsernameAndPassword(@RequestParam("username") String username, @RequestParam("password") String password) { | ||
User user = userService.findUserByUsernameAndPassword(username, password); | ||
return UserMapper.INSTANCE.userToUserVo(user); | ||
} | ||
|
||
/** | ||
* 更新用户密码 | ||
* | ||
* @param userId 用户ID | ||
* @param newPassword 新密码 | ||
* @return 是否更新成功 | ||
*/ | ||
@RequestMapping(path = "/updatePassword") | ||
public boolean updatePassword(String userId, String newPassword) { | ||
return userService.updatePassword(userId, newPassword); | ||
} | ||
|
||
/** | ||
* 更新用户个人信息 | ||
* | ||
* @param user 新的个人信息 | ||
* @return 是否更新成功 | ||
*/ | ||
@RequestMapping(path = "/updateProfile") | ||
public boolean updateProfile(@ModelAttribute("user") User user, User updateUser) { | ||
updateUser.setId(user.getId()); | ||
return userService.updateProfile(updateUser); | ||
} | ||
|
||
|
||
/** | ||
* 处理用户注册请求的方法。 | ||
* 调用userService中的registerUser方法,并返回注册结果。 | ||
* | ||
* @param user 用户对象,包含用户注册信息。 | ||
* @return 返回注册操作的成功与否。 | ||
*/ | ||
@RequestMapping(path = "/register") | ||
public boolean registerUser(User user) { | ||
return userService.registerUser(user); | ||
} | ||
|
||
@RequestMapping(path = "/getByUserId") | ||
public UserVo getByUserId(@RequestParam("userId") String userId) { | ||
User user = userService.findById(userId); | ||
return UserMapper.INSTANCE.userToUserVo(user); | ||
} | ||
|
||
/** | ||
* 该方法用于获取当前登录用户的信息 | ||
* 通过@RequestMapping注解映射请求路径为"/getLoginUser" | ||
* 使用@ModelAttribute注解将请求参数绑定到User对象 | ||
* 返回绑定了请求参数的User对象 | ||
*/ | ||
@RequestMapping(path = "/getLoginUser") | ||
public User getLoginUser(@ModelAttribute("user") User user) { | ||
return user; | ||
} | ||
|
||
@RequestMapping(path = "/login") | ||
public String login(User userReq) { | ||
MvcContext context = ContextHolder.getContext().get(); | ||
User user = userService.findUserByUsernameAndPassword(userReq.getUsername(), userReq.getPassword()); | ||
if (null == user) { | ||
return "error"; | ||
} | ||
user.setPassword(""); | ||
context.session().setAttribute("user", user); | ||
return "ok"; | ||
} | ||
|
||
@RequestMapping(path = "/logout") | ||
public String logout(MvcContext context) { | ||
context.session().removeAttribute("user"); | ||
return "ok"; | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/run/mone/mapper/UserMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package run.mone.mapper; | ||
|
||
import org.mapstruct.Mapper; | ||
import org.mapstruct.factory.Mappers; | ||
import run.mone.bo.User; | ||
import run.mone.vo.UserVo; | ||
|
||
/** | ||
* @author [email protected] | ||
* @date 2024/4/23 16:59 | ||
*/ | ||
@Mapper | ||
public interface UserMapper { | ||
|
||
UserMapper INSTANCE = Mappers.getMapper(UserMapper.class); | ||
|
||
UserVo userToUserVo(User user); | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
jcommon/docean-plugin/docean-plugin-mongodb/src/main/java/run/mone/service/UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package run.mone.service; | ||
|
||
import com.xiaomi.youpin.docean.anno.Service; | ||
import dev.morphia.query.filters.Filter; | ||
import dev.morphia.query.filters.Filters; | ||
import run.mone.bo.User; | ||
|
||
/** | ||
* @author [email protected] | ||
* @date 2024/4/18 22:49 | ||
*/ | ||
@Service | ||
public class UserService extends MongoService<User> { | ||
|
||
|
||
public UserService() { | ||
super(User.class); | ||
} | ||
|
||
/** | ||
* 根据用户名和密码查找用户 | ||
*/ | ||
public User findUserByUsernameAndPassword(String username, String password) { | ||
Filter filter = Filters.and( | ||
Filters.eq("username", username), | ||
Filters.eq("password", password) | ||
); | ||
return findFirst(filter); | ||
} | ||
|
||
/** | ||
* 更新用户密码 | ||
*/ | ||
public boolean updatePassword(String userId, String newPassword) { | ||
User user = findById(userId); | ||
if (user != null) { | ||
user.setPassword(newPassword); | ||
return update(user); | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* 更新用户个人信息 | ||
*/ | ||
public boolean updateProfile(User user) { | ||
return update(user); | ||
} | ||
|
||
|
||
/** | ||
* 注册用户 | ||
* <p> | ||
* 该方法用于注册新用户。首先会根据用户名查找是否已存在相同的用户,如果存在则返回 false 表示注册失败。 | ||
* 如果不存在相同用户,则保存新用户并返回 true 表示注册成功。 | ||
* | ||
* @param user 待注册的用户对象 | ||
* @return 注册是否成功, 成功返回 true,失败返回 false | ||
*/ | ||
public boolean registerUser(User user) { | ||
User existingUser = findFirst(Filters.eq("username", user.getUsername())); | ||
if (existingUser != null) { | ||
return false; // 用户名已存在 | ||
} | ||
return save(user); | ||
} | ||
} |
Oops, something went wrong.