-
Notifications
You must be signed in to change notification settings - Fork 0
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 #114 from Likelion-YeungNam-Univ/feature-security
feat: swagger 접근 수정 및 filter 추가
- Loading branch information
Showing
2 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/main/java/com/example/holing/base/config/JwtFilter.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.example.holing.base.config; | ||
|
||
import com.example.holing.base.jwt.JwtProvider; | ||
import com.example.holing.bounded_context.user.entity.User; | ||
import com.example.holing.bounded_context.user.service.UserService; | ||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import java.io.IOException; | ||
|
||
@RequiredArgsConstructor | ||
public class JwtFilter extends OncePerRequestFilter { | ||
private final UserService userService; | ||
private final JwtProvider jwtProvider; | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { | ||
try { | ||
String accessToken = jwtProvider.getToken(request); | ||
String userId = jwtProvider.getUserId(accessToken); | ||
User user = userService.read(Long.parseLong(userId)); | ||
|
||
Authentication authentication = new UsernamePasswordAuthenticationToken(userId, null, null); //인증객체 생성 | ||
SecurityContextHolder.getContext().setAuthentication(authentication); //인증정보 저장 | ||
filterChain.doFilter(request, response); | ||
} catch (Exception e) { | ||
|
||
} | ||
} | ||
} |
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