springboot修改密码代码逻辑
时间: 2024-04-22 09:21:12 浏览: 145
以下是两种SpringBoot修改密码的代码逻辑:
1.持久层/业务层修改密码[^1]
```java
@Test
public void changePassword() {
userService.changePassword(9, "hery", "111", "1111");
}
```
2.Mybatis修改密码逻辑[^2]
```java
@RestController
public class UpdatePasswordController {
@Autowired
private UpdatePasswordService updatePasswordService;
@RequestMapping("update")
public void updatePassword(@RequestParam(value = "username") String username,
@RequestParam(value = "password") String password,
@RequestParam(value = "newPsw") String newPsw){
updatePasswordService.updatePassword(username,password,newPsw);
}
}
```
相关问题
vue加springboot登录注册代码
以下是一个简单的 Vue.js 和 Spring Boot 的登录注册代码示例:
Vue.js 代码:
```
<template>
<div>
<h1>登录</h1>
<form>
<div>
<label for="username">用户名:</label>
<input type="text" id="username" v-model="username">
</div>
<div>
<label for="password">密码:</label>
<input type="password" id="password" v-model="password">
</div>
<button type="button" @click="login">登录</button>
</form>
<hr>
<h1>注册</h1>
<form>
<div>
<label for="newUsername">用户名:</label>
<input type="text" id="newUsername" v-model="newUsername">
</div>
<div>
<label for="newPassword">密码:</label>
<input type="password" id="newPassword" v-model="newPassword">
</div>
<button type="button" @click="register">注册</button>
</form>
</div>
</template>
<script>
export default {
data() {
return {
username: '',
password: '',
newUsername: '',
newPassword: ''
}
},
methods: {
login() {
// 发送登录请求
axios.post('/api/login', {
username: this.username,
password: this.password
}).then(response => {
// 处理登录成功的逻辑
}).catch(error => {
// 处理登录失败的逻辑
})
},
register() {
// 发送注册请求
axios.post('/api/register', {
username: this.newUsername,
password: this.newPassword
}).then(response => {
// 处理注册成功的逻辑
}).catch(error => {
// 处理注册失败的逻辑
})
}
}
}
</script>
```
Spring Boot 代码:
```
@RestController
@RequestMapping("/api")
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/login")
public ResponseEntity<?> login(@RequestBody User user) {
User loginUser = userService.login(user.getUsername(), user.getPassword());
if (loginUser != null) {
return ResponseEntity.ok().build();
} else {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
}
}
@PostMapping("/register")
public ResponseEntity<?> register(@RequestBody User user) {
boolean success = userService.register(user.getUsername(), user.getPassword());
if (success) {
return ResponseEntity.ok().build();
} else {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}
}
}
```
注意:以上代码仅供参考,实际使用时需要根据具体情况进行修改。
用java代码示例springboot中修改密码的接口
好的,以下是一个简单的示例代码:
```java
@RestController
@RequestMapping("/api/user")
public class UserController {
@Autowired
private UserService userService;
@PutMapping("/{userId}/password")
public ResponseEntity<String> updatePassword(@PathVariable Long userId, @RequestBody PasswordUpdateRequest request) {
userService.updatePassword(userId, request.getOldPassword(), request.getNewPassword());
return ResponseEntity.ok("Password updated successfully.");
}
}
public class PasswordUpdateRequest {
private String oldPassword;
private String newPassword;
// getters and setters
}
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public void updatePassword(Long userId, String oldPassword, String newPassword) {
User user = userRepository.findById(userId).orElseThrow(() -> new UserNotFoundException(userId));
if (!user.getPassword().equals(oldPassword)) {
throw new InvalidPasswordException("Old password is incorrect.");
}
user.setPassword(newPassword);
userRepository.save(user);
}
}
public class User {
private Long id;
private String username;
private String password;
// getters and setters
}
public interface UserRepository extends JpaRepository<User, Long> {}
public class UserNotFoundException extends RuntimeException {
public UserNotFoundException(Long userId) {
super("User not found with id: " + userId);
}
}
public class InvalidPasswordException extends RuntimeException {
public InvalidPasswordException(String message) {
super(message);
}
}
```
这个示例中,我们定义了一个 `UserController`,其中有一个 `updatePassword` 方法,用于修改用户的密码。在 `UserService` 中,我们实现了修改密码的逻辑,并在出现异常的情况下抛出自定义的异常。最后,我们使用 `UserRepository` 来与数据库进行交互,完成密码的修改。
阅读全文