Merge branch 'master' into 메뉴_등록
This commit is contained in:
@@ -1,9 +1,17 @@
|
||||
package com.justpickup.orderservice;
|
||||
|
||||
import com.justpickup.orderservice.domain.order.entity.Order;
|
||||
import com.justpickup.orderservice.domain.order.repository.OrderRepository;
|
||||
import com.justpickup.orderservice.domain.orderItem.entity.OrderItem;
|
||||
import com.justpickup.orderservice.domain.orderItemOption.entity.OrderItemOption;
|
||||
import com.justpickup.orderservice.domain.transaction.entity.Transaction;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableEurekaClient
|
||||
@@ -14,4 +22,32 @@ public class OrderServiceApplication {
|
||||
SpringApplication.run(OrderServiceApplication.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Transactional
|
||||
CommandLineRunner run(OrderRepository orderRepository) {
|
||||
return args -> {
|
||||
Long userId = 1L;
|
||||
Long userCouponId = null;
|
||||
Long storeId = 1L;
|
||||
Long orderPrice = 1000L;
|
||||
|
||||
for (int i = 1; i <= 20; i++) {
|
||||
Transaction transaction = Transaction.of();
|
||||
|
||||
Long itemId = 1L;
|
||||
Long price = 100L;
|
||||
Long count = 1L;
|
||||
OrderItem orderItem = OrderItem.of(itemId + i, price * i, count + i,
|
||||
OrderItemOption.of(), OrderItemOption.of());
|
||||
|
||||
OrderItem orderItem1 = OrderItem.of(itemId + i + 1, price * (i + 1), count + (i + 1),
|
||||
OrderItemOption.of(), OrderItemOption.of());
|
||||
|
||||
Order order = Order.of(userId, userCouponId, storeId, orderPrice * i, transaction, orderItem, orderItem1);
|
||||
|
||||
orderRepository.save(order);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import org.hibernate.annotations.BatchSize;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@@ -37,11 +38,39 @@ public class Order extends BaseEntity {
|
||||
@Enumerated(EnumType.STRING)
|
||||
private OrderStatus orderStatus;
|
||||
|
||||
@OneToOne(mappedBy = "order", fetch = FetchType.LAZY)
|
||||
@OneToOne(mappedBy = "order", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
|
||||
private Transaction transaction;
|
||||
|
||||
@BatchSize(size = 100)
|
||||
@OneToMany(mappedBy = "order")
|
||||
private List<OrderItem> orderItems;
|
||||
@OneToMany(mappedBy = "order", cascade = CascadeType.ALL)
|
||||
private List<OrderItem> orderItems = new ArrayList<>();
|
||||
|
||||
public static Order of(Long userId, Long userCouponId, Long storeId, Long orderPrice,
|
||||
Transaction transaction, OrderItem... orderItems) {
|
||||
Order order = new Order();
|
||||
order.userId = userId;
|
||||
order.userCouponId = userCouponId;
|
||||
order.storeId = storeId;
|
||||
order.orderPrice = orderPrice;
|
||||
|
||||
order.setTransaction(transaction);
|
||||
for (OrderItem orderItem : orderItems) {
|
||||
order.addOrderItem(orderItem);
|
||||
}
|
||||
|
||||
order.usedPoint = 0L;
|
||||
order.orderStatus = OrderStatus.PLACED;
|
||||
order.orderTime = LocalDateTime.now();
|
||||
return order;
|
||||
}
|
||||
|
||||
public void setTransaction(Transaction transaction) {
|
||||
this.transaction = transaction;
|
||||
transaction.setOrder(this);
|
||||
}
|
||||
|
||||
public void addOrderItem(OrderItem orderItem) {
|
||||
this.orderItems.add(orderItem);
|
||||
orderItem.setOrder(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@@ -27,11 +28,30 @@ public class OrderItem extends BaseEntity {
|
||||
@JoinColumn(name = "order_id")
|
||||
private Order order;
|
||||
|
||||
@OneToMany(mappedBy = "orderItem")
|
||||
private List<OrderItemOption> orderItemOptions;
|
||||
@OneToMany(mappedBy = "orderItem", cascade = CascadeType.ALL)
|
||||
private List<OrderItemOption> orderItemOptions = new ArrayList<>();
|
||||
|
||||
private Long price;
|
||||
|
||||
private Long count;
|
||||
|
||||
public void setOrder(Order order) {
|
||||
this.order = order;
|
||||
}
|
||||
|
||||
public void addOrderItemOption(OrderItemOption orderItemOption) {
|
||||
this.orderItemOptions.add(orderItemOption);
|
||||
orderItemOption.setOrderItem(this);
|
||||
}
|
||||
|
||||
public static OrderItem of(Long itemId, Long price, Long count, OrderItemOption... orderItemOptions) {
|
||||
OrderItem orderItem = new OrderItem();
|
||||
orderItem.itemId = itemId;
|
||||
orderItem.price = price;
|
||||
orderItem.count = count;
|
||||
for (OrderItemOption orderItemOption : orderItemOptions) {
|
||||
orderItem.addOrderItemOption(orderItemOption);
|
||||
}
|
||||
return orderItem;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,4 +24,12 @@ public class OrderItemOption extends BaseEntity {
|
||||
|
||||
private Long itemOptionId;
|
||||
|
||||
public void setOrderItem(OrderItem orderItem) {
|
||||
this.orderItem = orderItem;
|
||||
}
|
||||
|
||||
public static OrderItemOption of() {
|
||||
OrderItemOption orderItemOption = new OrderItemOption();
|
||||
return orderItemOption;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,4 +22,13 @@ public class Transaction extends BaseEntity {
|
||||
@JoinColumn(name = "order_id")
|
||||
private Order order;
|
||||
|
||||
public void setOrder(Order order) {
|
||||
this.order = order;
|
||||
}
|
||||
|
||||
public static Transaction of() {
|
||||
Transaction transaction = new Transaction();
|
||||
return transaction;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package com.justpickup.ownerapigatewayservice;
|
||||
|
||||
import com.justpickup.ownerapigatewayservice.handler.GlobalExceptionHandler;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
|
||||
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableEurekaClient
|
||||
@@ -12,4 +15,9 @@ public class OwnerApigatewayServiceApplication {
|
||||
SpringApplication.run(OwnerApigatewayServiceApplication.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ErrorWebExceptionHandler globalExceptionHandler() {
|
||||
return new GlobalExceptionHandler();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -44,9 +44,7 @@ public class AuthorizationHeaderFilter extends AbstractGatewayFilterFactory<Auth
|
||||
// JWT 토큰 판별
|
||||
String token = authorizationHeader.replace("Bearer", "");
|
||||
|
||||
if (!jwtTokenProvider.validateJwtToken(token)) {
|
||||
return onError(exchange, "JWT token is not valid", HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
jwtTokenProvider.validateJwtToken(token);
|
||||
|
||||
String subject = jwtTokenProvider.getUserId(token);
|
||||
if (false == jwtTokenProvider.getRoles(token).contains("StoreOwner")) {
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.justpickup.ownerapigatewayservice.handler;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.jsonwebtoken.ExpiredJwtException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class GlobalExceptionHandler implements ErrorWebExceptionHandler {
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
@Override
|
||||
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
|
||||
|
||||
Class<? extends Throwable> exceptionClass = ex.getClass();
|
||||
|
||||
Map<String, Object> responseBody = new HashMap<>();
|
||||
if (exceptionClass == ExpiredJwtException.class) {
|
||||
exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
|
||||
exchange.getResponse().getHeaders().setContentType(MediaType.APPLICATION_JSON);
|
||||
responseBody.put("code", "EXPIRED");
|
||||
responseBody.put("message", "Access Token is Expired!");
|
||||
}
|
||||
|
||||
DataBuffer wrap = null;
|
||||
try {
|
||||
byte[] bytes = objectMapper.writeValueAsBytes(responseBody);
|
||||
wrap = exchange.getResponse().bufferFactory().wrap(bytes);
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return exchange.getResponse().writeWith(Flux.just(wrap));
|
||||
}
|
||||
}
|
||||
@@ -73,25 +73,12 @@ public class JwtTokenProvider {
|
||||
return (List<String>) getClaimsFromJwtToken(token).get("roles");
|
||||
}
|
||||
|
||||
public boolean validateJwtToken(String token) {
|
||||
public void validateJwtToken(String token) {
|
||||
try {
|
||||
Jwts.parser().setSigningKey(SECRET).parseClaimsJws(token);
|
||||
return true;
|
||||
} catch (SignatureException e) {
|
||||
log.error("Invalid JWT signature: {}", e.getMessage());
|
||||
return false;
|
||||
} catch (MalformedJwtException e) {
|
||||
log.error("Invalid JWT token: {}", e.getMessage());
|
||||
return false;
|
||||
} catch (ExpiredJwtException e) {
|
||||
log.error("JWT token is expired: {}", e.getMessage());
|
||||
return false;
|
||||
} catch (UnsupportedJwtException e) {
|
||||
log.error("JWT token is unsupported: {}", e.getMessage());
|
||||
return false;
|
||||
} catch (IllegalArgumentException e) {
|
||||
log.error("JWT claims string is empty: {}", e.getMessage());
|
||||
return false;
|
||||
} catch (SignatureException | MalformedJwtException |
|
||||
UnsupportedJwtException | IllegalArgumentException | ExpiredJwtException jwtException) {
|
||||
throw jwtException;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ spring:
|
||||
globalcors:
|
||||
cors-configurations:
|
||||
'[/**]':
|
||||
allowedOrigins: "*"
|
||||
allowedOrigins: "http://localhost:8080"
|
||||
allowedMethods:
|
||||
- GET
|
||||
- POST
|
||||
@@ -32,6 +32,7 @@ spring:
|
||||
- OPTIONS
|
||||
- DELETE
|
||||
allowedHeaders: '*'
|
||||
allow-credentials: true
|
||||
routes:
|
||||
- id: owner-frontend-service
|
||||
uri: lb://OWNER-FRONTEND-SERVICE
|
||||
@@ -39,18 +40,22 @@ spring:
|
||||
- Path=/owner-frontend-service/**
|
||||
filters:
|
||||
- RewritePath=/owner-frontend-service/(?<segment>.*),/$\{segment}
|
||||
|
||||
- id: order-service
|
||||
uri: lb://ORDER-SERVCIE
|
||||
predicates:
|
||||
- Path=/order-service/**
|
||||
filters:
|
||||
- RewritePath=/order-service/(?<segment>.*),/$\{segment}
|
||||
- AuthorizationHeaderFilter
|
||||
|
||||
- id: store-service
|
||||
uri: lb://STORE-SERVCIE
|
||||
predicates:
|
||||
- Path=/store-service/**
|
||||
filters:
|
||||
- RewritePath=/store-service/(?<segment>.*),/$\{segment}
|
||||
|
||||
- id: user-service
|
||||
uri: lb://USER-SERVICE
|
||||
predicates:
|
||||
@@ -61,7 +66,7 @@ spring:
|
||||
- id: user-service
|
||||
uri: lb://USER-SERVICE
|
||||
predicates:
|
||||
- Path=/user-service/refreshToken
|
||||
- Path=/user-service/auth/reissue
|
||||
- Method=GET
|
||||
filters:
|
||||
- RewritePath=/user-service/(?<segment>.*),/$\{segment}
|
||||
@@ -85,6 +90,7 @@ spring:
|
||||
- Path=/user-service/**
|
||||
filters:
|
||||
- RewritePath=/user-service/(?<segment>.*),/$\{segment}
|
||||
- AuthorizationHeaderFilter
|
||||
|
||||
token:
|
||||
access-expired-time: 3600000
|
||||
|
||||
26
owner-vue/src/api/auth.js
Normal file
26
owner-vue/src/api/auth.js
Normal file
@@ -0,0 +1,26 @@
|
||||
import axios from "axios";
|
||||
import jwt from "@/common/jwt";
|
||||
|
||||
export default {
|
||||
async requestReissue() {
|
||||
const config = {
|
||||
headers: {
|
||||
"X-AUTH-TOKEN": jwt.getToken()
|
||||
}
|
||||
}
|
||||
|
||||
const res = await axios.get("http://localhost:8001/user-service/auth/reissue", config);
|
||||
|
||||
const accessToken = res.data.data.accessToken;
|
||||
jwt.saveToken(accessToken);
|
||||
jwt.saveExpiredTime(res.data.data.expiredTime);
|
||||
axios.defaults.headers.common['Authorization'] = "Bearer " + accessToken;
|
||||
|
||||
return accessToken;
|
||||
},
|
||||
requestCheckAccessToken() {
|
||||
axios.defaults.headers.common['Authorization'] = "Bearer " + jwt.getToken();
|
||||
|
||||
return axios.get("http://localhost:8001/user-service/auth/check/access-token");
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,32 @@
|
||||
import axios from "axios";
|
||||
import jwt from '../common/jwt.js';
|
||||
|
||||
export default {
|
||||
|
||||
requestRegisterUser(user) {
|
||||
return axios.post("http://localhost:8001/user-service/store-owner", user);
|
||||
},
|
||||
|
||||
async requestLoginUser(email, password) {
|
||||
const user = {
|
||||
email: email,
|
||||
password: password
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await axios.post("http://localhost:8001/user-service/login", user);
|
||||
const data = response.data.data;
|
||||
|
||||
jwt.saveToken(data.accessToken);
|
||||
jwt.saveExpiredTime(data.expiredTime);
|
||||
|
||||
axios.defaults.headers.common['Authorization'] = "Bearer " + data.accessToken;
|
||||
|
||||
return true;
|
||||
} catch (err) {
|
||||
console.log("Error = ", err);
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
import axios from "axios";
|
||||
|
||||
38
owner-vue/src/common/jwt.js
Normal file
38
owner-vue/src/common/jwt.js
Normal file
@@ -0,0 +1,38 @@
|
||||
|
||||
const moment = require('moment');
|
||||
const ACCESS_TOKEN_NAME = "accessToken";
|
||||
const EXPIRED_TIME_NAME = "expiredTime";
|
||||
|
||||
const tag = "[jwt]";
|
||||
|
||||
export default {
|
||||
getToken() {
|
||||
return localStorage.getItem(ACCESS_TOKEN_NAME);
|
||||
},
|
||||
saveToken(token) {
|
||||
localStorage.setItem(ACCESS_TOKEN_NAME, token);
|
||||
},
|
||||
getExpiredTime() {
|
||||
return localStorage.getItem(EXPIRED_TIME_NAME);
|
||||
},
|
||||
saveExpiredTime(expiredTime) {
|
||||
localStorage.setItem(EXPIRED_TIME_NAME, expiredTime);
|
||||
},
|
||||
destroyAll() {
|
||||
localStorage.removeItem(ACCESS_TOKEN_NAME);
|
||||
localStorage.removeItem(EXPIRED_TIME_NAME);
|
||||
},
|
||||
isExpired() {
|
||||
const expiredTime = this.getExpiredTime();
|
||||
|
||||
const expiredMoment = moment(expiredTime);
|
||||
let currentMoment = moment();
|
||||
|
||||
const difference = moment.duration(expiredMoment.diff(currentMoment)).asSeconds();
|
||||
|
||||
console.log(tag, "expireMoment = ", expiredMoment, "currentMoment = ", currentMoment, "diff = ", difference);
|
||||
|
||||
// 만료 30초 전일 경우 만료로 판단
|
||||
return difference <= 30;
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,11 @@
|
||||
import Vue from 'vue'
|
||||
import App from './App.vue'
|
||||
import vuetify from './plugins/vuetify'
|
||||
import router from './router'
|
||||
import Vue from 'vue';
|
||||
import App from './App.vue';
|
||||
import vuetify from './plugins/vuetify';
|
||||
import router from './router';
|
||||
import axios from "axios";
|
||||
import customUtil from './util/customUtil'
|
||||
import auth from "./api/auth.js";
|
||||
|
||||
axios.defaults.withCredentials = true;
|
||||
|
||||
Vue.config.productionTip = false
|
||||
Vue.prototype.$axios = axios;
|
||||
@@ -17,3 +18,29 @@ new Vue({
|
||||
router,
|
||||
render: h => h(App)
|
||||
}).$mount('#app')
|
||||
|
||||
axios.interceptors.response.use(
|
||||
(response) => {
|
||||
return response;
|
||||
},
|
||||
async (error) => {
|
||||
const originalRequest = error.config;
|
||||
if (error.response.status === 401) {
|
||||
let code = error.response.data.code;
|
||||
if (code === "EXPIRED") {
|
||||
console.log("## expired");
|
||||
try {
|
||||
const accessToken = await auth.requestReissue();
|
||||
originalRequest.headers.Authorization = "Bearer " + accessToken;
|
||||
return axios(originalRequest);
|
||||
} catch (reissueError) {
|
||||
window.location.href = "http://localhost:8080";
|
||||
alert("권한이 없습니다. 다시 로그인 해주세요");
|
||||
}
|
||||
}
|
||||
window.location.href = "http://localhost:8080";
|
||||
alert("권한이 없습니다. 다시 로그인해주세요.");
|
||||
}
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
@@ -4,13 +4,30 @@ import VueRouter from 'vue-router'
|
||||
import DashboardLayout from "@/views/Layout/DashboardLayout";
|
||||
import AuthLayout from "@/views/Layout/AuthLayout";
|
||||
|
||||
import jwt from "../common/jwt.js";
|
||||
import auth from "../api/auth.js";
|
||||
|
||||
Vue.use(VueRouter)
|
||||
|
||||
const authCheck = async function (to, from, next) {
|
||||
try {
|
||||
if (jwt.isExpired()) {
|
||||
// refresh 호출
|
||||
await auth.requestReissue();
|
||||
} else {
|
||||
await auth.requestCheckAccessToken();
|
||||
}
|
||||
} catch (error) {
|
||||
await router.replace("/login");
|
||||
}
|
||||
next();
|
||||
};
|
||||
const routes = [
|
||||
{
|
||||
path: '/dashboard',
|
||||
redirect: 'dashboard',
|
||||
component: DashboardLayout,
|
||||
beforeEnter: authCheck,
|
||||
children: [
|
||||
{
|
||||
path: "/dashboard",
|
||||
|
||||
61
owner-vue/src/views/LoginUser.vue
Normal file
61
owner-vue/src/views/LoginUser.vue
Normal file
@@ -0,0 +1,61 @@
|
||||
<template>
|
||||
<v-card width="800" class="mx-auto mt-5">
|
||||
<v-card-title>
|
||||
<h1>Login</h1>
|
||||
</v-card-title>
|
||||
<v-card-text>
|
||||
<v-form ref="form" lazy-validation>
|
||||
<v-text-field
|
||||
v-model="email"
|
||||
:rules="[v => /.+@.+\..+/.test(v) || 'E-mail must be valid', v => !!v || '이메일은 필수 값입니다']"
|
||||
label="이메일"
|
||||
prepend-icon="mdi-account-circle"
|
||||
></v-text-field>
|
||||
<v-text-field
|
||||
v-model="password"
|
||||
:rules="[v => !!v || '비밀번호는 필수 값입니다']"
|
||||
label="비밀번호"
|
||||
type="Password"
|
||||
prepend-icon="mdi-lock"
|
||||
append-icon="mdi-eye-off"
|
||||
></v-text-field>
|
||||
</v-form>
|
||||
</v-card-text>
|
||||
<v-divider></v-divider>
|
||||
<v-card-actions>
|
||||
<v-btn color="success" v-on:click="links('/register')">Register</v-btn>
|
||||
<v-spacer></v-spacer>
|
||||
<v-btn color="info" v-on:click="login">Login</v-btn>
|
||||
</v-card-actions>
|
||||
</v-card>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import userApi from '../api/user.js'
|
||||
|
||||
export default {
|
||||
name: "LoginUser",
|
||||
data: function() {
|
||||
return {
|
||||
email: 'owner@gmail.com',
|
||||
password: '1234'
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
links: function(url) {
|
||||
this.$router.push(url);
|
||||
},
|
||||
login: async function() {
|
||||
if (!this.$refs.form.validate()) return;
|
||||
|
||||
const flag = await userApi.requestLoginUser(this.email, this.password);
|
||||
|
||||
if (flag) await this.$router.push('/prev-order');
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
|
||||
</style>
|
||||
@@ -6,6 +6,6 @@ import org.springframework.http.HttpStatus;
|
||||
public class AccessTokenNotValidException extends CustomException {
|
||||
|
||||
public AccessTokenNotValidException(String message) {
|
||||
super(HttpStatus.FORBIDDEN, message);
|
||||
super(HttpStatus.UNAUTHORIZED, message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.justpickup.userservice.domain.jwt.service;
|
||||
|
||||
public interface AccessTokenService {
|
||||
|
||||
void checkAccessToken(String authorizationHeader);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.justpickup.userservice.domain.jwt.service;
|
||||
|
||||
import com.justpickup.userservice.domain.jwt.exception.AccessTokenNotValidException;
|
||||
import com.justpickup.userservice.global.utils.JwtTokenProvider;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class AccessTokenServiceImpl implements AccessTokenService {
|
||||
|
||||
private final JwtTokenProvider tokenProvider;
|
||||
|
||||
@Override
|
||||
public void checkAccessToken(String authorizationHeader) {
|
||||
|
||||
String token = authorizationHeader.replace("Bearer", "");
|
||||
|
||||
if (!tokenProvider.validateJwtToken(token)) {
|
||||
throw new AccessTokenNotValidException("Access Token is not Valid");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@ import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -69,9 +70,11 @@ public class RefreshTokenServiceImpl implements RefreshTokenService {
|
||||
.stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList());
|
||||
|
||||
String newAccessToken = jwtTokenProvider.createJwtAccessToken(userId, "/refreshToken", roles);
|
||||
Date expiredTime = jwtTokenProvider.getExpiredTime(newAccessToken);
|
||||
|
||||
return JwtTokenDto.builder()
|
||||
.accessToken(newAccessToken)
|
||||
.accessTokenExpiredDate(expiredTime)
|
||||
.refreshToken(refreshToken)
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.justpickup.userservice.domain.jwt.web;
|
||||
|
||||
import com.justpickup.userservice.domain.jwt.service.RefreshTokenServiceImpl;
|
||||
import com.justpickup.userservice.domain.jwt.service.AccessTokenService;
|
||||
import com.justpickup.userservice.domain.jwt.service.RefreshTokenService;
|
||||
import com.justpickup.userservice.domain.user.dto.JwtTokenDto;
|
||||
import com.justpickup.userservice.global.dto.Result;
|
||||
import com.justpickup.userservice.global.utils.CookieProvider;
|
||||
@@ -15,20 +16,22 @@ import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.ws.rs.core.HttpHeaders;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/auth")
|
||||
@Slf4j
|
||||
public class AuthController {
|
||||
|
||||
private final RefreshTokenServiceImpl refreshTokenServiceImpl;
|
||||
private final RefreshTokenService refreshTokenService;
|
||||
private final AccessTokenService accessTokenService;
|
||||
private final CookieProvider cookieProvider;
|
||||
|
||||
@GetMapping("/refreshToken")
|
||||
@GetMapping("/reissue")
|
||||
public ResponseEntity<Result> refreshToken(@RequestHeader("X-AUTH-TOKEN") String accessToken,
|
||||
@CookieValue("refresh-token") String refreshToken) {
|
||||
|
||||
JwtTokenDto jwtTokenDto = refreshTokenServiceImpl.refreshJwtToken(accessToken, refreshToken);
|
||||
JwtTokenDto jwtTokenDto = refreshTokenService.refreshJwtToken(accessToken, refreshToken);
|
||||
|
||||
ResponseCookie responseCookie = cookieProvider.createRefreshTokenCookie(refreshToken);
|
||||
|
||||
@@ -42,9 +45,12 @@ public class AuthController {
|
||||
@AllArgsConstructor
|
||||
static class RefreshTokenResponse {
|
||||
private String accessToken;
|
||||
private String expiredTime;
|
||||
|
||||
public RefreshTokenResponse(JwtTokenDto jwtTokenDto) {
|
||||
this.accessToken = jwtTokenDto.getAccessToken();
|
||||
this.expiredTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
|
||||
.format(jwtTokenDto.getAccessTokenExpiredDate());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,7 +58,7 @@ public class AuthController {
|
||||
public ResponseEntity<Result> logout(@RequestHeader("X-AUTH-TOKEN") String accessToken,
|
||||
@RequestHeader("REFRESH-TOKEN") String refreshToken) {
|
||||
|
||||
refreshTokenServiceImpl.logoutToken(accessToken);
|
||||
refreshTokenService.logoutToken(accessToken);
|
||||
|
||||
ResponseCookie refreshCookie = cookieProvider.removeRefreshTokenCookie();
|
||||
|
||||
@@ -60,4 +66,13 @@ public class AuthController {
|
||||
.header(HttpHeaders.SET_COOKIE, refreshCookie.toString())
|
||||
.body(Result.createErrorResult(""));
|
||||
}
|
||||
|
||||
@GetMapping("/check/access-token")
|
||||
public ResponseEntity<Result> checkAccessToken(@RequestHeader(name = "Authorization") String authorization) {
|
||||
|
||||
accessTokenService.checkAccessToken(authorization);
|
||||
|
||||
return ResponseEntity.status(HttpStatus.OK)
|
||||
.body(Result.createSuccessResult(null));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,14 +3,18 @@ package com.justpickup.userservice.domain.user.dto;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Getter
|
||||
public class JwtTokenDto {
|
||||
private String accessToken;
|
||||
private Date accessTokenExpiredDate;
|
||||
private String refreshToken;
|
||||
|
||||
@Builder
|
||||
public JwtTokenDto(String accessToken, String refreshToken) {
|
||||
public JwtTokenDto(String accessToken, String refreshToken, Date accessTokenExpiredDate) {
|
||||
this.accessToken = accessToken;
|
||||
this.refreshToken = refreshToken;
|
||||
this.accessTokenExpiredDate = accessTokenExpiredDate;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ public class GlobalExceptionHandler {
|
||||
// 쿠키 삭제
|
||||
ResponseCookie responseCookie = cookieProvider.removeRefreshTokenCookie();
|
||||
|
||||
return ResponseEntity.status(HttpStatus.FORBIDDEN)
|
||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
|
||||
.header(HttpHeaders.SET_COOKIE, responseCookie.toString())
|
||||
.body(e.getResult());
|
||||
}
|
||||
|
||||
@@ -18,11 +18,12 @@ import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
||||
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -70,6 +71,7 @@ public class LoginAuthenticationFilter extends UsernamePasswordAuthenticationFil
|
||||
String userId = user.getUsername();
|
||||
|
||||
String accessToken = jwtTokenProvider.createJwtAccessToken(userId, request.getRequestURI(), roles);
|
||||
Date expiredTime = jwtTokenProvider.getExpiredTime(accessToken);
|
||||
String refreshToken = jwtTokenProvider.createJwtRefreshToken();
|
||||
|
||||
refreshTokenServiceImpl.updateRefreshToken(Long.valueOf(userId), jwtTokenProvider.getRefreshTokenId(refreshToken));
|
||||
@@ -83,17 +85,11 @@ public class LoginAuthenticationFilter extends UsernamePasswordAuthenticationFil
|
||||
response.addCookie(cookie);
|
||||
|
||||
// body 설정
|
||||
Map<String, String> tokens = Map.of(
|
||||
"access_token", accessToken
|
||||
Map<String, Object> tokens = Map.of(
|
||||
"accessToken", accessToken,
|
||||
"expiredTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(expiredTime)
|
||||
);
|
||||
|
||||
new ObjectMapper().writeValue(response.getOutputStream(), Result.createSuccessResult(tokens));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void unsuccessfulAuthentication
|
||||
(HttpServletRequest request, HttpServletResponse response, AuthenticationException failed)
|
||||
throws IOException, ServletException {
|
||||
log.warn("로그인 실패!!");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,10 +5,6 @@ import io.jsonwebtoken.*;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Date;
|
||||
@@ -57,8 +53,6 @@ public class JwtTokenProvider {
|
||||
.compact();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String getUserId(String token) {
|
||||
return getClaimsFromJwtToken(token).getSubject();
|
||||
}
|
||||
@@ -75,6 +69,10 @@ public class JwtTokenProvider {
|
||||
return getClaimsFromJwtToken(token).get("value").toString();
|
||||
}
|
||||
|
||||
public Date getExpiredTime(String token) {
|
||||
return getClaimsFromJwtToken(token).getExpiration();
|
||||
}
|
||||
|
||||
public List<String> getRoles(String token) {
|
||||
return (List<String>) getClaimsFromJwtToken(token).get("roles");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user