基于springboot 的后台管理系统,包括用户管理,角色管理,登录退出等功能

以下是一个基于Spring Boot的简单后台管理网站的示例代码,包括用户管理、角色管理、登录退出等功能。

  1. 创建Spring Boot项目

首先,创建一个新的Spring Boot项目。可以使用Spring Initializer(https://start.spring.io/)来生成项目的初始结构。确保选择适当的依赖项,如Spring Web和Spring Security。

  1. 配置数据库

在application.properties文件中配置数据库连接信息,例如:

spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver

  1. 创建实体类和数据库表

创建User和Role实体类,用于表示用户和角色。同时,在数据库中创建相应的表。

User.java:

@Entity @Table(name = "users") public class User implements Serializable {

复制代码
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false, unique = true)
private String username;

@Column(nullable = false)
private String password;

// getters and setters

}

Role.java:

@Entity @Table(name = "roles") public class Role implements Serializable {

复制代码
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false, unique = true)
private String name;

// getters and setters

}

  1. 创建数据访问层

创建UserRepository和RoleRepository接口,用于访问数据库中的用户和角色数据。

UserRepository.java:

@Repository public interface UserRepository extends JpaRepository<User, Long> {

复制代码
User findByUsername(String username);

}

RoleRepository.java:

@Repository public interface RoleRepository extends JpaRepository<Role, Long> {

复制代码
Role findByName(String name);

}

  1. 创建服务层

创建UserService和RoleService接口以及其实现类,用于处理用户和角色的业务逻辑。

UserService.java:

public interface UserService {

复制代码
User findByUsername(String username);

}

UserServiceImpl.java:

@Service public class UserServiceImpl implements UserService {

复制代码
@Autowired
private UserRepository userRepository;

@Override
public User findByUsername(String username) {
    return userRepository.findByUsername(username);
}

}

RoleService.java:

public interface RoleService {

复制代码
Role findByName(String name);

}

RoleServiceImpl.java:

@Service public class RoleServiceImpl implements RoleService {

复制代码
@Autowired
private RoleRepository roleRepository;

@Override
public Role findByName(String name) {
    return roleRepository.findByName(name);
}

}

  1. 创建控制器

创建UserController和RoleController类,用于处理用户和角色的HTTP请求。

UserController.java:

@RestController @RequestMapping("/users") public class UserController {

复制代码
@Autowired
private UserService userService;

@GetMapping("/{username}")
public User getUserByUsername(@PathVariable String username) {
    return userService.findByUsername(username);
}

}

RoleController.java:

@RestController @RequestMapping("/roles") public class RoleController {

复制代码
@Autowired
private RoleService roleService;

@GetMapping("/{name}")
public Role getRoleByName(@PathVariable String name) {
    return roleService.findByName(name);
}

}

  1. 创建安全配置

创建一个SecurityConfig类,用于配置Spring Security,包括登录和退出功能。

SecurityConfig.java:

@Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter {

复制代码
@Autowired
private UserService userService;

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
            .antMatchers("/login", "/logout").permitAll()
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .loginPage("/login")
            .defaultSuccessUrl("/home")
            .permitAll()
            .and()
        .logout()
            .logoutUrl("/logout")
            .logoutSuccessUrl("/login")
            .permitAll();
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(userService);
}

}

  1. 创建页面

创建login.html和home.html等页面,用于用户登录和后台管理功能。这些页面可以使用Thymeleaf或其他前端技术来渲染。

login.html:

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Login</title></head>
<body><h1>Login</h1>
<form action="/login" method="post"><label for="username">Username:</label> <input type="text" id="username"
                                                                                   name="username" required><br> <label
        for="password">Password:</label> <input type="password" id="password" name="password" required><br> <input
        type="submit" value="Login"></form>
</body>
</html>

home.html:

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Home</title></head>
<body><h1>Welcome, [[${username}]]!</h1> <a href="/logout">Logout</a></body>
</html>
  1. 启动应用程序

运行应用程序,访问http://localhost:8080/login进行登录,然后访问http://localhost:8080/home进行后台管理。

这只是一个简单的示例,可以根据具体需求进行扩展和定制。例如,可以添加更多的功能、权限控制和页面样式等。

相关推荐
小锋java12341 分钟前
【技术专题】Matplotlib3 Python 数据可视化 - Matplotlib3 绘制饼状图(Pie)
java
wuminyu2 分钟前
专家视角看JVM_StartThread
java·linux·c语言·jvm·c++
吕永强7 分钟前
基于SpringBoot+Vue小区报修系统的设计与实现(源码+论文+部署)
spring boot·毕业设计·毕业论文·报修系统·小区报修
awljwlj9 分钟前
黑马点评复习—缓存相关【包含可能的问题和基础知识复习】
java·后端·spring·缓存
Gofarlic_OMS10 分钟前
ENOVIA基于Token的许可证消费模式分析与分点策略
java·大数据·开发语言·人工智能·制造
XY_墨莲伊14 分钟前
【实战项目】基于B/S结构Flask+Folium技术的出租车轨迹可视化分析系统(文末含完整源代码)
开发语言·后端·python·算法·机器学习·flask
ROLL.716 分钟前
Git和Repo
java·git·安卓
神奇小汤圆17 分钟前
为什么Claude Code这么强?我从泄漏的源码里挖到了核心秘密
后端
小雅痞24 分钟前
[Java][Leetcode simple] 1. 两数之和
java·算法·leetcode
bingd0124 分钟前
慕课网、CSDN、菜鸟教程…2026 国内编程学习平台实测对比
java·开发语言·人工智能·python·学习