版权声明 本项目为学习交流项目,参考自MarkerHub的vueblog[https://github.com/MarkerHub/vueblog]
项目:vueblog
公众号:MarkerHub
介绍 一个基于SpringBoot + Vue开发的前后端分离博客项目,带有超级详细开发文档和讲解视频。还未接触过vue开发,或者前后端分离的同学,学起来哈。别忘了给vueblog一个star!感谢
开发文档
项目地址
项目效果图
后端开发环境
jdk8
IDEA 2020
MybatisPlus 3.2.0
MySQL 8.0.23
Springboot 2.6.2
Maven
Lombok
Shiro
Redis-x64-5.0.10
hutool
导入数据 新建一个数据库vueblog
然后用下列脚本导入数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 SET FOREIGN_KEY_CHECKS= 0 ;DROP TABLE IF EXISTS `m_blog`;CREATE TABLE `m_blog` ( `id` bigint (20 ) NOT NULL AUTO_INCREMENT, `user_id` bigint (20 ) NOT NULL , `title` varchar (255 ) NOT NULL , `description` varchar (255 ) NOT NULL , `content` longtext, `created` datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP , `status` tinyint(4 ) DEFAULT NULL , PRIMARY KEY (`id`) ) ENGINE= InnoDB AUTO_INCREMENT= 15 DEFAULT CHARSET= utf8mb4;INSERT INTO `m_blog` VALUES ('1' , '1' , '生活就像海洋,只有意志坚强的人才能到达彼岸' , '这里是摘要哈哈哈' , '内容???' , '2020-05-21 22:08:42' , '0' );INSERT INTO `m_blog` VALUES ('2' , '1' , '最值得学习的博客项目eblog' , 'eblog是一个基于Springboot2.1.2开发的博客学习项目,为了让项目融合更多的知识点,达到学习目的,编写了详细的从0到1开发文档。主要学习包括:自定义Freemarker标签,使用shiro+redis完成了会话共享,redis的zset结构完成本周热议排行榜,t-io+websocket完成即时消息通知和群聊,rabbitmq+elasticsearch完成博客内容搜索引擎等。值得学习的地方很多!' , '**推荐阅读:**\r\n\r\n[分享一套SpringBoot开发博客系统源码,以及完整开发文档!速度保存!](https://mp.weixin.qq.com/s/jz6e977xP-OyaAKNjNca8w)\r\n\r\n[Github上最值得学习的100个Java开源项目,涵盖各种技术栈!](https://mp.weixin.qq.com/s/N-U0TaEUXnBFfBsmt_OESQ)\r\n\r\n[2020年最新的常问企业面试题大全以及答案](https://mp.weixin.qq.com/s/lR5LC5GnD2Gs59ecV5R0XA)' , '2020-05-28 09:36:38' , '0' );INSERT INTO `m_blog` VALUES ('3' , '1' , '关注公众号JavaCat,回复xshell或navicat获取破解对应工具' , '视频中所用到的xshell和navicat直接获取哈!' , '### 工具获取\r\n\r\n* xshell 6 绿色破解版:关注公众号:JavaCat,回复 xshell 获取\r\n* Navicat 11 简体中文版:关注公众号:JavaCat,回复 navicat 获取\r\n\r\n公众号二维码:\r\n\r\n![JavaCat](//image-1300566513.cos.ap-guangzhou.myqcloud.com/upload/images/20201020/7fa16a1f957f4cfebe7be1f6675f6f36.png \"JavaCat\")\r\n\r\n直接扫码回复对应关键字\r\n\r\n**推荐阅读:**\r\n\r\n[B站86K播放量,SpringBoot+Vue前后端分离完整入门教程!](https://mp.weixin.qq.com/s/jGEkHTf2X8l-wUenc-PpEw)\r\n\r\n[分享一套SpringBoot开发博客系统源码,以及完整开发文档!速度保存!](https://mp.weixin.qq.com/s/jz6e977xP-OyaAKNjNca8w)\r\n\r\n[Github上最值得学习的100个Java开源项目,涵盖各种技术栈!](https://mp.weixin.qq.com/s/N-U0TaEUXnBFfBsmt_OESQ)\r\n\r\n[2020年最新的常问企业面试题大全以及答案](https://mp.weixin.qq.com/s/lR5LC5GnD2Gs59ecV5R0XA)' , '2020-10-20 05:05:31' , '0' );INSERT INTO `m_blog` VALUES ('7' , '1' , '你真的会写单例模式吗?' , '单例模式可能是代码最少的模式了,但是少不一定意味着简单,想要用好、用对单例模式,还真得费一番脑筋。本文对 Java 中常见的单例模式写法做了一个总结,如有错漏之处,恳请读者指正。' , '> 作者:吃桔子的攻城狮 来源:http://www.tekbroaden.com/singleton-java.html\n\n\n单例模式可能是代码最少的模式了,但是少不一定意味着简单,想要用好、用对单例模式,还真得费一番脑筋。本文对 Java 中常见的单例模式写法做了一个总结,如有错漏之处,恳请读者指正。\n\n饿汉法\n===\n\n顾名思义,饿汉法就是在第一次引用该类的时候就创建对象实例,而不管实际是否需要创建。代码如下:\n\n```\npublic class Singleton { \n private static Singleton = new Singleton();\n private Singleton() {}\n public static getSignleton(){\n return singleton;\n }\n}\n\n```\n\n这样做的好处是编写简单,但是无法做到延迟创建对象。但是我们很多时候都希望对象可以尽可能地延迟加载,从而减小负载,所以就需要下面的懒汉法:\n' , '2020-05-22 00:42:44' , '0' );INSERT INTO `m_blog` VALUES ('9' , '1' , '真正理解Mysql的四种隔离级别@' , '事务是应用程序中一系列严密的操作,所有操作必须成功完成,否则在每个操作中所作的所有更改都会被撤消。也就是事务具有原子性,一个事务中的一系列的操作要么全部成功,要么一个都不做。\n\n事务的结束有两种,当事务中的所以步骤全部成功执行时,事务提交。如果其中一个步骤失败,将发生回滚操作,撤消撤消之前到事务开始时的所以操作。' , '### 什么是事务 \n\n> 事务是应用程序中一系列严密的操作,所有操作必须成功完成,否则在每个操作中所作的所有更改都会被撤消。也就是事务具有原子性,一个事务中的一系列的操作要么全部成功,要么一个都不做。\n> \n> 事务的结束有两种,当事务中的所以步骤全部成功执行时,事务提交。如果其中一个步骤失败,将发生回滚操作,撤消撤消之前到事务开始时的所以操作。\n\n**事务的 ACID**\n\n事务具有四个特征:原子性( Atomicity )、一致性( Consistency )、隔离性( Isolation )和持续性( Durability )。这四个特性简称为 ACID 特性。\n\n> 1 、原子性。事务是数据库的逻辑工作单位,事务中包含的各操作要么都做,要么都不做\n> \n> 2 、一致性。事 务执行的结果必须是使数据库从一个一致性状态变到另一个一致性状态。因此当数据库只包含成功事务提交的结果时,就说数据库处于一致性状态。如果数据库系统 运行中发生故障,有些事务尚未完成就被迫中断,这些未完成事务对数据库所做的修改有一部分已写入物理数据库,这时数据库就处于一种不正确的状态,或者说是 不一致的状态。' , '2020-05-22 22:04:46' , '0' );INSERT INTO `m_blog` VALUES ('10' , '1' , '博客项目eblog讲解视频上线啦,长达17个小时!!' , '1. 慕课网免费资源好久都没更新了,新教程大都付费\n2. B站上的视频繁多,通过收藏和弹幕数量通常很容易判断出视频是否优质\n3. 讲真,B站的弹幕文化,让我觉得,我不是一个在学习,自古人才出评论。哈哈哈\n4. B站视频通常广告少,up主的用心录制,通常只为了你关注他' , 'ok,再回到我们的eblog项目,源码、文档、视频我都开源出来了。来些基本操作:github上给个star,B站视频给个三连支持咧。\n\neblog源码:https://github.com/MarkerHub/eblog\n\n点击这里:[10+篇完整开发文档](https://mp.weixin.qq.com/mp/homepage?__biz=MzIwODkzOTc1MQ==&hid=1&sn=8e512316c3dfe140e636d0c996951166)\n\n![](//image-1300566513.cos.ap-guangzhou.myqcloud.com/upload/images/20200508/c290d945b7d24c79b172759bdb5b94e0.png)\n\n视频讲解:(记得关注我噢!)\n\nhttps://www.bilibili.com/video/BV1ri4y1x71A\n\n![](//image-1300566513.cos.ap-guangzhou.myqcloud.com/upload/images/20200508/983b5abc1c934360a1a1362347a275f7.png)\n\n项目其实还很多bug的,哈哈,我还需要进行二次迭代,到时候再发迭代文档出来。\n\n关注下我的B站,作为一个自媒体的自由职业者,没有什么比涨粉更让我开心的了,嘻嘻。\n\n近期即将推出的视频教程:\n\n1. 搭建脚手架,前后端分离首秀\n2. Shiro入门到精通教程\n3. SpringBoot2.2.6最新入门教程' , '2020-05-22 22:05:49' , '0' );DROP TABLE IF EXISTS `m_user`;CREATE TABLE `m_user` ( `id` bigint (20 ) NOT NULL AUTO_INCREMENT, `username` varchar (64 ) DEFAULT NULL , `avatar` varchar (255 ) DEFAULT NULL , `email` varchar (64 ) DEFAULT NULL , `password` varchar (64 ) DEFAULT NULL , `status` int (5 ) NOT NULL , `created` datetime DEFAULT NULL , `last_login` datetime DEFAULT NULL , PRIMARY KEY (`id`), KEY `UK_USERNAME` (`username`) USING BTREE ) ENGINE= InnoDB AUTO_INCREMENT= 2 DEFAULT CHARSET= utf8;
后端测试运行记得打开redis服务
前端开发环境
vue
element-ui
axios
mavon-editor
markdown-it
github-markdown-css
致谢 非常感谢MarkerHub提供的学习机会与课程!
后端 配置 MybatisPlusConfig 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 package com.ahawkeye.config;import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;import org.mybatis.spring.annotation.MapperScan;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.transaction.annotation.EnableTransactionManagement;@Configuration @EnableTransactionManagement @MapperScan("com.ahawkeye.mapper") public class MybatisPlusConfig { @Bean public PaginationInterceptor paginationInterceptor () { PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); return paginationInterceptor; } }
application.yml 1 2 3 4 5 6 7 8 9 10 11 12 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/vueblog?useUnicode=true&useSSL=false&characterEncoding=UTF-8&serverTimezone=UTC&allowPublicKeyRetrieval=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true username: root password: "010208" mybatis-plus: mapper-locations: classpath*:/mapper/**Mapper.xml server: port: 8081
CodeGenerator 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 package com.ahawkeye;import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;import com.baomidou.mybatisplus.core.toolkit.StringPool;import com.baomidou.mybatisplus.core.toolkit.StringUtils;import com.baomidou.mybatisplus.generator.AutoGenerator;import com.baomidou.mybatisplus.generator.InjectionConfig;import com.baomidou.mybatisplus.generator.config.*;import com.baomidou.mybatisplus.generator.config.po.TableInfo;import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;import java.util.ArrayList;import java.util.List;import java.util.Scanner;public class CodeGenerator { public static String scanner (String tip) { Scanner scanner = new Scanner(System.in); StringBuilder help = new StringBuilder(); help.append("请输入" + tip + ":" ); System.out.println(help.toString()); if (scanner.hasNext()) { String ipt = scanner.next(); if (StringUtils.isNotEmpty(ipt)) { return ipt; } } throw new MybatisPlusException("请输入正确的" + tip + "!" ); } public static void main (String[] args) { AutoGenerator mpg = new AutoGenerator(); GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir" ); gc.setOutputDir(projectPath + "/src/main/java" ); gc.setAuthor("ahawkeye" ); gc.setOpen(false ); gc.setServiceName("%sService" ); mpg.setGlobalConfig(gc); DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://localhost:3306/vueblog?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=UTC" ); dsc.setDriverName("com.mysql.cj.jdbc.Driver" ); dsc.setUsername("root" ); dsc.setPassword("010208" ); mpg.setDataSource(dsc); PackageConfig pc = new PackageConfig(); pc.setModuleName(null ); pc.setParent("com.ahawkeye" ); mpg.setPackageInfo(pc); InjectionConfig cfg = new InjectionConfig() { @Override public void initMap () { } }; String templatePath = "/templates/mapper.xml.ftl" ; List<FileOutConfig> focList = new ArrayList<>(); focList.add(new FileOutConfig(templatePath) { @Override public String outputFile (TableInfo tableInfo) { return projectPath + "/src/main/resources/mapper/" + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML; } }); cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); TemplateConfig templateConfig = new TemplateConfig(); templateConfig.setXml(null ); mpg.setTemplate(templateConfig); StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setEntityLombokModel(true ); strategy.setRestControllerStyle(true ); strategy.setInclude(scanner("表名,多个英文逗号分割" ).split("," )); strategy.setControllerMappingHyphenStyle(true ); strategy.setTablePrefix("m_" ); mpg.setStrategy(strategy); mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); } }
运行这个main方法,输入两个表名m_user和m_blog,可以自动生成mapper,service,controller等。
统一结果封装 Result 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 package com.ahawkeye.common.lang;import lombok.Data;import java.io.Serializable;@Data public class Result implements Serializable { private int code; private String msg; private Object data; public static Result succ (Object data) { return succ(200 , "操作成功" , data); } public static Result succ (int code, String msg, Object data) { Result r = new Result(); r.setCode(code); r.setMsg(msg); r.setData(data); return r; } public static Result fail (String msg) { return fail(400 , msg, null ); } public static Result fail (String msg, Object data) { return fail(400 , msg, data); } public static Result fail (int code, String msg, Object data) { Result r = new Result(); r.setCode(code); r.setMsg(msg); r.setData(data); return r; } }
整合shiro+jwt并会话共享 难点在本部分
第一步:导入jar包 导入shiro-redis的starter包:还有jwt的工具包,以及为了简化开发,我引入了hutool工具包。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <dependency > <groupId > org.crazycake</groupId > <artifactId > shiro-redis-spring-boot-starter</artifactId > <version > 3.2.1</version > </dependency > <dependency > <groupId > cn.hutool</groupId > <artifactId > hutool-all</artifactId > <version > 5.3.3</version > </dependency > <dependency > <groupId > io.jsonwebtoken</groupId > <artifactId > jjwt</artifactId > <version > 0.9.1</version > </dependency >
第二步:编写配置 ShiroConfig 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 package com.ahawkeye.config;import com.ahawkeye.shiro.AccountRealm;import com.ahawkeye.shiro.JwtFilter;import org.apache.shiro.mgt.DefaultSessionStorageEvaluator;import org.apache.shiro.mgt.DefaultSubjectDAO;import org.apache.shiro.mgt.SecurityManager;import org.apache.shiro.session.mgt.SessionManager;import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;import org.apache.shiro.spring.web.ShiroFilterFactoryBean;import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;import org.apache.shiro.web.mgt.DefaultWebSecurityManager;import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;import org.crazycake.shiro.RedisCacheManager;import org.crazycake.shiro.RedisSessionDAO;import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import javax.servlet.Filter;import java.util.HashMap;import java.util.LinkedHashMap;import java.util.Map;@Configuration public class ShiroConfig { @Autowired JwtFilter jwtFilter; @Bean public SessionManager sessionManager (RedisSessionDAO redisSessionDAO) { DefaultWebSessionManager sessionManager = new DefaultWebSessionManager(); sessionManager.setSessionDAO(redisSessionDAO); return sessionManager; } @Bean public DefaultWebSecurityManager securityManager (AccountRealm accountRealm, SessionManager sessionManager, RedisCacheManager redisCacheManager) { DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(accountRealm); securityManager.setSessionManager(sessionManager); securityManager.setCacheManager(redisCacheManager); DefaultSubjectDAO subjectDAO = new DefaultSubjectDAO(); DefaultSessionStorageEvaluator defaultSessionStorageEvaluator = new DefaultSessionStorageEvaluator(); defaultSessionStorageEvaluator.setSessionStorageEnabled(false ); subjectDAO.setSessionStorageEvaluator(defaultSessionStorageEvaluator); securityManager.setSubjectDAO(subjectDAO); return securityManager; } @Bean public ShiroFilterChainDefinition shiroFilterChainDefinition () { DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition(); Map<String, String> filterMap = new LinkedHashMap<>(); filterMap.put("/**" , "jwt" ); chainDefinition.addPathDefinitions(filterMap); return chainDefinition; } @Bean("shiroFilterFactoryBean") public ShiroFilterFactoryBean shiroFilterFactoryBean (SecurityManager securityManager, ShiroFilterChainDefinition shiroFilterChainDefinition) { ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean(); shiroFilter.setSecurityManager(securityManager); Map<String, Filter> filters = new HashMap<>(); filters.put("jwt" , jwtFilter); shiroFilter.setFilters(filters); Map<String, String> filterMap = shiroFilterChainDefinition.getFilterChainMap(); shiroFilter.setFilterChainDefinitionMap(filterMap); return shiroFilter; } @Bean public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor (SecurityManager securityManager) { AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor(); authorizationAttributeSourceAdvisor.setSecurityManager(securityManager); return authorizationAttributeSourceAdvisor; } @Bean public static DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator () { DefaultAdvisorAutoProxyCreator creator = new DefaultAdvisorAutoProxyCreator(); return creator; } }
上面ShiroConfig,我们主要做了几件事情:
引入RedisSessionDAO和RedisCacheManager,为了解决shiro的权限数据和会话信息能保存到redis中,实现会话共享。
重写了SessionManager和DefaultWebSecurityManager,同时在DefaultWebSecurityManager中为了关闭shiro自带的session方式,我们需要设置为false,这样用户就不再能通过session方式登录shiro。后面将采用jwt凭证登录。
在ShiroFilterChainDefinition中,我们不再通过编码形式拦截Controller访问路径,而是所有的路由都需要经过JwtFilter这个过滤器,然后判断请求头中是否含有jwt的信息,有就登录,没有就跳过。跳过之后,有Controller中的shiro注解进行再次拦截,比如@RequiresAuthentication ,这样控制权限访问。
那么,接下来,我们聊聊ShiroConfig中出现的AccountRealm,还有JwtFilter。
AccountRealm AccountRealm是shiro进行登录或者权限校验的逻辑所在,算是核心了,我们需要重写3个方法,分别是
supports:为了让realm支持jwt的凭证校验
doGetAuthorizationInfo:权限校验
doGetAuthenticationInfo:登录认证校验
我们先来总体看看AccountRealm的代码,然后逐个分析:
com.ahawkeye.shiro.AccountRealm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 package com.ahawkeye.shiro;import cn.hutool.core.bean.BeanUtil;import com.ahawkeye.entity.User;import com.ahawkeye.service.UserService;import com.ahawkeye.util.JwtUtils;import lombok.extern.slf4j.Slf4j;import org.apache.shiro.authc.*;import org.apache.shiro.authz.AuthorizationInfo;import org.apache.shiro.realm.AuthorizingRealm;import org.apache.shiro.subject.PrincipalCollection;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;@Slf4j @Component public class AccountRealm extends AuthorizingRealm { @Autowired JwtUtils jwtUtils; @Autowired UserService userService; @Override public boolean supports (AuthenticationToken token) { return token instanceof JwtToken; } @Override protected AuthorizationInfo doGetAuthorizationInfo (PrincipalCollection principals) { return null ; } @Override protected AuthenticationInfo doGetAuthenticationInfo (AuthenticationToken token) throws AuthenticationException { JwtToken jwt = (JwtToken) token; log.info("jwt----------------->{}" , jwt); String userId = jwtUtils.getClaimByToken((String) jwt.getPrincipal()).getSubject(); User user = userService.getById(Long.parseLong(userId)); if (user == null ) { throw new UnknownAccountException("账户不存在!" ); } if (user.getStatus() == -1 ) { throw new LockedAccountException("账户已被锁定!" ); } AccountProfile profile = new AccountProfile(); BeanUtil.copyProperties(user, profile); log.info("profile----------------->{}" , profile.toString()); return new SimpleAuthenticationInfo(profile, jwt.getCredentials(), getName()); } }
其实主要就是doGetAuthenticationInfo登录认证这个方法,可以看到我们通过jwt获取到用户信息,判断用户的状态,最后异常就抛出对应的异常信息,否者封装成SimpleAuthenticationInfo返回给shiro。 接下来我们逐步分析里面出现的新类:
1、shiro默认supports的是UsernamePasswordToken,而我们现在采用了jwt的方式,所以这里我们自定义一个JwtToken,来完成shiro的supports方法。
JwtToken 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 package com.ahawkeye.shiro;import org.apache.shiro.authc.AuthenticationToken;public class JwtToken implements AuthenticationToken { private String token; public JwtToken (String token) { this .token = token; } @Override public Object getPrincipal () { return token; } @Override public Object getCredentials () { return token; } }
JwtUtils 2、JwtUtils是个生成和校验jwt的工具类,其中有些jwt相关的密钥信息是从项目配置文件中配置的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 package com.ahawkeye.util;import io.jsonwebtoken.Claims;import io.jsonwebtoken.Jwts;import io.jsonwebtoken.SignatureAlgorithm;import lombok.Data;import lombok.extern.slf4j.Slf4j;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;import java.util.Date;@Slf4j @Data @Component @ConfigurationProperties(prefix = "ahawkeye.jwt") public class JwtUtils { private String secret; private long expire; private String header; public String generateToken (long userId) { Date nowDate = new Date(); Date expireDate = new Date(nowDate.getTime() + expire * 1000 ); return Jwts.builder() .setHeaderParam("typ" , "JWT" ) .setSubject(userId+"" ) .setIssuedAt(nowDate) .setExpiration(expireDate) .signWith(SignatureAlgorithm.HS512, secret) .compact(); } public Claims getClaimByToken (String token) { try { return Jwts.parser() .setSigningKey(secret) .parseClaimsJws(token) .getBody(); }catch (Exception e){ log.debug("validate is token error " , e); return null ; } } public boolean isTokenExpired (Date expiration) { return expiration.before(new Date()); } }
AccountProfile 3、而在AccountRealm我们还用到了AccountProfile,这是为了登录成功之后返回的一个用户信息的载体,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 package com.ahawkeye.shiro;import lombok.Data;import java.io.Serializable;@Data public class AccountProfile implements Serializable { private static final long serialVersionUID = 10L ; private Long id; private String username; private String avatar; private String email; }
补充配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/vueblog?useUnicode=true&useSSL=false&characterEncoding=UTF-8&serverTimezone=UTC&allowPublicKeyRetrieval=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true username: root password: "010208" mybatis-plus: mapper-locations: classpath*:/mapper/**Mapper.xml server: port: 8081 shiro-redis: enabled: true redis-manager: host: 127.0 .0 .1 :6379 ahawkeye: jwt: secret: f4e2e52034348f86b67cde581c0f9eb5 expire: 604800 header: Authorization
另外,如果你项目有使用spring-boot-devtools,需要添加一个配置文件,在resources目录下新建文件夹META-INF,然后新建文件spring-devtools.properties,这样热重启时候才不会报错。
resources/META-INF/spring-devtools.properties
1 restart.include.shiro-redis =/shiro-[\\w-\\.]+jar
JwtFilter 定义jwt的过滤器JwtFilter。
这个过滤器是我们的重点,这里我们继承的是Shiro内置的AuthenticatingFilter,一个可以内置了可以自动登录方法的的过滤器,有些同学继承BasicHttpAuthenticationFilter也是可以的。
我们需要重写几个方法:
createToken:实现登录,我们需要生成我们自定义支持的JwtToken
onAccessDenied:拦截校验,当头部没有Authorization时候,我们直接通过,不需要自动登录;当带有的时候,首先我们校验jwt的有效性,没问题我们就直接执行executeLogin方法实现自动登录
onLoginFailure:登录异常时候进入的方法,我们直接把异常信息封装然后抛出
preHandle:拦截器的前置拦截,因为我们是前后端分析项目,项目中除了需要跨域全局配置之外,我们再拦截器中也需要提供跨域支持。这样,拦截器才不会在进入Controller之前就被限制了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 package com.ahawkeye.shiro;import cn.hutool.json.JSONUtil;import com.ahawkeye.common.lang.Result;import com.ahawkeye.util.JwtUtils;import com.baomidou.mybatisplus.core.toolkit.StringUtils;import io.jsonwebtoken.Claims;import org.apache.shiro.authc.AuthenticationException;import org.apache.shiro.authc.AuthenticationToken;import org.apache.shiro.authc.ExpiredCredentialsException;import org.apache.shiro.web.filter.authc.AuthenticatingFilter;import org.apache.shiro.web.util.WebUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import org.springframework.web.bind.annotation.RequestMethod;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;@Component public class JwtFilter extends AuthenticatingFilter { @Autowired JwtUtils jwtUtils; @Override protected AuthenticationToken createToken (ServletRequest servletRequest, ServletResponse servletResponse) throws Exception { HttpServletRequest request = (HttpServletRequest) servletRequest; String jwt = request.getHeader("Authorization" ); if (StringUtils.isEmpty(jwt)){ return null ; } return new JwtToken(jwt); } @Override protected boolean onAccessDenied (ServletRequest servletRequest, ServletResponse servletResponse) throws Exception { HttpServletRequest request = (HttpServletRequest) servletRequest; String token = request.getHeader("Authorization" ); if (StringUtils.isEmpty(token)) { return true ; } else { Claims claim = jwtUtils.getClaimByToken(token); if (claim == null || jwtUtils.isTokenExpired(claim.getExpiration())) { throw new ExpiredCredentialsException("token已失效,请重新登录!" ); } } return executeLogin(servletRequest, servletResponse); } @Override protected boolean onLoginFailure (AuthenticationToken token, AuthenticationException e, ServletRequest request, ServletResponse response) { HttpServletResponse httpResponse = (HttpServletResponse) response; try { Throwable throwable = e.getCause() == null ? e : e.getCause(); Result r = Result.fail(throwable.getMessage()); String json = JSONUtil.toJsonStr(r); httpResponse.getWriter().print(json); } catch (IOException ignored) { } return false ; } @Override protected boolean preHandle (ServletRequest request, ServletResponse response) throws Exception { HttpServletRequest httpServletRequest = WebUtils.toHttp(request); HttpServletResponse httpServletResponse = WebUtils.toHttp(response); httpServletResponse.setHeader("Access-control-Allow-Origin" , httpServletRequest.getHeader("Origin" )); httpServletResponse.setHeader("Access-Control-Allow-Methods" , "GET,POST,OPTIONS,PUT,DELETE" ); httpServletResponse.setHeader("Access-Control-Allow-Headers" , httpServletRequest.getHeader("Access-Control-Request-Headers" )); if (httpServletRequest.getMethod().equals(RequestMethod.OPTIONS.name())) { httpServletResponse.setStatus(org.springframework.http.HttpStatus.OK.value()); return false ; } return super .preHandle(request, response); } }
异常处理 有时候不可避免服务器报错的情况,如果不配置异常处理机制,就会默认返回tomcat或者nginx的5XX页面,对普通用户来说,不太友好,用户也不懂什么情况。这时候需要我们程序员设计返回一个友好简单的格式给前端。
处理办法如下:通过使用@ControllerAdvice 来进行统一异常处理
@ExceptionHandler (value = RuntimeException.class)来指定捕获的Exception各个类型异常 ,这个异常的处理,是全局的,所有类似的异常,都会跑到这个地方处理。
com.ahawkeye.common.exception.GlobalExceptionHandler
步骤二、定义全局异常处理,@ControllerAdvice 表示定义全局控制器异常处理,@ExceptionHandler 表示针对性异常处理,可对每种异常针对性处理。
GlobalExceptionHandler 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 package com.ahawkeye.common.exception;import com.ahawkeye.common.lang.Result;import lombok.extern.slf4j.Slf4j;import org.apache.shiro.ShiroException;import org.springframework.http.HttpStatus;import org.springframework.validation.BindingResult;import org.springframework.validation.ObjectError;import org.springframework.web.bind.MethodArgumentNotValidException;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.ResponseStatus;import org.springframework.web.bind.annotation.RestControllerAdvice;@Slf4j @RestControllerAdvice public class GlobalExceptionHandler { @ResponseStatus(HttpStatus.UNAUTHORIZED) @ExceptionHandler(value = ShiroException.class) public Result handler (ShiroException e) { log.error("运行时异常:----------------{}" , e); return Result.fail(401 , e.getMessage(), null ); } @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(value = MethodArgumentNotValidException.class) public Result handler (MethodArgumentNotValidException e) { log.error("实体校验异常:----------------{}" , e); BindingResult bindingResult = e.getBindingResult(); ObjectError objectError = bindingResult.getAllErrors().stream().findFirst().get(); return Result.fail(objectError.getDefaultMessage()); } @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(value = IllegalArgumentException.class) public Result handler (IllegalArgumentException e) { log.error("Assert异常:----------------{}" , e); return Result.fail(e.getMessage()); } @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(value = RuntimeException.class) public Result handler (RuntimeException e) { log.error("运行时异常:----------------{}" , e); return Result.fail(e.getMessage()); } }
上面我们捕捉了几个异常:
ShiroException:shiro抛出的异常,比如没有权限,用户登录异常
IllegalArgumentException:处理Assert的异常
MethodArgumentNotValidException:处理实体校验的异常
RuntimeException:捕捉其他异常
实体校验 当我们表单数据提交的时候,前端的校验我们可以使用一些类似于jQuery Validate等js插件实现,而后端我们可以使用Hibernate validatior来做校验。
我们使用springboot框架作为基础,那么就已经自动集成了Hibernate validatior。
那么用起来啥样子的呢?
第一步:首先在实体的属性上添加对应的校验规则
Blog 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 package com.ahawkeye.entity;import com.baomidou.mybatisplus.annotation.TableName;import com.baomidou.mybatisplus.annotation.IdType;import com.baomidou.mybatisplus.annotation.TableId;import java.time.LocalDateTime;import java.io.Serializable;import com.fasterxml.jackson.annotation.JsonFormat;import lombok.Data;import lombok.EqualsAndHashCode;import lombok.experimental.Accessors;import javax.validation.constraints.NotBlank;@Data @EqualsAndHashCode(callSuper = false) @Accessors(chain = true) @TableName("m_blog") public class Blog implements Serializable { private static final long serialVersionUID = 1L ; @TableId(value = "id", type = IdType.AUTO) private Long id; private Long userId; @NotBlank(message = "标题不能为空") private String title; @NotBlank(message = "摘要不能为空") private String description; @NotBlank(message = "内容不能为空") private String content; @JsonFormat(pattern="yyyy-MM-dd") private LocalDateTime created; private Integer status; }
User 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 package com.ahawkeye.entity;import com.baomidou.mybatisplus.annotation.IdType;import com.baomidou.mybatisplus.annotation.TableId;import com.baomidou.mybatisplus.annotation.TableName;import lombok.Data;import lombok.EqualsAndHashCode;import lombok.experimental.Accessors;import javax.validation.constraints.Email;import javax.validation.constraints.NotBlank;import java.io.Serializable;import java.time.LocalDateTime;@Data @EqualsAndHashCode(callSuper = false) @Accessors(chain = true) @TableName("m_user") public class User implements Serializable { private static final long serialVersionUID = 1L ; @TableId(value = "id", type = IdType.AUTO) private Long id; @NotBlank(message = "昵称不能为空") private String username; private String avatar; @NotBlank(message = "邮箱不能为空") @Email(message = "邮箱格式不正确") private String email; private String password; private Integer status; private LocalDateTime created; private LocalDateTime lastLogin; }
第二步 :这里我们使用@Validated 注解方式,如果实体不符合要求,系统会抛出异常,那么我们的异常处理中就捕获到MethodArgumentNotValidException。
1 2 3 4 5 6 7 8 9 @PostMapping("/save") public Object testUser (@Validated @RequestBody User user) { return user.toString(); }
解决跨域问题 CorsConfig 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 package com.ahawkeye.config;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.CorsRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configuration public class CorsConfig implements WebMvcConfigurer { @Override public void addCorsMappings (CorsRegistry registry) { registry.addMapping("/**" ) .allowedOriginPatterns("*" ) .allowedMethods("GET" , "HEAD" , "POST" , "PUT" , "DELETE" , "OPTIONS" ) .allowCredentials(true ) .maxAge(3600 ) .allowedHeaders("*" ); } }
注意我们在JwtFilter里也需要解决跨域问题(前面已经写了)
JwtFilter 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 @Override protected boolean preHandle (ServletRequest request, ServletResponse response) throws Exception { HttpServletRequest httpServletRequest = WebUtils.toHttp(request); HttpServletResponse httpServletResponse = WebUtils.toHttp(response); httpServletResponse.setHeader("Access-control-Allow-Origin" , httpServletRequest.getHeader("Origin" )); httpServletResponse.setHeader("Access-Control-Allow-Methods" , "GET,POST,OPTIONS,PUT,DELETE" ); httpServletResponse.setHeader("Access-Control-Allow-Headers" , httpServletRequest.getHeader("Access-Control-Request-Headers" )); if (httpServletRequest.getMethod().equals(RequestMethod.OPTIONS.name())) { httpServletResponse.setStatus(org.springframework.http.HttpStatus.OK.value()); return false ; } return super .preHandle(request, response); }
登录接口开发 LoginDto 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 package com.ahawkeye.common.dto;import lombok.Data;import javax.validation.constraints.NotBlank;import java.io.Serializable;@Data public class LoginDto implements Serializable { @NotBlank(message = "昵称不能为空") private String username; @NotBlank(message = "密码不能为空") private String password; }
AccountController 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 package com.ahawkeye.controller;import cn.hutool.core.map.MapUtil;import cn.hutool.crypto.SecureUtil;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;import com.ahawkeye.common.dto.LoginDto;import com.ahawkeye.common.lang.Result;import com.ahawkeye.entity.User;import com.ahawkeye.service.UserService;import com.ahawkeye.util.JwtUtils;import org.apache.shiro.SecurityUtils;import org.apache.shiro.authz.annotation.RequiresAuthentication;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.util.Assert;import org.springframework.validation.annotation.Validated;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletResponse;@RestController public class AccountController { @Autowired UserService userService; @Autowired JwtUtils jwtUtils; @PostMapping("/login") public Result login (@Validated @RequestBody LoginDto loginDto, HttpServletResponse response) { User user = userService.getOne(new QueryWrapper<User>().eq("username" , loginDto.getUsername())); Assert.notNull(user, "用户不存在" ); if (!user.getPassword().equals(SecureUtil.md5(loginDto.getPassword()))){ return Result.fail("密码不正确" ); } String jwt = jwtUtils.generateToken(user.getId()); response.setHeader("Authorization" , jwt); response.setHeader("Access-control-Expose-Headers" , "Authorization" ); return Result.succ(MapUtil.builder() .put("id" , user.getId()) .put("username" , user.getUsername()) .put("avatar" , user.getAvatar()) .put("email" , user.getEmail()) .map() ); } @RequiresAuthentication @GetMapping("/logout") public Result logout () { SecurityUtils.getSubject().logout(); return Result.succ(null ); } }
测试 (注意保持redis服务开启 )
1 2 3 4 { "username" : "admin" , "password" : "111111" }
博客接口开发 BlogController 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 package com.ahawkeye.controller;import cn.hutool.core.bean.BeanUtil;import cn.hutool.core.lang.Assert;import com.ahawkeye.common.lang.Result;import com.ahawkeye.entity.Blog;import com.ahawkeye.service.BlogService;import com.ahawkeye.util.ShiroUtil;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;import com.baomidou.mybatisplus.core.metadata.IPage;import com.baomidou.mybatisplus.extension.plugins.pagination.Page;import org.apache.shiro.authz.annotation.RequiresAuthentication;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.validation.annotation.Validated;import org.springframework.web.bind.annotation.*;import java.time.LocalDateTime;@RestController public class BlogController { @Autowired BlogService blogService; @GetMapping("/blogs") public Result list (@RequestParam(defaultValue = "1") Integer currentPage) { Page page = new Page(currentPage, 5 ); IPage pageData = blogService.page(page, new QueryWrapper<Blog>().orderByDesc("created" )); return Result.succ(pageData); } @GetMapping("/blog/{id}") public Result detail (@PathVariable(name = "id") Long id) { Blog blog = blogService.getById(id); Assert.notNull(blog, "该博客已被删除" ); return Result.succ(blog); } @RequiresAuthentication @PostMapping("/blog/edit") public Result edit (@Validated @RequestBody Blog blog) { Blog temp = null ; if (blog.getId() != null ) { temp = blogService.getById(blog.getId()); System.out.println(ShiroUtil.getProfile().getId()); Assert.isTrue(temp.getUserId().longValue() == ShiroUtil.getProfile().getId().longValue(), "没有权限编辑" ); } else { temp = new Blog(); temp.setUserId(ShiroUtil.getProfile().getId()); temp.setCreated(LocalDateTime.now()); temp.setStatus(0 ); } BeanUtil.copyProperties(blog, temp, "id" , "userId" , "created" , "status" ); blogService.saveOrUpdate(temp); return Result.succ(null ); } }
测试 登录[POST] 1 2 3 4 { "username" : "admin" , "password" : "111111" }
拿到jwt
请求头添加上jwt
添加blog[POST] 1 2 3 4 5 { "title" :"test12313123" , "description" :"te231231std" , "content" :"testc" }
修改blog[POST] 1 2 3 4 5 6 { "id" :18 , "title" :"modmodmod" , "description" :"te231231std" , "content" :"testc" }
查看blog列表[GET]
查看某一blog[GET]
前端 创建vue项目 vue3.0 使用vue ui
命令 进行可视化创建
下一步中,也选上【Use history mode for router】,点击创建项目,然后弹窗中选择按钮【创建项目,不保存预设】,就进入项目创建啦。
稍等片刻之后,项目就初始化完成了。上面的步骤中,我们创建了一个vue项目,并且安装了Router、Vuex。这样我们后面就可以直接使用。
我们来看下整个vueblog-vue的项目结构
安装element-ui和axios 在项目根目录使用命令
cnpm install element-ui --save
然后我们打开项目src目录下的main.js,引入element-ui依赖。
1 2 3 import Element from 'element-ui' import "element-ui/lib/theme-chalk/index.css" Vue.use(Element)
这样我们就可以愉快得在官网上选择组件复制代码到我们项目中直接使用啦。
接下来,我们来安装axios(http://www.axios-js.com/),axios是一个基于 promise 的 HTTP 库,这样我们进行前后端对接的时候,使用这个工具可以提高我们的开发效率。
使用命令
cnpm install axios --save
然后同样我们在main.js中全局引入axios。
1 2 import axios from 'axios' Vue.prototype.$axios = axios
组件中,我们就可以通过this.$axios.get()来发起我们的请求了哈。
页面路由 接下来,我们先定义好路由和页面,因为我们只是做一个简单的博客项目,页面比较少,所以我们可以直接先定义好,然后在慢慢开发,这样需要用到链接的地方我们就可以直接可以使用:
我们在views文件夹下定义几个页面:
BlogDetail.vue(博客详情页)
BlogEdit.vue(编辑博客)
Blogs.vue(博客列表)
Login.vue(登录页面)
然后再路由中心配置:
router\index.js 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 import Vue from 'vue' import VueRouter from 'vue-router' import Blogs from "../views/Blogs" ;import Login from "../views/Login" ;import BlogEdit from "../views/BlogEdit" ;import BlogDetail from "../views/BlogDetail" ; Vue.use(VueRouter)const routes = [ { path: '/' , name: 'Index' , redirect: {name : "Blogs" } }, { path: '/blogs' , name: 'Blogs' , component: Blogs }, { path: '/login' , name: 'Login' , component: Login }, { path: '/blog/add' , name: 'BlogEdit' , component: BlogEdit }, { path: '/blog/:blogId' , name: 'BlogDetail' , component: BlogDetail }, { path: '/blog/:blogId/edit' , name: 'BlogEdit' , component: BlogEdit } ]const router = new VueRouter({ mode: 'history' , base: process.env.BASE_URL, routes })export default router
接下来我们去开发我们的页面。其中,带有meta:requireAuth: true说明是需要登录字后才能访问的受限资源,后面我们路由权限拦截时候会用到。
登录页面 Login.vue 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 <template> <div> <el-container> <el-header> <img class="mlogo" src="../assets/logo.png" alt=""> </el-header> <el-main> <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm"> <el-form-item label="用户名" prop="username"> <el-input v-model="ruleForm.username"></el-input> </el-form-item> <el-form-item label="密码" prop="password"> <el-input type="password" v-model="ruleForm.password"></el-input> </el-form-item> <el-form-item> <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button> <el-button @click="resetForm('ruleForm')">重置</el-button> </el-form-item> </el-form> </el-main> </el-container> </div> </template> <script> export default { name: "Login.vue", data() { return { ruleForm: { username: '', password: '' }, rules: { username: [ { required: true, message: '请输入用户名', trigger: 'blur' }, { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' } ], password: [ { required: true, message: '请输入密码', trigger: 'blur' }, { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' } ] } }; }, methods: { submitForm(formName) { this.$refs[formName].validate((valid) => { if (valid) { alert('submit!'); } else { console.log('error submit!!'); return false; } }); }, resetForm(formName) { this.$refs[formName].resetFields(); } } } </script> <style scoped> .el-header, .el-footer { background-color: #B3C0D1; color: #333; text-align: center; line-height: 60px; } .el-aside { background-color: #D3DCE6; color: #333; text-align: center; line-height: 200px; } .el-main { /*background-color: #E9EEF3;*/ color: #333; text-align: center; line-height: 160px; } body > .el-container { margin-bottom: 40px; } .el-container:nth-child(5) .el-aside, .el-container:nth-child(6) .el-aside { line-height: 260px; } .el-container:nth-child(7) .el-aside { line-height: 320px; } .mlogo{ height: 60%; margin-top: 10px; } .demo-ruleForm{ max-width: 500px; margin: 0 auto; } </style>
发起POST请求 Login.vue 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 <template> <div> <el-container> <el-header> <img class="mlogo" src="../assets/logo.png" alt=""> </el-header> <el-main> <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm"> <el-form-item label="用户名" prop="username"> <el-input v-model="ruleForm.username"></el-input> </el-form-item> <el-form-item label="密码" prop="password"> <el-input type="password" v-model="ruleForm.password"></el-input> </el-form-item> <el-form-item> <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button> <el-button @click="resetForm('ruleForm')">重置</el-button> </el-form-item> </el-form> </el-main> </el-container> </div> </template> <script> export default { name: "Login.vue", data() { return { ruleForm: { username: 'admin', password: '111111' }, rules: { username: [ { required: true, message: '请输入用户名', trigger: 'blur' }, { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' } ], password: [ { required: true, message: '请输入密码', trigger: 'blur' }, { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' } ] } }; }, methods: { submitForm(formName) { this.$refs[formName].validate((valid) => { if (valid) { const _this = this this.$axios.post('http://localhost:8081/login',this.ruleForm).then(res => { const jwt = res.headers['authorization'] const userInfo = res.data.data _this.$store.commit("SET_TOKEN",jwt) _this.$store.commit("SET_USERINFO",userInfo) //跳转 _this.$router.push("/blogs") }) } else { console.log('error submit!!'); return false; } }); }, resetForm(formName) { this.$refs[formName].resetFields(); } } } </script> <style scoped> .el-header, .el-footer { background-color: #B3C0D1; color: #333; text-align: center; line-height: 60px; } .el-aside { background-color: #D3DCE6; color: #333; text-align: center; line-height: 200px; } .el-main { /*background-color: #E9EEF3;*/ color: #333; text-align: center; line-height: 160px; } body > .el-container { margin-bottom: 40px; } .el-container:nth-child(5) .el-aside, .el-container:nth-child(6) .el-aside { line-height: 260px; } .el-container:nth-child(7) .el-aside { line-height: 320px; } .mlogo{ height: 60%; margin-top: 10px; } .demo-ruleForm{ max-width: 500px; margin: 0 auto; } </style>
我们增加了设置token和userInfo以及页面路由跳转到/blogs
1 2 3 4 5 6 7 8 const _this = this this .$axios.post('http://localhost:8081/login' ,this .ruleForm).then(res => { const jwt = res.headers['authorization' ] const userInfo = res.data.data _this.$store.commit("SET_TOKEN" ,jwt) _this.$store.commit("SET_USERINFO" ,userInfo) _this.$router.push("/blogs" )
store\index.js 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex)export default new Vuex.Store({ state: { token: '' , userInfo: JSON .parse(sessionStorage.getItem("userInfo" )) }, mutations: { SET_TOKEN: (state, token ) => { state.token = token localStorage .setItem("token" , token) }, SET_USERINFO: (state, userInfo ) => { state.userInfo = userInfo sessionStorage.setItem("userInfo" , JSON .stringify(userInfo)) }, REMOVE_INFO: (state ) => { state.token = '' state.userInfo = {} localStorage .setItem("token" , '' ) sessionStorage.setItem("userInfo" , JSON .stringify('' )) } }, getters: { getUser: state => { return state.userInfo } }, actions: { }, modules: { } })
登录后浏览器可以存下token和userInfo
配置axios全局拦截 axios.js 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 import axios from 'axios' import Element from 'element-ui' import router from './router' import store from './store' axios.defaults.baseURL = "http://localhost:8081" axios.interceptors.request.use(config => { return config }) axios.interceptors.response.use(response => { let res = response.data; if (res.code === 200 ) { return response } else { Element.Message.error('密码错误' , {duration : 3 * 1000 }) return Promise .reject(response.data.msg) } }, error => { console .log(error) if (error.response.data) { error.message = error.response.data.msg } if (error.response.status === 401 ) { store.commit("REMOVE_INFO" ) router.push("/login" ) } Element.Message.error(error.message, {duration : 3 * 1000 }) return Promise .reject(error) } )
然后在main.js引入这个文件
main.js
博客列表 登录完成之后直接进入博客列表页面,然后加载博客列表的数据渲染出来。同时页面头部我们需要把用户的信息展示出来,因为很多地方都用到这个模块,所以我们把页面头部的用户信息单独抽取出来作为一个组件。
头部用户信息 那么,我们先来完成头部的用户信息,应该包含三部分信息:id,头像、用户名,而这些信息我们是在登录之后就已经存在了sessionStorage。因此,我们可以通过store的getters获取到用户信息。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 <template> <div class="m-content"> <h3>欢迎来到我的博客</h3> <div class="block"> <el-avatar :size="50" :src="user.avatar"></el-avatar> <div>{{ user.username }}</div> </div> <div class="maction"> <span><el-link href="/blogs">主页</el-link></span> <el-divider direction="vertical"></el-divider> <span><el-link type="success" href="/blog/add">发表博客</el-link></span> <el-divider direction="vertical"></el-divider> <span v-show="!hasLogin"><el-link type="primary" href="/login">登录</el-link></span> <span v-show="hasLogin"><el-link type="danger" @click="logout">退出</el-link></span> </div> </div> </template> <script> export default { name: "Header", data() { return { user: { username: '请先登录', avatar: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png' }, hasLogin: false } }, methods: { logout() { const _this = this _this.$axios.get("/logout", { headers: { "Authorization": localStorage.getItem("token") } }).then(res => { _this.$store.commit("REMOVE_INFO") _this.$router.push("/login") }) } }, created() { if(this.$store.getters.getUser.username) { this.user.username = this.$store.getters.getUser.username this.user.avatar = this.$store.getters.getUser.avatar this.hasLogin = true } } } </script> <style scoped> .m-content { max-width: 960px; margin: 0 auto; text-align: center; } .maction { margin: 10px 0; } </style>
上面代码created()中初始化用户的信息,通过hasLogin的状态来控制登录和退出按钮的切换,以及发表文章链接的disabled,这样用户的信息就能展示出来了。 然后这里有个退出按钮,在methods中有个logout()方法,逻辑比较简单,直接访问/logout,因为之前axios.js中我们已经设置axios请求的baseURL,所以这里我们不再需要链接的前缀了哈。因为是登录之后才能访问的受限资源,所以在header中带上了Authorization。返回结果清楚store中的用户信息和token信息,跳转到登录页面。
然后需要头部用户信息的页面只需要几个步骤:
1 2 3 4 5 6 import Header from "@/components/Header" ;data ( ) { components: {Header} } # 然后模板中调用组件 <Header></Header>
博客分页 接下来就是列表页面,需要做分页,列表我们在element-ui中直接使用时间线 组件来作为我们的列表样式,还是挺好看的。还有我们的分页组件。
需要几部分信息:
分页信息
博客列表内容,包括id、标题、摘要、创建时间
views\Blogs.vue
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 <template> <div class="mcontaner"> <Header></Header> <div class="block"> <el-timeline> <el-timeline-item :timestamp="blog.created" placement="top" v-for="blog in blogs"> <el-card> <h4> <router-link :to="{name: 'BlogDetail', params: {blogId: blog.id}}"> {{blog.title}} </router-link> </h4> <p>{{blog.description}}</p> </el-card> </el-timeline-item> </el-timeline> <el-pagination class="mpage" background layout="prev, pager, next" :current-page="currentPage" :page-size="pageSize" :total="total" @current-change=page> </el-pagination> </div> </div> </template> <script> import Header from "../components/Header"; export default { name: "Blogs.vue", components: {Header}, data() { return { blogs: {}, currentPage: 1, total: 0, pageSize: 5 } }, methods: { page(currentPage) { const _this = this _this.$axios.get("/blogs?currentPage=" + currentPage).then(res => { console.log(res) _this.blogs = res.data.data.records _this.currentPage = res.data.data.current _this.total = res.data.data.total _this.pageSize = res.data.data.size }) } }, created() { this.page(1) } } </script> <style scoped> .mpage { margin: 0 auto; text-align: center; } </style>
data()中直接定义博客列表blogs、以及一些分页信息。methods()中定义分页的调用接口page(currentPage),参数是需要调整的页码currentPage,得到结果之后直接赋值即可。然后初始化时候,直接在mounted()方法中调用第一页this.page(1). 注意标题这里我们添加了链接,使用的是标签。
博客编辑(发表) 我们点击发表博客链接调整到/blog/add页面,这里我们需要用到一个markdown编辑器,在vue组件中,比较好用的是mavon-editor,那么我们直接使用哈。先来安装mavon-editor相关组件:
安装mavon-editor
基于Vue的markdown编辑器mavon-editor
1 cnpm install mavon-editor --save
然后在main.js中全局注册:
1 2 3 4 5 6 import Vue from 'vue' import mavonEditor from 'mavon-editor' import 'mavon-editor/dist/css/index.css' Vue.use(mavonEditor)
ok,那么我们去定义我们的博客表单:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 <template> <div class="m-container"> <Header></Header> <div class="m-content"> <el-form ref="editForm" status-icon :model="editForm" :rules="rules" label-width="80px"> <el-form-item label="标题" prop="title"> <el-input v-model="editForm.title"></el-input> </el-form-item> <el-form-item label="摘要" prop="description"> <el-input type="textarea" v-model="editForm.description"></el-input> </el-form-item> <el-form-item label="内容" prop="content"> <mavon-editor v-model="editForm.content"/> </el-form-item> <el-form-item> <el-button type="primary" @click="submitForm()">立即创建</el-button> <el-button>取消</el-button> </el-form-item> </el-form> </div> </div> </template> import Header from "@/components/Header"; export default { name: "BlogEdit", components: {Header}, data() { return { editForm: { id: null, title: '', description: '', content: '' }, rules: { title: [ {required: true, message: '请输入标题', trigger: 'blur'}, {min: 3, max: 50, message: '长度在 3 到 50 个字符', trigger: 'blur'} ], description: [ {required: true, message: '请输入摘要', trigger: 'blur'} ] } } }, created() { const blogId = this.$route.params.blogId const _this = this if(blogId) { this.$axios.get('/blog/' + blogId).then((res) => { const blog = res.data.data _this.editForm.id = blog.id _this.editForm.title = blog.title _this.editForm.description = blog.description _this.editForm.content = blog.content }); } }, methods: { submitForm() { const _this = this this.$refs.editForm.validate((valid) => { if (valid) { this.$axios.post('/blog/edit', this.editForm, { headers: { "Authorization": localStorage.getItem("token") } }).then((res) => { _this.$alert('操作成功', '提示', { confirmButtonText: '确定', callback: action => { _this.$router.push("/blogs") } }); }); } else { console.log('error submit!!'); return false; } }) } } }
逻辑依然简单,校验表单,然后点击按钮提交表单,注意头部加上Authorization信息,返回结果弹窗提示操作成功,然后跳转到博客列表页面。emm,和写ajax没啥区别。熟悉一下vue的一些指令使用即可。 然后因为编辑和添加是同一个页面,所以有了create()方法,比如从编辑连接/blog/7/edit中获取blogId为7的这个id。然后回显博客信息。获取方式是const blogId = this.$route.params.blogId。
对了,mavon-editor因为已经全局注册,所以我们直接使用组件即可:
1 <mavon-editor v-model="editForm.content"/>
博客详情 博客详情中需要回显博客信息,然后有个问题就是,后端传过来的是博客内容是markdown格式的内容,我们需要进行渲染然后显示出来,这里我们使用一个插件markdown-it,用于解析md文档,然后导入github-markdown-c,所谓md的样式。
方法如下:
1 2 3 4 cnpm install markdown-it --save cnpm install github-markdown-css
然后就可以在需要渲染的地方使用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 <template> <div class="m-container"> <Header></Header> <div class="mblog"> <h2>{{ blog.title }}</h2> <el-link icon="el-icon-edit" v-if="ownBlog"><router-link :to="{name: 'BlogEdit', params: {blogId: blog.id}}">编辑</router-link></el-link> <el-divider></el-divider> <div class="content markdown-body" v-html="blog.content"></div> </div> </div> </template> import 'github-markdown-css/github-markdown.css' // 然后添加样式markdown-body import Header from "@/components/Header"; export default { name: "BlogDetail", components: { Header }, data() { return { blog: { userId: null, title: "", description: "", content: "" }, ownBlog: false } }, methods: { getBlog() { const blogId = this.$route.params.blogId const _this = this this.$axios.get('/blog/' + blogId).then((res) => { console.log(res) console.log(res.data.data) _this.blog = res.data.data var MarkdownIt = require('markdown-it'), md = new MarkdownIt(); var result = md.render(_this.blog.content); _this.blog.content = result // 判断是否是自己的文章,能否编辑 _this.ownBlog = (_this.blog.userId === _this.$store.getters.getUser.id) }); } }, created() { this.getBlog() } }
具体逻辑还是挺简单,初始化create()方法中调用getBlog()方法,请求博客详情接口,返回的博客详情content通过markdown-it工具进行渲染。
再导入样式:
1 import 'github-markdown.css'
然后在content的div中添加class为markdown-body即可。
另外标题下添加了个小小的编辑按钮,通过ownBlog (判断博文作者与登录用户是否同一人)来判断按钮是否显示出来。
路由权限拦截 页面已经开发完毕之后,我们来控制一下哪些页面是需要登录之后才能跳转的,如果未登录访问就直接重定向到登录页面,因此我们在src目录下定义一个js文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 import router from "./router" ; router.beforeEach((to, from , next ) => { if (to.matched.some(record => record.meta.requireAuth)) { const token = localStorage .getItem("token" ) console .log("------------" + token) if (token) { if (to.path === '/login' ) { } else { next() } } else { next({ path: '/login' }) } } else { next() } })
通过之前我们再定义页面路由时候的的meta信息,指定requireAuth: true,需要登录才能访问,因此这里我们在每次路由之前(router.beforeEach)判断token的状态,觉得是否需要跳转到登录页面。
1 2 3 4 5 6 7 8 { path: '/blog/add' , name: 'BlogAdd' , meta: { requireAuth: true }, component: BlogEdit }
然后我们再main.js中import我们的permission.js
1 import './permission.js'