三、设置Axios发起请求统一前缀的路径
https://code100.blog.csdn.net/article/details/123302546
1、HelloWorld.vue
getInfo() {
this.$http.get(‘blog/queryBlogByPage?title=’ + this.title + ‘&page=’ + this.page + ‘&rows=’ + this.rows)
.then(response => (
this.info = response.data,
this.total = this.info.total,
this.totalPage = this.info.totalPage,
this.items = this.info.items
)).catch(function (error) { // 请求失败处理
console.log(error);
});
},
2、Article.vue
getInfo() {
this.$http.get(‘/blog/queryBlogArticleById?id=’ + this.id )
.then(response => (
this.info = response.data,
this.title = this.info.title
)).catch(function (error) { // 请求失败处理
console.log(error);
});
},
selectBlog() {
this.page = 1;
this.rows = 10;
let startTime = (new Date(((this.value1+“”).split(“,”)[0]))).getTime();
let endTime = (new Date(((this.value1+“”).split(“,”)[1]))).getTime();
this.startBlogTime = startTime;
this.endBlogTime = endTime;
this.getInfo();
},
like(){
this.$http.get(‘blog/blogLikeId?id=’ + this.id );
this.getInfo();
},
四、实现登录功能
1、创建ConsumerService
package cn.itbluebox.springbootcsdn.service.Impl;
import cn.itbluebox.springbootcsdn.domain.Consumer;
import cn.itbluebox.springbootcsdn.enums.ExceptionEnum;
import cn.itbluebox.springbootcsdn.exception.BlException;
import cn.itbluebox.springbootcsdn.mapper.ConsumerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional(propagation = Propagation.REQUIRED)
public class ConsumerService {
@Autowired
private ConsumerMapper consumerMapper;
public Boolean checkData(String data, Integer type) {
Consumer consumer = new Consumer();
//判断数据类型
switch (type) {
case 1:
consumer.setEmail(data);
break;
case 2:
consumer.setPhone(Long.parseLong(data));
break;
default:
return null;
}
return consumerMapper.selectCount(consumer) == 0;
}
public Consumer queryUser(String email, String password) {
// 查询
Consumer consumer = new Consumer();
consumer.setEmail(email);
consumer.setPassword(password);
Consumer consumer1 = this.consumerMapper.selectOne(consumer);
// 校验用户名
if (consumer1 == null) {
return null;
}
// 用户名密码都正确
return consumer1;
}
public String saveConsumer(Consumer consumer) {
int insert = consumerMapper.insert(consumer);
if (insert != 1) {
throw new BlException(ExceptionEnum.CONSUMER_SAVE_ERROR);
}
return insert + “”;
}
public Consumer queryConsumerById(Long id) {
Consumer consumer = new Consumer();
consumer.setId(id);
Consumer consumer1 = consumerMapper.selectOne(consumer);
consumer1.setPassword(“”);
return consumer1;
}
}
2、创建AuthService
package cn.itbluebox.springbootcsdn.properties;
import cn.itbluebox.springbootcsdn.utils.RsaUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import javax.annotation.PostConstruct;
import java.io.File;
import java.security.PrivateKey;
import java.security.PublicKey;
@ConfigurationProperties(prefix = “sc.jwt”)
@Data
@Slf4j
public class JwtProperties {
private String secret; // 密钥
private String pubKeyPath;// 公钥
private String priKeyPath;// 私钥
private int expire;// token过期时间
private PublicKey publicKey; // 公钥
private PrivateKey privateKey; // 私钥
private String cookieName;
private Integer cookieMaxAge;
// private static final Logger logger = LoggerFactory.getLogger(JwtProperties.class);
/**
- @PostContruct:在构造方法执行之后执行该方法
*/
@PostConstruct
public void init(){
try {
File pubKey = new File(pubKeyPath);
File priKey = new File(priKeyPath);
if (!pubKey.exists() || !priKey.exists()) {
// 生成公钥和私钥
RsaUtils.generateKey(pubKeyPath, priKeyPath, secret);
}
// 获取公钥和私钥
this.publicKey = RsaUtils.getPublicKey(pubKeyPath);
this.privateKey = RsaUtils.getPrivateKey(priKeyPath);
} catch (Exception e) {
log.error(“初始化公钥和私钥失败!”, e);
throw new RuntimeException();
}
}
}
3、创建AuthController
package cn.itbluebox.springbootcsdn.web;
import cn.itbluebox.springbootcsdn.enums.ExceptionEnum;
import cn.itbluebox.springbootcsdn.exception.BlException;
import cn.itbluebox.springbootcsdn.properties.JwtProperties;
import cn.itbluebox.springbootcsdn.service.Impl.AuthService;
import cn.itbluebox.springbootcsdn.utils.CookieUtils;
import cn.itbluebox.springbootcsdn.utils.JwtUtils;
import cn.itbluebox.springbootcsdn.utils.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@RestController
@EnableConfigurationProperties(JwtProperties.class)
@Slf4j
public class AuthController {
@Autowired
private AuthService authService;
@Autowired
private JwtProperties prop;
/**
-
登录授权
-
@return
*/
@GetMapping(“accredit”)
public ResponseEntity authentication(
@RequestParam(“email”) String email,
@RequestParam(“password”) String password,
HttpServletRequest request,
HttpServletResponse response) {
// 登录校验
System.out.println(" 登录校验 登录校验 登录校验 登录校验");
String token = authService.authentication(email, password);
if (StringUtils.isBlank(token)) {
log.info(“用户授权失败”);
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}
Cookie cookie = CookieUtils.setGetCookie(request, response, prop.getCookieName(), token, prop.getCookieMaxAge(), true);
return ResponseEntity.ok(cookie);
}
@GetMapping(“verify”)
public ResponseEntity verify(
@CookieValue(“SC_TOKEN”) String token,
HttpServletRequest request,
HttpServletResponse response
) {
//解析token
System.out.println(“解析token解析token解析token解析token解析token”);
try {
UserInfo userInfo = JwtUtils.getUserInfo(prop.getPublicKey(), token);
//刷新token,重新生成token
String newToken = JwtUtils.generateToken(userInfo, prop.getPrivateKey(), prop.getExpire());
//写回cookie
CookieUtils.setCookie(request, response, prop.getCookieName(), newToken, prop.getCookieMaxAge(), true);
//返回用户信息
return ResponseEntity.ok(userInfo);
} catch (Exception e) {
//token以过期,或者token篡改
throw new BlException(ExceptionEnum.UN_AUTHORIZED);
}
}
}
5、创建Login.vue
登录博客
<el-button type=“primary” @click=“login” style=“margin-top: 80px”>登录
<el-button @click=“register” style=“margin-top: 80px”>注册账号
6、设置访问路径
import Vue from ‘vue’
import Router from ‘vue-router’
import HelloWorld from ‘@/components/HelloWorld’
import Article from ‘@/components/Article’
import Write from ‘@/components/Write’
import Login from ‘@/components/Login’
Vue.use(Router)
export default new Router({
routes: [
{
path: ‘/’,
name: ‘HelloWorld’,
component: HelloWorld
},
{
path: ‘/Article’,
name: ‘Article’,
component: Article
},
{
path: ‘/Write’,
name: ‘Write’,
component: Write
},
{
path: ‘/Login’,
name: ‘Login’,
component: Login
},
]
})
访问页面:http://localhost:8080/#/Login
五、实现发布文章的功能
1、在HelloWorld.vue 节目添加跳转写文章的功能
<el-button @click=“goToWrite”>写文章
小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Web前端开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频文章来源:https://www.toymoban.com/news/detail-853056.html
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注:前端)
rocess=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YyX6Iqz56eR5oqA,size_20,color_FFFFFF,t_70,g_se,x_16)
五、实现发布文章的功能
1、在HelloWorld.vue 节目添加跳转写文章的功能
<el-button @click=“goToWrite”>写文章
小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Web前端开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-6M4L60Cf-1710879618642)]
[外链图片转存中…(img-jbQOc38m-1710879618642)]
[外链图片转存中…(img-OjyafJ8e-1710879618643)]
[外链图片转存中…(img-toDzO1NM-1710879618643)]
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注:前端)
[外链图片转存中…(img-WoSqGpKP-1710879618644)]文章来源地址https://www.toymoban.com/news/detail-853056.html
到了这里,关于Java之Spring Boot+Vue+Element UI前后端分离项目,前端插件化主流框架和实现原理的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!