Skip to content

Commit

Permalink
JWT 토큰 시간 변경, existDataOptional타입 변경(Optional)
Browse files Browse the repository at this point in the history
  • Loading branch information
chobeebee committed Jul 1, 2024
1 parent e2d4a49 commit 88abeaf
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 26 deletions.
39 changes: 22 additions & 17 deletions src/main/java/com/sparta/binplay/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.web.cors.CorsConfiguration;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;

@Configuration
@EnableWebSecurity
Expand All @@ -36,6 +39,25 @@ public SecurityConfig(CustomOAuth2UserService customOAuth2UserService, CustomSuc

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// CORS 설정
http
.cors(corsCustomizer -> corsCustomizer.configurationSource(request -> {

CorsConfiguration configuration = new CorsConfiguration();

configuration.setAllowedOrigins(Collections.singletonList("http://localhost:3000"));
configuration.setAllowedMethods(Collections.singletonList("*"));
configuration.setAllowCredentials(true);
configuration.setAllowedHeaders(Collections.singletonList("*"));
configuration.setMaxAge(3600L);

// configuration.setExposedHeaders(Collections.singletonList("Set-Cookie"));
// configuration.setExposedHeaders(Collections.singletonList("Authorization"));

configuration.setExposedHeaders(Arrays.asList("Set-Cookie","Authorization"));

return configuration;
}));

//csrf disable
http
Expand Down Expand Up @@ -74,23 +96,6 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.sessionManagement((session) -> session
.sessionCreationPolicy(SessionCreationPolicy.STATELESS));
// // CORS 설정
// http
// .cors(corsCustomizer -> corsCustomizer.configurationSource(request -> {
//
// CorsConfiguration configuration = new CorsConfiguration();
//
// configuration.setAllowedOrigins(Collections.singletonList("http://localhost:3000"));
// configuration.setAllowedMethods(Collections.singletonList("*"));
// configuration.setAllowCredentials(true);
// configuration.setAllowedHeaders(Collections.singletonList("*"));
// configuration.setMaxAge(3600L);
//
// configuration.setExposedHeaders(Collections.singletonList("Set-Cookie"));
// configuration.setExposedHeaders(Collections.singletonList("Authorization"));
//
// return configuration;
// }));

//로그아웃 설정
http
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/sparta/binplay/jwt/JWTFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected boolean shouldNotFilter(HttpServletRequest request) throws ServletExce

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
System.out.println("까꿍");

//cookie들을 불러온 뒤 Authorization Key에 담긴 쿠키를 찾음
String authorization = null;
Cookie[] cookies = request.getCookies();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
GrantedAuthority auth = iterator.next();
String role = auth.getAuthority();

String token = jwtUtil.createJwt(username, role, 180000L);
String token = jwtUtil.createJwt(username, role, 24*60*60*1000); //1일

response.addCookie(createCookie("Authorization", token));
response.sendRedirect("http://localhost:8080/");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.sparta.binplay.service;

import com.sparta.binplay.dto.UserDTO;
import com.sparta.binplay.dto.response.GoogleResponse;
import com.sparta.binplay.dto.response.OAuth2Response;
import com.sparta.binplay.dto.UserDTO;
import com.sparta.binplay.entity.CustomOAuth2User;
import com.sparta.binplay.entity.Role;
import com.sparta.binplay.entity.Users;
Expand All @@ -14,11 +14,13 @@
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Service;

import java.util.Optional;

@RequiredArgsConstructor
@Service
public class CustomOAuth2UserService extends DefaultOAuth2UserService {
private final UserRepository userRepository;

//유저 정보 DB 저장
@Override
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
Expand All @@ -33,11 +35,11 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic
} else {
return null;
}

String username = oAuth2Response.getProvider()+" "+oAuth2Response.getProviderId();
Users existData = userRepository.findByUsername(username);
Optional<Users> existDataOptional = userRepository.findByUsername(username);

if (existData == null) {
if (existDataOptional.isEmpty()) {

Users user = new Users();
user.setUsername(username);
Expand All @@ -55,6 +57,7 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic
return new CustomOAuth2User(userDTO);
}
else {
Users existData = existDataOptional.get();
existData.setEmail(oAuth2Response.getEmail());
existData.setName(oAuth2Response.getName());

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/sparta/binplay/service/JWTUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Boolean isExpired(String token) {
return Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(token).getBody().getExpiration().before(new Date());
}

public String createJwt(String username, String role, Long expiredMs) {
public String createJwt(String username, String role, int expiredMs) {

Claims claims = Jwts.claims();
claims.put("username", username);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.sparta.binplay.service;

public class ResourceNotFoundException extends RuntimeException {
public ResourceNotFoundException(String message) {
super(message);
}
}
3 changes: 2 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
server.port=8080
# Hibernate JPA ??
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
Expand Down

0 comments on commit 88abeaf

Please sign in to comment.