쓰기 들어가야함

This commit is contained in:
이진석
2020-02-03 10:58:45 +09:00
parent c2eb47fd9d
commit 6a59d1a541
12 changed files with 128 additions and 12 deletions

View File

@@ -0,0 +1,16 @@
import axios from "axios";
export default async function () {
try {
const token = this.$cookie.get('accessToken');
const result = await axios({
method: 'get',
url: '/api/users',
headers: {
'Authorization': 'Bearer ' + token
}
});
} catch (e) {
}
}

View File

@@ -1,15 +1,22 @@
<template>
<div>
<div v-for="article in articles" v-bind:key="article.id">
{{ article.title }}
<article v-for="article in articles" v-bind:key="article.id">
<span>{{ article.title }}</span>
<span>{{ article }}</span>
</article>
<div>
<router-link to="/articles/write">글쓰기</router-link>
</div>
</div>
</template>
<script>
import articleApi from "../../api/articleApi";
import authApi from "../../api/authApi";
export default {
name: "index",
name: "List",
data() {
return {
articles: [],
@@ -17,6 +24,13 @@
}
},
async beforeCreate() {
try {
await authApi.session(this.$cookie.get('accessToken'));
} catch (e) {
alert('토큰이 존재하지 않거나 올바르지 않은 토큰입니다.');
await this.$router.push('/');
}
try {
const result = await articleApi.getArticles.bind(this)({});
this.articles = result.data;

View File

@@ -0,0 +1,21 @@
<template>
<div>
<form ></form>
</div>
</template>
<script>
import articleApi from "../../api/articleApi";
import authApi from "../../api/authApi";
export default {
name: "Write",
methods: {
}
}
</script>
<style scoped>
</style>

View File

@@ -32,6 +32,7 @@
const result = await authApi.login({email, password});
const { token } = result.data;
this.$cookie.set('accessToken', token, 1000);
await this.$router.push('/articles');
} catch (err) {
console.log(err);
}

View File

@@ -4,7 +4,8 @@ import VueRouter from "vue-router";
import Welcome from "../pages/Welcome";
import Login from "../pages/auth/Login";
import Register from "../pages/auth/Register";
import Articles from "../pages/articles";
import Articles from "../pages/articles/List";
import WriteArticle from '../pages/articles/Write';
Vue.use(VueRouter);
@@ -26,6 +27,10 @@ export const router = new VueRouter({
{
path: '/articles',
component: Articles
},
{
path: '/articles/write',
component: WriteArticle
}
]
});

View File

@@ -1,8 +1,10 @@
package com.example.vue.config.security;
import com.example.vue.domain.auth.AuthException;
import com.example.vue.domain.user.User;
import com.example.vue.util.JwtUtil;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtException;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
@@ -31,6 +33,7 @@ public class JwtAuthenticationFilter extends BasicAuthenticationFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
Authentication authentication = getAuthentication(request);
if (authentication != null) {
@@ -49,13 +52,18 @@ public class JwtAuthenticationFilter extends BasicAuthenticationFilter {
return null;
}
Claims claims = jwtUtil.getClaims(token.substring("Bearer ".length()));
Claims claims;
try {
claims = jwtUtil.getClaims(token.substring("Bearer ".length()));
} catch (JwtException e) {
throw new AuthException.MalformedJwt(token);
}
Set<GrantedAuthority> roles = new HashSet<>();
String role = (String) claims.get("role");
roles.add(new SimpleGrantedAuthority("ROLE_" + role));
// return new UsernamePasswordAuthenticationToken(claims, null, roles);
return new UsernamePasswordAuthenticationToken(new User(claims), null, roles);
}

View File

@@ -1,6 +1,7 @@
package com.example.vue.domain.article;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
@@ -10,6 +11,7 @@ import java.time.LocalDateTime;
@Entity
@Getter
@NamedQuery(name = "findAll", query = "select a from Article a order by a.createdAt desc")
@NoArgsConstructor
public class Article {
@Id @GeneratedValue
@@ -27,4 +29,18 @@ public class Article {
@LastModifiedDate
private LocalDateTime updatedAt;
public Article(ArticleRequestDto articleRequestDto) {
this.title = articleRequestDto.getTitle();
this.content = articleRequestDto.getContent();
}
@PrePersist
private void prePersist() {
this.createdAt = LocalDateTime.now();
}
@PreUpdate
private void preUpdate() {
this.updatedAt = LocalDateTime.now();
}
}

View File

@@ -2,10 +2,9 @@ package com.example.vue.domain.article;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
@RestController
@@ -17,7 +16,11 @@ public class ArticleController {
@GetMapping
public List<ArticleResponseDto> getArticles(Pageable pageable) {
List<ArticleResponseDto> articles = articleService.findAll(pageable);
return articles;
return articleService.findAll(pageable);
}
@PostMapping
public ArticleResponseDto postArticles(@RequestBody @Valid ArticleRequestDto articleRequestDto) {
return articleService.save(articleRequestDto);
}
}

View File

@@ -14,6 +14,11 @@ public class ArticleRepository {
private final EntityManager em;
public Article save(Article article) {
em.persist(article);
return article;
}
public List<Article> findAll(Pageable pageable) {
int page = pageable.getPageNumber();
int size = pageable.getPageSize();

View File

@@ -0,0 +1,15 @@
package com.example.vue.domain.article;
import lombok.Data;
import javax.validation.constraints.NotNull;
@Data
public class ArticleRequestDto {
@NotNull
private String title;
@NotNull
private String content;
}

View File

@@ -1,7 +1,6 @@
package com.example.vue.domain.article;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
@@ -14,6 +13,11 @@ public class ArticleService {
private final ArticleRepository articleRepository;
public ArticleResponseDto save(ArticleRequestDto articleRequestDto) {
Article article = articleRepository.save(new Article(articleRequestDto));
return new ArticleResponseDto(article);
}
public List<ArticleResponseDto> findAll(Pageable pageable) {
return articleRepository.findAll(pageable)
.stream()

View File

@@ -1,5 +1,6 @@
package com.example.vue.domain.auth;
import io.jsonwebtoken.JwtException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@@ -25,4 +26,11 @@ public class AuthException {
super("이미 존재하는 사용자입니다. [email=" + email + "]");
}
}
@ResponseStatus(HttpStatus.BAD_REQUEST)
public static class MalformedJwt extends JwtException {
public MalformedJwt(String token) {
super("올바르지 않은 토큰 입니다. [token=" + token + "]");
}
}
}