1.基础知识
2.环境搭建
3.Restful风格的开发规范
4. 开发流程
正式开发
5.部门管理
5.1 查询部门
6.文件上传
6.1 文件上传三要素
(1)本地存储–生成随机名称
@Slf4j
@RestController
public class UploadController {@PostMapping("/upload")public Result upload(String username, Integer age, MultipartFile file) throws IOException {log.info("文件上传:{},{},{}",username,age,file);//将文件保存到本地服务器//1.获取原始文件名String originalFilename=file.getOriginalFilename();//2.生成新文件名String tmp=originalFilename.substring(originalFilename.lastIndexOf(".")); //文件后缀String newFilename= UUID.randomUUID()+tmp;log.info("新的文件名为:{}",newFilename);//3.将文件保存在本地服务器磁盘的特定位置file.transferTo(new File("E:\\1\\"+newFilename));return Result.success();}}
(2)云服务器存储
package com.xylearn.utils;import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyuncs.exceptions.ClientException;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;/*** 阿里云 OSS 工具类*/
@Component
public class AliOSSUtils {private String endpoint = "http://oss-cn-shanghai.aliyuncs.com";//说明:用EnvironmentVariableCredentialsProvider的话,要先根据官方文档去设置环境变量,重启电脑才生效EnvironmentVariableCredentialsProvider credentialsProvider =CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// String accessKeyId = "填你的accessKeyId";
// String accessKeySecret = "填你的accessKeySecret";private String bucketName = "web-framework0011111";public AliOSSUtils() throws ClientException {}/*** 实现上传图片到OSS*/public String upload(MultipartFile file) throws IOException {// 获取上传的文件的输入流InputStream inputStream = file.getInputStream();// 避免文件覆盖String originalFilename = file.getOriginalFilename();String fileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));//上传文件到 OSSOSSClient ossClient = new OSSClient(endpoint, credentialsProvider);ossClient.putObject(bucketName, fileName, inputStream);//文件访问路径String url = endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + fileName;// 关闭ossClientossClient.shutdown();return url;// 把上传到oss的路径返回}}
总结
7.参数配置化
(1)配置文件
(2)Yaml配置文件
(3)@ConfigurationProperties注解
8.登录
8.1 会话
8.1.1 基于cookie的会话跟踪技术
8.1.2 JWT令牌
8.1.3 登录校验-过滤器Filter
package com.xylearn.filter;import com.alibaba.fastjson2.JSONObject;
import com.xylearn.pojo.Result;
import com.xylearn.utils.JwtUtils;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;import java.io.IOException;@Slf4j
@WebFilter(urlPatterns = "/*")
public class LoginCheckFilter implements Filter {/*** 完成登录校验** @param servletRequest* @param servletResponse* @param filterChain* @throws IOException* @throws ServletException*/@Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {//获取请求的urlHttpServletRequest request = (HttpServletRequest) servletRequest;HttpServletResponse response = (HttpServletResponse) servletResponse;String url = request.getRequestURL().toString();log.info("请求的url{}", url);//判断是否为登录请求,登录请求直接放行if (url.contains("login")) {log.info("登录操作,放行...");filterChain.doFilter(request, response);return;}//获取请求头中的令牌tokenString jwt = request.getHeader("token");//判断令牌是否存在,不存在返回错误if (!StringUtils.hasText(jwt)) {log.info("请求头token为空,返回未登录信息");Result error = Result.error("NOT LOGIN");String s = JSONObject.toJSONString(error);response.getWriter().write(s);return;}//令牌存在,解析,若解析失败返回错误try {JwtUtils.parseJWT(jwt);} catch (Exception e) {e.printStackTrace();log.info("jwt解析失败,返回未登录信息");Result error = Result.error("NOT LOGIN");String s = JSONObject.toJSONString(error);response.getWriter().write(s);return;}//放行filterChain.doFilter(request, response);}}
8.1.4 登录校验-拦截器Interceptor
9.异常处理