21xrx.com
2024-09-19 09:20:32 Thursday
登录
文章检索 我的文章 写文章
JavaWeb 项目实战:基于 Spring Boot 和 MySQL 的会议管理系统
2023-06-14 12:11:24 深夜i     --     --
JavaWeb Spring

JavaWeb 项目实战:基于 Spring Boot 和 MySQL 的会议管理系统

最近,我在学习 JavaWeb 开发的过程中,实现了一个基于 Spring Boot 和 MySQL 数据库的会议管理系统。通过这个项目,我不仅熟悉了 Spring Boot 框架的使用和 MySQL 数据库的操作,还锻炼了实际开发项目的能力。

首先,我创建了项目并配置了 Spring Boot 相关依赖。接着,我使用 Thymeleaf 模板引擎实现了系统的前端页面,并使用 Bootstrap 和 jQuery 完成了界面样式和交互效果。对于后端,我使用了 Spring Data JPA 框架实现了数据的持久化,并配置了 Spring Security 实现了登录和权限控制。同时,我在开发过程中封装了一些常用的工具类,如时间转换、异常处理等。

下面是一些关键的代码示例:

1. 配置 MySQL 数据库

yaml

spring:

 datasource:

  url: jdbc:mysql://localhost:3306/meeting-manager?useUnicode=true&characterEncoding=utf-8

  driverClassName: com.mysql.jdbc.Driver

  username: root

  password: 123456

  # 自动创建数据表

  initialization-mode: always

  schema:

   - classpath:db/schema.sql

  # 插入测试数据

  data:

   - classpath:db/data.sql

 jpa:

  hibernate:

   ddl-auto: update

  show-sql: true

2. 使用 Spring Security 实现登录和权限控制


@Configuration

@EnableWebSecurity

public class SecurityConfig extends WebSecurityConfigurerAdapter {

  @Autowired

  private UserDetailsService userDetailsService;

  @Autowired

  private PasswordEncoder passwordEncoder;

  // 配置用户信息服务

  @Override

  protected void configure(AuthenticationManagerBuilder auth) throws Exception {

    auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder);

  }

  // 配置安全拦截机制

  @Override

  protected void configure(HttpSecurity http) throws Exception {

    http.authorizeRequests()

        .antMatchers("/login", "/css/**", "/js/**", "/images/**").permitAll() // 允许访问登录页面、静态资源

        .antMatchers("/admin/**").hasRole("ADMIN") // 需要 ADMIN 角色才能访问

        .anyRequest().authenticated() // 其他请求都需要认证

        .and()

        .formLogin()

        .loginPage("/login") // 登录页面路径

        .defaultSuccessUrl("/admin/index") // 登录成功后跳转的路径

        .failureUrl("/login?error") // 登录失败后跳转的路径

        .permitAll()

        .and()

        .logout()

        .logoutUrl("/logout") // 注销路径

        .logoutSuccessUrl("/login") // 注销成功后跳转的路径

        .invalidateHttpSession(true)

        .permitAll()

        .and()

        .rememberMe()

        .key("meeting-manager")

        .tokenValiditySeconds(60 * 60 * 24 * 7); // 保持登录状态的时间为 7 天

  }

}

3. 使用 Thymeleaf 模板引擎实现前端页面


  

  

  

  

  

  

  

  

  

  

  

总的来说,这个项目让我受益匪浅,让我更加深入地理解了 JavaWeb 开发的各个方面。希望这个项目也可以对你有所启发。

Boot、MySQL

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复