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

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

相关推荐
间彧10 分钟前
Spring Boot集成Spring Security 6.x完整指南
java
绝无仅有27 分钟前
面试实战总结:数据结构与算法面试常见问题解析
后端·面试·github
绝无仅有30 分钟前
Docker 面试常见问题及解答
后端·面试·github
程序员爱钓鱼33 分钟前
Go语言100个实战案例-项目实战篇:股票行情数据爬虫
后端·go·trae
IT_陈寒1 小时前
Redis 性能翻倍的 7 个冷门技巧,第 5 个大多数人都不知道!
前端·人工智能·后端
xiezhr1 小时前
用户只需要知道「怎么办」,不需要知道「为什么炸了」
java·api·接口设计规范
xiezhr1 小时前
接口设计18条军规:写给那些半夜被“502”叫醒的人
java·api·restful
你的人类朋友9 小时前
说说签名与验签
后端
databook9 小时前
Manim实现脉冲闪烁特效
后端·python·动效
RainbowSea10 小时前
12. LangChain4j + 向量数据库操作详细说明
java·langchain·ai编程