软件开发流程
角色分工
软件环境
开发环境搭建
创建工程
application.yml:
server: port: 80 spring: application: name: reggie_take datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/reggie username: root password: 123 mybatis-plus: configuration: #是否 map-underscore-to-camel-case: true log-impl: org.apache.ibatis.logging.stdout.StdOutImpl global-config: db-config: id-type: ASSIGN_ID
pom.xml:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.0.0</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.itheima</groupId> <artifactId>reggie_take</artifactId> <version>0.0.1-SNAPSHOT</version> <name>reggie_take</name> <description>reggie_take</description> <properties> <java.version>18</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <scope>compile</scope> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.2</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.20</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.76</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.8</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
我们的静态资源没有放在static包里,所以要做相应的映射
后台登录功能开发(springboot 不要用3.0.0)
controller:
@Slf4j @RestController @RequestMapping("/employee") public class EmployeeController { @Resource private EmployeeService employeeService; /** * 员工登录 * @param * @param employee * @return */ @PostMapping("/login") public R login(HttpServletRequest request, @RequestBody Employee employee){ //将页面提交的密码加密 String password =employee.getPassword(); password= DigestUtils.md5DigestAsHex(password.getBytes()); //2.根据页面提交的用户名查询数据库 LambdaQueryWrapper<Employee> queryWrapper =new LambdaQueryWrapper<>(); queryWrapper.eq(Employee::getUsername,employee.getUsername()); Employee emp =employeeService.getOne(queryWrapper); //如果没查询到就返回登录失败 if(emp == null) { return new R(0,"登录失败",null); } //查到了就进行密码比对 if (!emp.getPassword().equals(password)){ return new R(0,"登录失败"); } //比对成功 查看员工状态是否为禁用 if(emp.getStatus()==0){ return new R(0,"账号已禁用"); } //登录成功,将员工id存入Session 并返回登录成功结果 request.getSession().setAttribute("employee",emp.getId()); return new R(1,"登录成功",emp); } @PostMapping("/logout") public R logout(HttpServletRequest request){ request.getSession().removeAttribute("employee"); return new R(1,"退出成功"); } }
问题:当前我们虽然完成了功能,但是用户不登录,还是能访问系统首页,是不合理的
所以用户没有登录,必须要跳转到登录页面
答案就是使用过滤器或者拦截器,在过滤器或者拦截器中判断用户是否已经完成登录,如果没有登录则跳转到登录页面。
@WebFilter(filterName = "loginCheckFilter",urlPatterns = "/*") @Slf4j public class LoginCheckFilter implements Filter { public static final AntPathMatcher PATH_MATCHER =new AntPathMatcher(); @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request =(HttpServletRequest) servletRequest; HttpServletResponse response =(HttpServletResponse) servletResponse; log.info("拦截到请求:{}",request.getRequestURI()); //1.获取本次请求的URI String requestURI =request.getRequestURI(); //定义不需要处理的请求路径 String[] urls= new String[]{ "/employee/login", "/employee/logout", "/backend/**", "/front/**" }; //判断本次请求是否需要处理 boolean check =check(urls,requestURI); //如果不需要处理则直接放行 if(check){ log.info("本次请求{}不需要处理",requestURI); filterChain.doFilter(request,response); return; } //判断登录状态,如果已登录,则直接放行 if(request.getSession().getAttribute("employee")!=null){ log.info("用户已登录,id为:{}",request.getSession().getAttribute("employee")); filterChain.doFilter(request,response); return; } log.info("未登录"); //如果未登录则返回未登录结果,通过输出流的方式像客户端页面响应数据 response.getWriter().write(JSON.toJSONString((new R(0,"NOTLOGIN")))); return; } /** * 路径匹配,检查请求是否需要放行 * @param urls * @return */ public boolean check(String[] urls,String requestURI){ for (String url : urls) { boolean match = PATH_MATCHER.match(url, requestURI); if (match){ return true; } } return false; } }