From 58e43d0f3904c7cd40de8fba20434aaecd619afa Mon Sep 17 00:00:00 2001 From: fanxb Date: Tue, 9 Jul 2019 16:43:00 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Feat:=20[=E5=90=8E=E5=8F=B0]:?= =?UTF-8?q?=E5=A4=9A=E6=A8=A1=E5=9D=97=E6=9E=B6=E6=9E=84=E5=AE=8C=E6=88=90?= =?UTF-8?q?=EF=BC=8C=E6=B3=A8=E5=86=8C=E7=99=BB=E5=BD=95=EF=BC=8C=E4=B9=A6?= =?UTF-8?q?=E7=AD=BE=E5=A4=87=E4=BB=BDhtml=E5=A4=87=E4=BB=BD=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E8=A7=A3=E6=9E=90=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bookMarkService/business/bookmark/pom.xml | 25 ++++ .../controller/BookmarkController.java | 50 ++++++++ .../business/bookmark/dao/BookmarkDao.java | 49 ++++++++ .../bookmark/service/BookmarkService.java | 113 ++++++++++++++++++ .../mapper/bookmark-bookmarkMapper.xml | 32 +++++ bookMarkService/business/pom.xml | 1 + .../user/controller/UserController.java | 2 - .../bookmark/business/user/dao/UserDao.java | 5 +- .../business/user/service/UserService.java | 13 +- .../main/resources/mapper/user-userMapper.xml | 2 +- bookMarkService/common/pom.xml | 8 +- .../com/fanxb/bookmark/common/dao/UrlDao.java | 26 ++++ .../bookmark/common/entity/Bookmark.java | 58 +++++++++ .../com/fanxb/bookmark/common/entity/Url.java | 18 +++ .../bookmark/common/entity/UserContext.java | 3 +- .../common/exception/NoLoginException.java | 6 +- .../bookmark/common/filter/LoginFilter.java | 89 +++++++++++++- .../resources/mapper/common-urlMapper.xml | 13 ++ bookMarkService/pom.xml | 8 ++ bookMarkService/web/pom.xml | 10 +- .../com/fanxb/bookmark/web/Application.java | 4 +- .../web/src/main/resources/application.yml | 14 ++- .../main/resources/db/migration/V1__init.sql | 0 .../resources/db/migration/V1__创建用户表.sql | 13 -- 24 files changed, 522 insertions(+), 40 deletions(-) create mode 100644 bookMarkService/business/bookmark/pom.xml create mode 100644 bookMarkService/business/bookmark/src/main/java/com/fanxb/bookmark/business/bookmark/controller/BookmarkController.java create mode 100644 bookMarkService/business/bookmark/src/main/java/com/fanxb/bookmark/business/bookmark/dao/BookmarkDao.java create mode 100644 bookMarkService/business/bookmark/src/main/java/com/fanxb/bookmark/business/bookmark/service/BookmarkService.java create mode 100644 bookMarkService/business/bookmark/src/main/resources/mapper/bookmark-bookmarkMapper.xml create mode 100644 bookMarkService/common/src/main/java/com/fanxb/bookmark/common/dao/UrlDao.java create mode 100644 bookMarkService/common/src/main/java/com/fanxb/bookmark/common/entity/Bookmark.java create mode 100644 bookMarkService/common/src/main/java/com/fanxb/bookmark/common/entity/Url.java create mode 100644 bookMarkService/common/src/main/resources/mapper/common-urlMapper.xml create mode 100644 bookMarkService/web/src/main/resources/db/migration/V1__init.sql delete mode 100644 bookMarkService/web/src/main/resources/db/migration/V1__创建用户表.sql diff --git a/bookMarkService/business/bookmark/pom.xml b/bookMarkService/business/bookmark/pom.xml new file mode 100644 index 0000000..3d3db8a --- /dev/null +++ b/bookMarkService/business/bookmark/pom.xml @@ -0,0 +1,25 @@ + + + + bookmark-business + com.fanxb + 1.0-SNAPSHOT + + 4.0.0 + + bookmark-business-bookmark + + + + + + org.jsoup + jsoup + 1.12.1 + + + + + \ No newline at end of file diff --git a/bookMarkService/business/bookmark/src/main/java/com/fanxb/bookmark/business/bookmark/controller/BookmarkController.java b/bookMarkService/business/bookmark/src/main/java/com/fanxb/bookmark/business/bookmark/controller/BookmarkController.java new file mode 100644 index 0000000..942b985 --- /dev/null +++ b/bookMarkService/business/bookmark/src/main/java/com/fanxb/bookmark/business/bookmark/controller/BookmarkController.java @@ -0,0 +1,50 @@ +package com.fanxb.bookmark.business.bookmark.controller; + +import com.fanxb.bookmark.business.bookmark.service.BookmarkService; +import com.fanxb.bookmark.common.entity.Result; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + +/** + * 类功能简述: + * 类功能详述: + * + * @author fanxb + * @date 2019/7/8 15:12 + */ +@RestController +@RequestMapping("/bookmark") +public class BookmarkController { + + @Autowired + private BookmarkService bookmarkService; + + /** + * Description: 获取某个用户的节点树 + * + * @return com.fanxb.bookmark.common.entity.Result + * @author fanxb + * @date 2019/7/9 14:20 + */ + @GetMapping("/currentUser") + public Result getUserBookmarkTree() { + return null; + } + + /** + * Description: 上传书签备份文件,并解析 + * + * @param file 文件 + * @param path 存放节点路径(根节点为空字符串) + * @return com.fanxb.bookmark.common.entity.Result + * @author fanxb + * @date 2019/7/8 15:17 + */ + @PutMapping("/uploadBookmarkFile") + public Result uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("path") String path) throws Exception { + bookmarkService.parseBookmarkFile(file.getInputStream(), path); + return Result.success(null); + } + +} diff --git a/bookMarkService/business/bookmark/src/main/java/com/fanxb/bookmark/business/bookmark/dao/BookmarkDao.java b/bookMarkService/business/bookmark/src/main/java/com/fanxb/bookmark/business/bookmark/dao/BookmarkDao.java new file mode 100644 index 0000000..cba97ba --- /dev/null +++ b/bookMarkService/business/bookmark/src/main/java/com/fanxb/bookmark/business/bookmark/dao/BookmarkDao.java @@ -0,0 +1,49 @@ +package com.fanxb.bookmark.business.bookmark.dao; + +import com.fanxb.bookmark.common.entity.Bookmark; +import org.apache.ibatis.annotations.Param; +import org.springframework.stereotype.Component; + +/** + * 类功能简述: + * 类功能详述: + * + * @author fanxb + * @date 2019/7/8 16:39 + */ +@Component +public interface BookmarkDao { + + /** + * Description: 插入一条书签记录 + * + * @param node node + * @return void + * @author fanxb + * @date 2019/7/8 16:49 + */ + void insertOne(Bookmark node); + + /** + * Description: 根据用户名和name获取节点id + * + * @param userId 用户名 + * @param name 姓名 + * @param path 当前节点路径 + * @return Integer + * @author fanxb + * @date 2019/7/8 17:18 + */ + Integer selectIdByUserIdAndNameAndPath(@Param("userId") int userId, @Param("name") String name, @Param("path") String path); + + /** + * Description: + * + * @param userId 用户id + * @param path 父节点路径 + * @return java.lang.Integer + * @author fanxb + * @date 2019/7/8 17:35 + */ + Integer selectMaxSort(@Param("userId") int userId, @Param("path") String path); +} diff --git a/bookMarkService/business/bookmark/src/main/java/com/fanxb/bookmark/business/bookmark/service/BookmarkService.java b/bookMarkService/business/bookmark/src/main/java/com/fanxb/bookmark/business/bookmark/service/BookmarkService.java new file mode 100644 index 0000000..0cb0f2d --- /dev/null +++ b/bookMarkService/business/bookmark/src/main/java/com/fanxb/bookmark/business/bookmark/service/BookmarkService.java @@ -0,0 +1,113 @@ +package com.fanxb.bookmark.business.bookmark.service; + +import com.fanxb.bookmark.business.bookmark.dao.BookmarkDao; +import com.fanxb.bookmark.common.entity.Bookmark; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.select.Elements; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.io.InputStream; + +/** + * 类功能简述: + * 类功能详述: + * + * @author fanxb + * @date 2019/7/8 15:00 + */ +@Service +public class BookmarkService { + /** + * chrome导出书签tag + */ + private static final String DT = "dt"; + private static final String A = "a"; + + @Autowired + private BookmarkDao bookmarkDao; + + @Transactional(rollbackFor = Exception.class) + public void parseBookmarkFile(InputStream stream, String path) throws Exception { + Document doc = Jsoup.parse(stream, "utf-8", ""); + Elements elements = doc.select("html>body>dl>dt"); + //获取当前层sort最大值 + Integer sortBase = bookmarkDao.selectMaxSort(1, path); + if (sortBase == null) { + sortBase = 0; + } + int count = 0; + for (int i = 0, length = elements.size(); i < length; i++) { + if (i == 0) { + Elements firstChildren = elements.get(0).child(1).children(); + count = firstChildren.size(); + for (int j = 0; j < count; j++) { + dealBookmark(firstChildren.get(j), path, sortBase + j); + } + } else { + dealBookmark(elements.get(i), path, sortBase + count + i - 1); + } + } + } + + /** + * Description: 处理html节点,解析出文件夹和书签 + * + * @param ele 待处理节点 + * @param path 节点路径,不包含自身 + * @param sort 当前层级中的排序序号 + * @author fanxb + * @date 2019/7/8 14:49 + */ + private void dealBookmark(Element ele, String path, int sort) { + if (!DT.equalsIgnoreCase(ele.tagName())) { + return; + } + Element first = ele.child(0); + if (A.equalsIgnoreCase(first.tagName())) { + //说明为链接 + Bookmark node = new Bookmark(1, path, first.ownText(), first.attr("href"), first.attr("icon") + , Long.valueOf(first.attr("add_date")) * 1000, sort); + //存入数据库 + insertOne(node); + } else { + //说明为文件夹 + Bookmark node = new Bookmark(1, path, first.ownText(), Long.valueOf(first.attr("add_date")) * 1000, sort); + Integer sortBase = 0; + if (insertOne(node)) { + sortBase = bookmarkDao.selectMaxSort(node.getUserId(), path); + if (sortBase == null) { + sortBase = 0; + } + } + String childPath = path.length() == 0 ? node.getBookmarkId().toString() : path + "." + node.getBookmarkId(); + Elements children = ele.child(1).children(); + for (int i = 0, size = children.size(); i < size; i++) { + dealBookmark(children.get(i), childPath, sortBase + i + 1); + } + } + } + + /** + * Description: + * + * @param node node + * @return boolean 如果已经存在返回true,否则false + * @author fanxb + * @date 2019/7/8 17:25 + */ + private boolean insertOne(Bookmark node) { + //先根据name,userId,parentId获取此节点id + Integer id = bookmarkDao.selectIdByUserIdAndNameAndPath(node.getUserId(), node.getName(), node.getPath()); + if (id == null) { + bookmarkDao.insertOne(node); + return false; + } else { + node.setBookmarkId(id); + return true; + } + } +} diff --git a/bookMarkService/business/bookmark/src/main/resources/mapper/bookmark-bookmarkMapper.xml b/bookMarkService/business/bookmark/src/main/resources/mapper/bookmark-bookmarkMapper.xml new file mode 100644 index 0000000..9650d45 --- /dev/null +++ b/bookMarkService/business/bookmark/src/main/resources/mapper/bookmark-bookmarkMapper.xml @@ -0,0 +1,32 @@ + + + + + + + insert into bookmark(userId,path,type,name,url,icon,sort,addTime,createTime) + value + ( #{userId},#{path},#{type},#{name}, + + "","", + + + #{url},#{icon}, + + #{sort},#{addTime},#{createTime}) + + + + + + + + \ No newline at end of file diff --git a/bookMarkService/business/pom.xml b/bookMarkService/business/pom.xml index b9e123a..ab0d339 100644 --- a/bookMarkService/business/pom.xml +++ b/bookMarkService/business/pom.xml @@ -13,6 +13,7 @@ pom user + bookmark diff --git a/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/controller/UserController.java b/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/controller/UserController.java index 468be45..e2ee4e6 100644 --- a/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/controller/UserController.java +++ b/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/controller/UserController.java @@ -4,7 +4,6 @@ import com.fanxb.bookmark.business.user.entity.LoginBody; import com.fanxb.bookmark.business.user.entity.RegisterBody; import com.fanxb.bookmark.business.user.service.UserService; import com.fanxb.bookmark.common.entity.Result; -import com.fanxb.bookmark.common.entity.User; import org.apache.ibatis.annotations.Param; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @@ -50,7 +49,6 @@ public class UserController { return Result.success(userService.login(body)); } - /** * Description: 获取验证码 * diff --git a/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/dao/UserDao.java b/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/dao/UserDao.java index c070ba9..4ef4178 100644 --- a/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/dao/UserDao.java +++ b/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/dao/UserDao.java @@ -26,12 +26,13 @@ public interface UserDao { /** * Description: 通过用户名或者email获取用户信息 * - * @param str username/email + * @param name username + * @param email email * @return com.fanxb.bookmark.common.entity.User * @author fanxb * @date 2019/7/6 16:45 */ - User selectByUsernameOrEmail(String str); + User selectByUsernameOrEmail(@Param("name") String name,@Param("email") String email); /** * Description: 更新用户上次登录时间 diff --git a/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/service/UserService.java b/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/service/UserService.java index cefe98c..e368edc 100644 --- a/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/service/UserService.java +++ b/bookMarkService/business/user/src/main/java/com/fanxb/bookmark/business/user/service/UserService.java @@ -75,7 +75,16 @@ public class UserService { throw new CustomException("验证码错误"); } RedisUtil.delete(codeKey); - User user = new User(); + User user = userDao.selectByUsernameOrEmail(body.getUsername(), body.getEmail()); + if (user != null) { + if (user.getUsername().equals(body.getUsername())) { + throw new FormDataException("用户名已经被注册"); + } + if (user.getEmail().equals(body.getEmail())) { + throw new FormDataException("邮箱已经被注册"); + } + } + user = new User(); user.setUsername(body.getUsername()); user.setEmail(body.getEmail()); user.setIcon(DEFAULT_ICON); @@ -94,7 +103,7 @@ public class UserService { * @date 2019/7/6 16:37 */ public LoginRes login(LoginBody body) { - User userInfo = userDao.selectByUsernameOrEmail(body.getStr()); + User userInfo = userDao.selectByUsernameOrEmail(body.getStr(), body.getStr()); if (userInfo == null) { throw new FormDataException("账号/密码错误"); } diff --git a/bookMarkService/business/user/src/main/resources/mapper/user-userMapper.xml b/bookMarkService/business/user/src/main/resources/mapper/user-userMapper.xml index 3dac61a..7ee6a89 100644 --- a/bookMarkService/business/user/src/main/resources/mapper/user-userMapper.xml +++ b/bookMarkService/business/user/src/main/resources/mapper/user-userMapper.xml @@ -18,7 +18,7 @@ password, createTime from user - where username = #{str} or email = #{str} + where username = #{name} or email = #{email} diff --git a/bookMarkService/common/pom.xml b/bookMarkService/common/pom.xml index 59c9152..592835d 100644 --- a/bookMarkService/common/pom.xml +++ b/bookMarkService/common/pom.xml @@ -13,10 +13,7 @@ - - org.springframework.boot - spring-boot-starter-web - + @@ -98,6 +95,9 @@ 1.2.56 + + + diff --git a/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/dao/UrlDao.java b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/dao/UrlDao.java new file mode 100644 index 0000000..7543089 --- /dev/null +++ b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/dao/UrlDao.java @@ -0,0 +1,26 @@ +package com.fanxb.bookmark.common.dao; + +import com.fanxb.bookmark.common.entity.Url; +import org.springframework.stereotype.Component; + +import java.util.List; + +/** + * 类功能简述: + * 类功能详述: + * + * @author fanxb + * @date 2019/7/9 14:47 + */ +@Component +public interface UrlDao { + + /** + * Description: 获取公共接口 + * + * @author fanxb + * @date 2019/7/9 14:52 + * @return java.util.List + */ + List getPublicUrl(); +} diff --git a/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/entity/Bookmark.java b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/entity/Bookmark.java new file mode 100644 index 0000000..91b0db6 --- /dev/null +++ b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/entity/Bookmark.java @@ -0,0 +1,58 @@ +package com.fanxb.bookmark.common.entity; + +import lombok.Data; + +import java.util.ArrayList; + +/** + * 类功能简述:书签树 + * 类功能详述: + * + * @author fanxb + * @date 2019/7/8 11:19 + */ +@Data +public class Bookmark { + public static final int BOOKMARK_TYPE = 0; + public static final int FOLDER_TYPE = 1; + + private Integer bookmarkId; + /** + * 类型:0:文件夹,1:具体的书签 + */ + private int type; + private int userId; + private String path; + private String name; + private String url; + private String icon; + private int sort; + private long addTime; + private long createTime; + private ArrayList children; + + public Bookmark() { + } + + public Bookmark(int userId, String path, String name, long addTime, int sort) { + this.setUserId(userId); + this.setPath(path); + this.setType(FOLDER_TYPE); + this.setName(name); + this.setAddTime(addTime); + this.setSort(sort); + this.setCreateTime(System.currentTimeMillis()); + } + + public Bookmark(int userId, String path, String name, String url, String icon, long addTime, int sort) { + this.setUserId(userId); + this.setPath(path); + this.setType(BOOKMARK_TYPE); + this.setName(name); + this.setUrl(url); + this.setIcon(icon); + this.setSort(sort); + this.setAddTime(addTime); + this.setCreateTime(System.currentTimeMillis()); + } +} diff --git a/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/entity/Url.java b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/entity/Url.java new file mode 100644 index 0000000..9704038 --- /dev/null +++ b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/entity/Url.java @@ -0,0 +1,18 @@ +package com.fanxb.bookmark.common.entity; + +import lombok.Data; + +/** + * 类功能简述: + * 类功能详述: + * + * @author fanxb + * @date 2019/7/9 14:47 + */ +@Data +public class Url { + private int userId; + private String method; + private String url; + private int type; +} diff --git a/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/entity/UserContext.java b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/entity/UserContext.java index d138855..d583e41 100644 --- a/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/entity/UserContext.java +++ b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/entity/UserContext.java @@ -12,7 +12,6 @@ import lombok.Data; @Data public class UserContext { - private User user; + private int userId; private String jwt; - private String sessionId; } diff --git a/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/exception/NoLoginException.java b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/exception/NoLoginException.java index ca7b256..5adb08c 100644 --- a/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/exception/NoLoginException.java +++ b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/exception/NoLoginException.java @@ -8,7 +8,9 @@ package com.fanxb.bookmark.common.exception; * @date 2019/7/5 15:49 */ public class NoLoginException extends CustomException { - NoLoginException() { - super("您尚未登录", -1, null); + public static final int CODE = -1; + public static final String MESSAGE= "您尚未登录"; + public NoLoginException() { + super(MESSAGE, CODE, null); } } diff --git a/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/filter/LoginFilter.java b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/filter/LoginFilter.java index d9043d0..de84730 100644 --- a/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/filter/LoginFilter.java +++ b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/filter/LoginFilter.java @@ -1,12 +1,31 @@ package com.fanxb.bookmark.common.filter; +import com.alibaba.fastjson.JSON; +import com.auth0.jwt.interfaces.Claim; +import com.fanxb.bookmark.common.Constant; +import com.fanxb.bookmark.common.dao.UrlDao; +import com.fanxb.bookmark.common.entity.Result; +import com.fanxb.bookmark.common.entity.Url; +import com.fanxb.bookmark.common.entity.UserContext; +import com.fanxb.bookmark.common.exception.NoLoginException; +import com.fanxb.bookmark.common.util.JwtUtil; +import com.fanxb.bookmark.common.util.StringUtil; +import com.fanxb.bookmark.common.util.UserContextHolder; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.core.annotation.Order; +import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; +import org.springframework.util.AntPathMatcher; import javax.servlet.*; import javax.servlet.annotation.WebFilter; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import java.io.IOException; +import java.util.List; +import java.util.Map; /** * 类功能简述: @@ -17,14 +36,26 @@ import java.io.IOException; */ @Component -@WebFilter(urlPatterns = "/*", filterName = "loginFilter") @Slf4j -@Order(100) +@WebFilter(urlPatterns = "/*", filterName = "loginFilter") +@Order(1000) public class LoginFilter implements Filter { + @Value("${server.servlet.context-path}") + private String urlPrefix; + + @Value("${jwtSecret}") + private String secret; + + @Autowired + private UrlDao urlDao; + + private static AntPathMatcher matcher = new AntPathMatcher(); + + volatile private static List publicUrl; + @Override public void init(FilterConfig filterConfig) throws ServletException { - } @Override @@ -34,6 +65,56 @@ public class LoginFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { - filterChain.doFilter(servletRequest, servletResponse); + UserContextHolder.remove(); + List publicUrl = this.getPublicUrl(); + HttpServletRequest request = (HttpServletRequest) servletRequest; + String requestUrl = request.getRequestURI().replace(urlPrefix, ""); + String requestMethod = request.getMethod(); + for (Url url : publicUrl) { + if (url.getMethod().equalsIgnoreCase(requestMethod) && matcher.match(url.getUrl(), requestUrl)) { + filterChain.doFilter(servletRequest, servletResponse); + return; + } + } + if (this.checkJwt(request.getHeader(Constant.JWT_KEY))) { + filterChain.doFilter(servletRequest, servletResponse); + } else { + HttpServletResponse response = (HttpServletResponse) servletResponse; + response.setStatus(HttpStatus.OK.value()); + response.setContentType("application/json"); + response.setCharacterEncoding("utf-8"); + response.getWriter().write(JSON.toJSONString(new Result(NoLoginException.CODE, NoLoginException.MESSAGE, null))); + } + + } + + private List getPublicUrl() { + if (publicUrl == null) { + synchronized (LoginFilter.class) { + if (publicUrl == null) { + publicUrl = this.urlDao.getPublicUrl(); + } + } + } + return publicUrl; + } + + private boolean checkJwt(String jwt) { + if (StringUtil.isEmpty(jwt)) { + log.error("jwt为空"); + return false; + } + try { + Map map = JwtUtil.decode(jwt, secret); + int userId = Integer.valueOf(map.get("userId").asString()); + UserContext context = new UserContext(); + context.setJwt(jwt); + context.setUserId(userId); + UserContextHolder.set(context); + return true; + } catch (Exception e) { + log.error("jwt解密失败:{}", jwt, e); + return false; + } } } diff --git a/bookMarkService/common/src/main/resources/mapper/common-urlMapper.xml b/bookMarkService/common/src/main/resources/mapper/common-urlMapper.xml new file mode 100644 index 0000000..5d3f254 --- /dev/null +++ b/bookMarkService/common/src/main/resources/mapper/common-urlMapper.xml @@ -0,0 +1,13 @@ + + + + + + + + + \ No newline at end of file diff --git a/bookMarkService/pom.xml b/bookMarkService/pom.xml index 4458df9..a730374 100644 --- a/bookMarkService/pom.xml +++ b/bookMarkService/pom.xml @@ -26,4 +26,12 @@ + + + org.springframework.boot + spring-boot-starter-web + + + + \ No newline at end of file diff --git a/bookMarkService/web/pom.xml b/bookMarkService/web/pom.xml index cca60a3..e9203bb 100644 --- a/bookMarkService/web/pom.xml +++ b/bookMarkService/web/pom.xml @@ -12,15 +12,17 @@ bookmark-web - - org.springframework.boot - spring-boot-starter-web - + com.fanxb bookmark-business-user 1.0-SNAPSHOT + + com.fanxb + bookmark-business-bookmark + 1.0-SNAPSHOT + diff --git a/bookMarkService/web/src/main/java/com/fanxb/bookmark/web/Application.java b/bookMarkService/web/src/main/java/com/fanxb/bookmark/web/Application.java index 43c90f5..f90094c 100644 --- a/bookMarkService/web/src/main/java/com/fanxb/bookmark/web/Application.java +++ b/bookMarkService/web/src/main/java/com/fanxb/bookmark/web/Application.java @@ -4,6 +4,7 @@ package com.fanxb.bookmark.web; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.transaction.annotation.EnableTransactionManagement; /** * 类功能简述: @@ -12,8 +13,9 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; * @author fanxb * @date 2019/7/4 19:38 */ -@SpringBootApplication(scanBasePackages = "com.fanxb.bookmark") +@SpringBootApplication(scanBasePackages = {"com.fanxb.bookmark"}) @MapperScan(basePackages = "com.fanxb.bookmark.**.dao") +@EnableTransactionManagement public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); diff --git a/bookMarkService/web/src/main/resources/application.yml b/bookMarkService/web/src/main/resources/application.yml index 6754271..355c863 100644 --- a/bookMarkService/web/src/main/resources/application.yml +++ b/bookMarkService/web/src/main/resources/application.yml @@ -3,8 +3,14 @@ jwtSecret: abcdefgh server: port: 8088 servlet: - context-path: /bookmark/api/ + # 不要在最后加/ + context-path: /bookmark/api spring: + servlet: + # 表单配置 + multipart: + max-request-size: 10MB + max-file-size: 10MB profiles: active: dev application: @@ -48,5 +54,7 @@ spring: mybatis: configuration: - log-impl: org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl - mapper-locations: classpath:mapper/*.xml + log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl + # classpath后面加*,值里面的*才起作用 + mapper-locations: classpath*:mapper/*.xml +debug: false diff --git a/bookMarkService/web/src/main/resources/db/migration/V1__init.sql b/bookMarkService/web/src/main/resources/db/migration/V1__init.sql new file mode 100644 index 0000000..e69de29 diff --git a/bookMarkService/web/src/main/resources/db/migration/V1__创建用户表.sql b/bookMarkService/web/src/main/resources/db/migration/V1__创建用户表.sql deleted file mode 100644 index 1efca52..0000000 --- a/bookMarkService/web/src/main/resources/db/migration/V1__创建用户表.sql +++ /dev/null @@ -1,13 +0,0 @@ -CREATE TABLE `user` ( - `userId` int UNSIGNED NOT NULL AUTO_INCREMENT, - `username` varchar(20) NOT NULL - COMMENT '用户名20位以内数字,字母组合', - `email` varchar(40) NOT NULL, - `icon` varchar(50) NOT NULL, - `password` char(40) NOT NULL - COMMENT '明文(6-20位数字密码组合)+userId 进行sha1签名', - `createTime` bigint NOT NULL DEFAULT 0, - `lastLoginTime` bigint NOT NULL DEFAULT 0, - PRIMARY KEY (`userId`) -) - ENGINE = InnoDB; \ No newline at end of file