基于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进行后台管理。

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

相关推荐
皮卡丘不断更几秒前
手搓本地 RAG:我用 Python 和 Spring Boot 给 AI 装上了“实时代码监控”
人工智能·spring boot·python·ai编程
想用offer打牌9 分钟前
MCP (Model Context Protocol) 技术理解 - 第一篇
后端·aigc·mcp
千寻girling13 分钟前
Koa.js 教程 | 一份不可多得的 Node.js 的 Web 框架 Koa.js 教程
前端·后端·面试
程序员清风22 分钟前
北京回长沙了,简单谈谈感受!
java·后端·面试
lucky670730 分钟前
Spring Boot集成Kafka:最佳实践与详细指南
spring boot·kafka·linq
何中应31 分钟前
请求头设置没有生效
java·后端
NPE~34 分钟前
自动化工具Drissonpage 保姆级教程(含xpath语法)
运维·后端·爬虫·自动化·网络爬虫·xpath·浏览器自动化
Coder_Boy_38 分钟前
基于Spring AI的分布式在线考试系统-事件处理架构实现方案
人工智能·spring boot·分布式·spring
亓才孓1 小时前
[JDBC]批处理
java
春日见1 小时前
车辆动力学:前后轮车轴
java·开发语言·驱动开发·docker·计算机外设