Skip to content

Commit

Permalink
feat:[kakao-tech-campus-2nd-step3#84]- refact jwt
Browse files Browse the repository at this point in the history
로그 일부 추가, properties 추가, 비로그인 유저 접속 가능 url 추가
  • Loading branch information
yooonwodyd committed Nov 6, 2024
1 parent 814bb5f commit f011449
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
12 changes: 12 additions & 0 deletions src/main/java/com/helpmeCookies/global/config/ProPertyConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.helpmeCookies.global.config;

import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import com.helpmeCookies.global.jwt.JwtProperties;

@Configuration
@EnableConfigurationProperties(JwtProperties.class)
public class ProPertyConfig {
}
20 changes: 20 additions & 0 deletions src/main/java/com/helpmeCookies/global/jwt/JwtProperties.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.helpmeCookies.global.jwt;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import lombok.Getter;
import lombok.Setter;

@Component
@ConfigurationProperties(prefix = "jwt")
@Getter
@Setter
public class JwtProperties {
private String secret;
private long accessTokenExpireTime;
private long refreshTokenExpireTime;

public JwtProperties() {
}
}
14 changes: 6 additions & 8 deletions src/main/java/com/helpmeCookies/global/jwt/JwtProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.RequiredArgsConstructor;

@Component
@RequiredArgsConstructor
public class JwtProvider implements InitializingBean {
@Value("${jwt.secret}")
private String secret;
@Value("${jwt.access-token-expire-time}")
private long accessTokenExpireTime;
@Value("${jwt.refresh-token-expire-time}")
private long refreshTokenExpireTime;

private final JwtProperties jwtProperties;
private Key secretKey;
private static final String ROLE = "role";
private static final String IS_ACCESS_TOKEN = "isAccessToken";
Expand Down Expand Up @@ -95,7 +93,7 @@ private JwtUser claimsToJwtUser(Claims claims) {
}

private String generateToken(JwtUser jwtUser, boolean isAccessToken) {
long expireTime = isAccessToken ? accessTokenExpireTime : refreshTokenExpireTime;
long expireTime = isAccessToken ? jwtProperties.getAccessTokenExpireTime() : jwtProperties.getRefreshTokenExpireTime();
Date expireDate = new Date(System.currentTimeMillis() + expireTime);
return Jwts.builder()
.signWith(secretKey)
Expand All @@ -115,6 +113,6 @@ private Claims extractClaims(String rawToken) {

@Override
public void afterPropertiesSet() {
secretKey = new SecretKeySpec(secret.getBytes(), SignatureAlgorithm.HS256.getJcaName());
secretKey = new SecretKeySpec(jwtProperties.getSecret().getBytes(), SignatureAlgorithm.HS256.getJcaName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
@Component
public class JwtAuthenticationFilter extends OncePerRequestFilter {
private final JwtProvider jwtProvider;

private static final String AUTHORIZATION_HEADER = "Authorization";

@Override
Expand All @@ -46,6 +45,9 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
Authentication authentication = new UsernamePasswordAuthenticationToken(jwtUser, null,
jwtUser.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(authentication);
} else {
log.info("유효하지 않은 토큰 발생");
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "토큰이 유효하지 않습니다.");
}

filterChain.doFilter(request, response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
"/actuator/**",
"/v1/**",
"swagger-ui/**",
"/test/signup"
"/test/signup",
"/v1/artist",
"/v1/artists"
).permitAll()
.anyRequest().authenticated()
).exceptionHandling((exception) -> exception
Expand Down

0 comments on commit f011449

Please sign in to comment.