typeId : this.typeId,
}).then((res) => {
this.$router.push(“/”);
this.$message.success(“文章發(fā)布成功!”);
}).catch(() => {
this.$message.error(“文章發(fā)布失??!”);
});
},
handleAvatarSuccess(res, file) {
this.imageUrl = URL.createObjectURL(file.raw);
this.thumbnail = “http://localhost:9090/img/” + res;
},
selectType(typename,id) {
this.typeName = typename;
this.typeId = id;
},
beforeAvatarUpload(file) {
const isJPG = file.type === ‘image/jpeg’;
const isLt2M = file.size / 1024 / 1024 < 2;
if (!isJPG) {
this.$message.error(‘上傳頭像圖片只能是 JPG 格式!’);
}
if (!isLt2M) {
this.$message.error(‘上傳頭像圖片大小不能超過(guò) 2MB!’);
}
return isJPG && isLt2M;
}
}
}
三、設(shè)置Axios發(fā)起請(qǐng)求統(tǒng)一前綴的路徑
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) { // 請(qǐng)求失敗處理
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) { // 請(qǐng)求失敗處理
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();
},
四、實(shí)現(xiàn)登錄功能
1、創(chuàng)建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();
//判斷數(shù)據(jù)類型
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);
// 校驗(yàn)用戶名
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、創(chuàng)建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過(guò)期時(shí)間
private PublicKey publicKey; // 公鑰
private PrivateKey privateKey; // 私鑰
private String cookieName;
private Integer cookieMaxAge;
// private static final Logger logger = LoggerFactory.getLogger(JwtProperties.class);
/**
- @PostContruct:在構(gòu)造方法執(zhí)行之后執(zhí)行該方法
*/
@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、創(chuàng)建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;
/**
-
登錄授權(quán)
-
@return
*/
@GetMapping(“accredit”)
public ResponseEntity authentication(
@RequestParam(“email”) String email,
@RequestParam(“password”) String password,
HttpServletRequest request,
HttpServletResponse response) {
// 登錄校驗(yàn)
System.out.println(" 登錄校驗(yàn) 登錄校驗(yàn) 登錄校驗(yàn) 登錄校驗(yàn)");
String token = authService.authentication(email, password);
if (StringUtils.isBlank(token)) {
log.info(“用戶授權(quán)失敗”);
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以過(guò)期,或者token篡改
throw new BlException(ExceptionEnum.UN_AUTHORIZED);
}
}
}
5、創(chuàng)建Login.vue
登錄博客
<el-button type=“primary” @click=“l(fā)ogin” style=“margin-top: 80px”>登錄
<el-button @click=“register” style=“margin-top: 80px”>注冊(cè)賬號(hào)
6、設(shè)置訪問(wèn)路徑
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
},
]
})
訪問(wèn)頁(yè)面:http://localhost:8080/#/Login
五、實(shí)現(xiàn)發(fā)布文章的功能
1、在HelloWorld.vue 節(jié)目添加跳轉(zhuǎn)寫文章的功能
<el-button @click=“goToWrite”>寫文章
goToWrite() {
this.$router.push(“/Write”);
},
2、在Write.vue設(shè)置初始化的時(shí)候校驗(yàn)是否登錄
沒(méi)有登錄跳轉(zhuǎn)回登錄頁(yè)面
created() {//編寫構(gòu)造函數(shù)
this.$http.get(“verify/”)
.then((res) => {
}).catch(() => {
this.$router.push({path: “/Login”})
this.$message.error(“沒(méi)有登錄請(qǐng)登錄后發(fā)布文章!”);
});
},
3、實(shí)現(xiàn)文章保存功能
(1)修改Blog實(shí)體類用于接收參數(shù)
package cn.itbluebox.springbootcsdn.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Table(name = “blog”)
public class Blog {
private Long id; //文章id
private String title; //文章標(biāo)題
private String abstract_text; //文章內(nèi)容
private String thumbnail; //縮略圖
private Date create_time; //創(chuàng)建時(shí)間
private Long like_count; //點(diǎn)贊數(shù)量
private Long view_count; //瀏覽量
private Long consumer_id; //用戶ID
private String type_id; //類型
private Long blog_article_id; //博客文章ID
@Transient
private String context;
@Transient
private Date last_update_time; //更新時(shí)間
@Transient
private Character is_original;
@Transient //Transient聲明當(dāng)前字段不是數(shù)據(jù)對(duì)應(yīng)的字段
private Long[] typeId;
}
(2)在BlogController當(dāng)中創(chuàng)建保存的接口
@PostMapping(“save”)
public ResponseEntity saveBlogging(
@RequestBody Blog blog,
@CookieValue(“SC_TOKEN”) String token,
HttpServletRequest request, HttpServletResponse response
){
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);
//返回用戶信息
blog.setConsumer_id(userInfo.getId());
String blog_code = blogService.saveBlogging(blog);
return ResponseEntity.ok(blog_code);
}
(3)在blogService和對(duì)應(yīng)實(shí)現(xiàn)類當(dāng)中創(chuàng)建對(duì)應(yīng)的方法
String saveBlogging(Blog blog);
實(shí)現(xiàn)類當(dāng)中的方法
(4)實(shí)現(xiàn)BlogServiceImpl和對(duì)應(yīng)的Mapper
@Transactional
@Override
public String saveBlogging(Blog blog) {
//先插入博客的文章部分
long l = new IdWorker(new Random().nextInt(10), 1).nextId();
String ls = (l + “”);
ls = ls.substring(5, ls.length());
BlogArticle blogArticle = new BlogArticle();
blogArticle.setId(Long.parseLong(ls));
blogArticle.setContext(blog.getContext());
blogArticle.setLast_update_time(new Date());
blogArticle.setIs_original(blog.getIs_original());
//插入博客文章的代碼
int insert2 = blogArticleMapper.insertSql(blogArticle);
if (insert2 != 1) {
throw new BlException(ExceptionEnum.BLOG_SAVE_ERROR);
}
//插入博客
blog.setCreate_time(new Date());
blog.setLike_count(1L);
blog.setView_count(1L);
blog.setBlog_article_id(blogArticle.getId());
int insert1 = blogMapper.insert(blog);
if (insert1 != 1) {
throw new BlException(ExceptionEnum.BLOG_SAVE_ERROR);
}
return “success”;
}
自我介紹一下,小編13年上海交大畢業(yè),曾經(jīng)在小公司待過(guò),也去過(guò)華為、OPPO等大廠,18年進(jìn)入阿里一直到現(xiàn)在。
深知大多數(shù)前端工程師,想要提升技能,往往是自己摸索成長(zhǎng)或者是報(bào)班學(xué)習(xí),但對(duì)于培訓(xùn)機(jī)構(gòu)動(dòng)則幾千的學(xué)費(fèi),著實(shí)壓力不小。自己不成體系的自學(xué)效果低效又漫長(zhǎng),而且極易碰到天花板技術(shù)停滯不前!
因此收集整理了一份《2024年Web前端開(kāi)發(fā)全套學(xué)習(xí)資料》,初衷也很簡(jiǎn)單,就是希望能夠幫助到想自學(xué)提升又不知道該從何學(xué)起的朋友,同時(shí)減輕大家的負(fù)擔(dān)。
既有適合小白學(xué)習(xí)的零基礎(chǔ)資料,也有適合3年以上經(jīng)驗(yàn)的小伙伴深入學(xué)習(xí)提升的進(jìn)階課程,基本涵蓋了95%以上前端開(kāi)發(fā)知識(shí)點(diǎn),真正體系化!
由于文件比較大,這里只是將部分目錄大綱截圖出來(lái),每個(gè)節(jié)點(diǎn)里面都包含大廠面經(jīng)、學(xué)習(xí)筆記、源碼講義、實(shí)戰(zhàn)項(xiàng)目、講解視頻,并且后續(xù)會(huì)持續(xù)更新
如果你覺(jué)得這些內(nèi)容對(duì)你有幫助,可以添加V獲?。簐ip1024c (備注前端)
框架相關(guān)
原生JS雖能實(shí)現(xiàn)絕大部分功能,但要么就是過(guò)于繁瑣,要么就是存在缺陷,故絕大多數(shù)開(kāi)發(fā)者都會(huì)首選框架開(kāi)發(fā)方案?,F(xiàn)階段較熱門是React、Vue兩大框架,兩者工作原理上存在共通點(diǎn),也存在一些不同點(diǎn),對(duì)于校招來(lái)說(shuō),不需要兩個(gè)框架都學(xué)得特別熟,一般面試官會(huì)針對(duì)你簡(jiǎn)歷中寫的框架進(jìn)行提問(wèn)。
在框架方面,生命周期、鉤子函數(shù)、虛擬DOM這些基本知識(shí)是必須要掌握的,在學(xué)習(xí)的過(guò)程可以結(jié)合框架的官方文檔
CodeChina開(kāi)源項(xiàng)目:【大廠前端面試題解析+核心總結(jié)學(xué)習(xí)筆記+真實(shí)項(xiàng)目實(shí)戰(zhàn)+最新講解視頻】
Vue框架
知識(shí)要點(diǎn):
1. vue-cli工程
2. vue核心知識(shí)點(diǎn)
3. vue-router
4. vuex
5. http請(qǐng)求
6. UI樣式
7. 常用功能
8. MVVM設(shè)計(jì)模式
React框架
知識(shí)要點(diǎn):
1. 基本知識(shí)
2. React 組件
3. React Redux
4. React 路由文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-859706.html
在小公司待過(guò),也去過(guò)華為、OPPO等大廠,18年進(jìn)入阿里一直到現(xiàn)在。**
深知大多數(shù)前端工程師,想要提升技能,往往是自己摸索成長(zhǎng)或者是報(bào)班學(xué)習(xí),但對(duì)于培訓(xùn)機(jī)構(gòu)動(dòng)則幾千的學(xué)費(fèi),著實(shí)壓力不小。自己不成體系的自學(xué)效果低效又漫長(zhǎng),而且極易碰到天花板技術(shù)停滯不前!
因此收集整理了一份《2024年Web前端開(kāi)發(fā)全套學(xué)習(xí)資料》,初衷也很簡(jiǎn)單,就是希望能夠幫助到想自學(xué)提升又不知道該從何學(xué)起的朋友,同時(shí)減輕大家的負(fù)擔(dān)。
[外鏈圖片轉(zhuǎn)存中…(img-ztmNrmgi-1711916461473)]
[外鏈圖片轉(zhuǎn)存中…(img-gX1bfGqP-1711916461474)]
[外鏈圖片轉(zhuǎn)存中…(img-3MoOL1BU-1711916461474)]
[外鏈圖片轉(zhuǎn)存中…(img-gDfxo3QN-1711916461474)]
[外鏈圖片轉(zhuǎn)存中…(img-xT17kWxM-1711916461475)]
[外鏈圖片轉(zhuǎn)存中…(img-7Yeqlg52-1711916461475)]
既有適合小白學(xué)習(xí)的零基礎(chǔ)資料,也有適合3年以上經(jīng)驗(yàn)的小伙伴深入學(xué)習(xí)提升的進(jìn)階課程,基本涵蓋了95%以上前端開(kāi)發(fā)知識(shí)點(diǎn),真正體系化!
由于文件比較大,這里只是將部分目錄大綱截圖出來(lái),每個(gè)節(jié)點(diǎn)里面都包含大廠面經(jīng)、學(xué)習(xí)筆記、源碼講義、實(shí)戰(zhàn)項(xiàng)目、講解視頻,并且后續(xù)會(huì)持續(xù)更新
如果你覺(jué)得這些內(nèi)容對(duì)你有幫助,可以添加V獲?。簐ip1024c (備注前端)
[外鏈圖片轉(zhuǎn)存中…(img-Xh7WBZHx-1711916461475)]
框架相關(guān)
原生JS雖能實(shí)現(xiàn)絕大部分功能,但要么就是過(guò)于繁瑣,要么就是存在缺陷,故絕大多數(shù)開(kāi)發(fā)者都會(huì)首選框架開(kāi)發(fā)方案?,F(xiàn)階段較熱門是React、Vue兩大框架,兩者工作原理上存在共通點(diǎn),也存在一些不同點(diǎn),對(duì)于校招來(lái)說(shuō),不需要兩個(gè)框架都學(xué)得特別熟,一般面試官會(huì)針對(duì)你簡(jiǎn)歷中寫的框架進(jìn)行提問(wèn)。
在框架方面,生命周期、鉤子函數(shù)、虛擬DOM這些基本知識(shí)是必須要掌握的,在學(xué)習(xí)的過(guò)程可以結(jié)合框架的官方文檔
CodeChina開(kāi)源項(xiàng)目:【大廠前端面試題解析+核心總結(jié)學(xué)習(xí)筆記+真實(shí)項(xiàng)目實(shí)戰(zhàn)+最新講解視頻】
Vue框架
知識(shí)要點(diǎn):
1. vue-cli工程
2. vue核心知識(shí)點(diǎn)
3. vue-router
4. vuex
5. http請(qǐng)求
6. UI樣式
7. 常用功能
8. MVVM設(shè)計(jì)模式
[外鏈圖片轉(zhuǎn)存中…(img-FT3QokTX-1711916461476)]
React框架
知識(shí)要點(diǎn):
1. 基本知識(shí)
2. React 組件
3. React Redux
4. React 路由
[外鏈圖片轉(zhuǎn)存中…(img-zngYFMHS-1711916461476)]文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-859706.html
到了這里,關(guān)于Java之Spring Boot+Vue+Element UI前后端分離項(xiàng)目的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!