-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #292 from twenty-three-23/feature/TT-346-filter-chain
TT-346 filter chain
- Loading branch information
Showing
20 changed files
with
513 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/com/twentythree/peech/auth/controller/AuthController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.twentythree.peech.auth.controller; | ||
|
||
import com.twentythree.peech.auth.service.ReissueAccessAndRefreshTokenService; | ||
import com.twentythree.peech.user.dto.AccessAndRefreshToken; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/v1.1/auth") | ||
public class AuthController { | ||
|
||
private final ReissueAccessAndRefreshTokenService reissueAccessAndRefreshTokenService; | ||
|
||
@PatchMapping("/reissue") | ||
public AccessAndRefreshToken reissueAccessToken(String refreshToken) { | ||
return reissueAccessAndRefreshTokenService.createNewToken(refreshToken); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/twentythree/peech/auth/controller/SwaggerAuthController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.twentythree.peech.auth.controller; | ||
|
||
import com.twentythree.peech.user.dto.AccessAndRefreshToken; | ||
import io.swagger.v3.oas.annotations.media.Content; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
|
||
public interface SwaggerAuthController { | ||
@ApiResponse(responseCode = "200", description = "성공", content = {@Content(schema = @Schema(implementation = AccessAndRefreshToken.class), mediaType = "application/json")}) | ||
@ApiResponse(responseCode = "401", description = "실패", content = {@Content(schema = @Schema(implementation = Error.class), mediaType = "application/json")}) | ||
AccessAndRefreshToken reissueAccessToken(@RequestBody String refreshToken); | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/twentythree/peech/auth/service/ReissueAccessAndRefreshTokenService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.twentythree.peech.auth.service; | ||
|
||
import com.twentythree.peech.common.utils.JWTUtils; | ||
import com.twentythree.peech.common.utils.UserRoleConvertUtils; | ||
import com.twentythree.peech.security.jwt.JWTAuthentication; | ||
import com.twentythree.peech.user.dto.AccessAndRefreshToken; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ReissueAccessAndRefreshTokenService { | ||
|
||
private final JWTUtils jwtUtils; | ||
|
||
public AccessAndRefreshToken createNewToken(String refreshToken) { | ||
|
||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
JWTAuthentication jwtAuthentication = (JWTAuthentication) authentication.getPrincipal(); | ||
|
||
Long userId = jwtAuthentication.getUserId(); | ||
GrantedAuthority Authority = authentication.getAuthorities() | ||
.stream().findFirst().orElseThrow(() -> new NoSuchElementException("No authorities found for the user")); | ||
|
||
String newAccessToken = jwtUtils.createAccessToken(userId, UserRoleConvertUtils | ||
.convertStringToUserRole(Authority.getAuthority())); | ||
String newRefreshToken = jwtUtils.createRefreshToken(userId, UserRoleConvertUtils | ||
.convertStringToUserRole(Authority.getAuthority())); | ||
|
||
return new AccessAndRefreshToken(newAccessToken, newRefreshToken); | ||
} | ||
} | ||
|
||
|
7 changes: 7 additions & 0 deletions
7
src/main/java/com/twentythree/peech/common/exception/AccessTokenExpiredException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.twentythree.peech.common.exception; | ||
|
||
public class AccessTokenExpiredException extends RuntimeException { | ||
public AccessTokenExpiredException(String message) { | ||
super(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/main/java/com/twentythree/peech/common/exception/RefreshTokenExpiredException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.twentythree.peech.common.exception; | ||
|
||
import io.jsonwebtoken.ExpiredJwtException; | ||
|
||
public class RefreshTokenExpiredException extends RuntimeException{ | ||
public RefreshTokenExpiredException(String message) { | ||
super(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/java/com/twentythree/peech/common/utils/UserRoleConvertUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.twentythree.peech.common.utils; | ||
|
||
import com.twentythree.peech.user.value.UserRole; | ||
|
||
public class UserRoleConvertUtils { | ||
|
||
public static UserRole convertStringToUserRole(String role) { | ||
|
||
if (role.equals("ROLE_ADMIN")) { | ||
return UserRole.ROLE_ADMIN; | ||
} else if (role.equals("ROLE_COMMON")) { | ||
return UserRole.ROLE_COMMON; | ||
} else { | ||
throw new IllegalArgumentException("존재하지 않는 권한입니다."); | ||
} | ||
|
||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/twentythree/peech/security/config/JWTSecurityConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.twentythree.peech.security.config; | ||
|
||
import com.twentythree.peech.common.utils.JWTUtils; | ||
import com.twentythree.peech.security.filter.JWTAuthenticationFilter; | ||
import com.twentythree.peech.security.handler.JWTAuthAccessDeniedHandler; | ||
import com.twentythree.peech.security.jwt.JWTAuthenticationProvider; | ||
import com.twentythree.peech.user.service.UserService; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class JWTSecurityConfig { | ||
|
||
private final JWTUtils jwtUtils; | ||
private final UserService userService; | ||
|
||
public JWTSecurityConfig(JWTUtils jwtUtils, UserService userService) { | ||
this.jwtUtils = jwtUtils; | ||
this.userService = userService; | ||
} | ||
|
||
@Bean | ||
public JWTAuthenticationFilter jwtAuthenticationFilter() { | ||
return new JWTAuthenticationFilter(jwtUtils); | ||
} | ||
|
||
@Bean | ||
public JWTAuthenticationProvider jwtAuthenticationProvider() { | ||
return new JWTAuthenticationProvider(userService); | ||
} | ||
|
||
@Bean | ||
public JWTAuthAccessDeniedHandler authAccessDeniedHandler() { | ||
return new JWTAuthAccessDeniedHandler(); | ||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/com/twentythree/peech/security/config/SecurityConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.twentythree.peech.security.config; | ||
|
||
import com.twentythree.peech.security.filter.JWTAuthenticationFilter; | ||
import com.twentythree.peech.security.filter.JWTExceptionFilter; | ||
import com.twentythree.peech.security.handler.JWTAuthAccessDeniedHandler; | ||
import com.twentythree.peech.security.handler.JWTAuthEntryPoint; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; | ||
import org.springframework.security.config.http.SessionCreationPolicy; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
import org.springframework.security.web.access.ExceptionTranslationFilter; | ||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; | ||
|
||
@EnableWebSecurity | ||
@EnableMethodSecurity | ||
@Configuration | ||
public class SecurityConfig { | ||
|
||
private final JWTAuthenticationFilter jwtAuthenticationFilter; | ||
private final JWTAuthEntryPoint JWTAuthEntryPoint; | ||
private final JWTAuthAccessDeniedHandler JWTAuthAccessDeniedHandler; | ||
|
||
public SecurityConfig(JWTAuthenticationFilter jwtAuthenticationFilter, | ||
JWTAuthEntryPoint JWTAuthEntryPoint, | ||
JWTAuthAccessDeniedHandler JWTAuthAccessDeniedHandler | ||
) { | ||
this.jwtAuthenticationFilter = jwtAuthenticationFilter; | ||
this.JWTAuthEntryPoint = JWTAuthEntryPoint; | ||
this.JWTAuthAccessDeniedHandler = JWTAuthAccessDeniedHandler; | ||
} | ||
|
||
// 필터체인 설정 | ||
@Bean | ||
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { | ||
http | ||
.csrf(AbstractHttpConfigurer::disable) | ||
.sessionManagement(s -> s.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) | ||
.authorizeHttpRequests(authorize -> | ||
authorize.requestMatchers("/api/v1.1/auth/reissue").permitAll() | ||
.requestMatchers(HttpMethod.POST,"/api/v1.1/user").permitAll() | ||
.requestMatchers("/swagger-ui/").hasAuthority("ROLE_ADMIN") | ||
.anyRequest().authenticated()) | ||
.addFilterBefore(jwtAuthenticationFilter, ExceptionTranslationFilter.class) | ||
.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class) | ||
.exceptionHandling(e -> e.authenticationEntryPoint(JWTAuthEntryPoint) | ||
.accessDeniedHandler(JWTAuthAccessDeniedHandler)) | ||
|
||
.httpBasic(AbstractHttpConfigurer::disable) | ||
.anonymous(AbstractHttpConfigurer::disable); | ||
|
||
return http.build(); | ||
} | ||
} |
Oops, something went wrong.