融资平台用java的代码
时间: 2024-02-23 18:58:49 浏览: 36
以下是一个简单的融资平台Java代码示例,包含了用户认证、资金结算、项目管理和贷后管理等模块:
1. 用户认证模块
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
public class UserController {
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private UserService userService;
@PostMapping("/login")
public String login(@RequestParam String email, @RequestParam String password) {
UserDetails userDetails = userService.loadUserByUsername(email);
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(userDetails, password, userDetails.getAuthorities());
authenticationManager.authenticate(token);
if (token.isAuthenticated()) {
SecurityContextHolder.getContext().setAuthentication(token);
return "redirect:/home";
} else {
return "login";
}
}
@GetMapping("/logout")
public String logout() {
SecurityContextHolder.getContext().setAuthentication(null);
return "redirect:/login";
}
@GetMapping("/register")
public String register(Model model) {
model.addAttribute("user", new User());
return "register";
}
@PostMapping("/register")
public String register(@ModelAttribute User user) {
userService.save(user);
return "redirect:/login";
}
}
```
2. 资金结算模块
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
public class PaymentController {
@Autowired
private PaymentService paymentService;
@GetMapping("/payments")
public String payments(Model model) {
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
model.addAttribute("payments", paymentService.findByUser(user));
return "payments";
}
@PostMapping("/charge")
public String charge(@RequestParam BigDecimal amount) {
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
paymentService.charge(user, amount);
return "redirect:/payments";
}
}
```
3. 项目管理模块
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
public class ProjectController {
@Autowired
private ProjectService projectService;
@GetMapping("/projects")
public String projects(Model model) {
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
model.addAttribute("projects", projectService.findByBorrower(user));
return "projects";
}
@GetMapping("/projects/new")
public String newProject(Model model) {
model.addAttribute("project", new Project());
return "new_project";
}
@PostMapping("/projects")
public String createProject(@ModelAttribute Project project) {
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
project.setBorrower(user);
projectService.save(project);
return "redirect:/projects";
}
}
```
4. 贷后管理模块
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
public class LoanController {
@Autowired
private LoanService loanService;
@GetMapping("/loans")
public String loans(Model model) {
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
model.addAttribute("loans", loanService.findByBorrower(user));
return "loans";
}
@GetMapping("/loans/new")
public String newLoan(Model model) {
model.addAttribute("loan", new Loan());
return "new_loan";
}
@PostMapping("/loans")
public String createLoan(@ModelAttribute Loan loan) {
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
loan.setBorrower(user);
loanService.save(loan);
return "redirect:/loans";
}
}
```
以上代码只是示例,实际的融资平台Java代码可能还包括其他模块和更复杂的功能。