Skip to content

Commit

Permalink
Merge pull request #115 from Likelion-YeungNam-Univ/feature-filter
Browse files Browse the repository at this point in the history
feat: filter 수정
  • Loading branch information
iampingu99 authored Aug 6, 2024
2 parents 022559f + 0c5f5c3 commit 60de82e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
20 changes: 18 additions & 2 deletions src/main/java/com/example/holing/base/config/JwtFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
Expand All @@ -19,6 +20,21 @@
public class JwtFilter extends OncePerRequestFilter {
private final UserService userService;
private final JwtProvider jwtProvider;
@Value("${webUrl}")
private String webUrl;

@Override
protected boolean shouldNotFilter(HttpServletRequest request) {
String path = request.getRequestURI();
return path.startsWith("/auth/") ||
path.startsWith("/survey/self-test") ||
path.startsWith("/swagger-resources/") ||
path.startsWith("/swagger-ui/") ||
path.startsWith("/v3/api-docs/") ||
path.startsWith("/webjars/") ||
path.startsWith("/error");
}


@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
Expand All @@ -27,11 +43,11 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
String userId = jwtProvider.getUserId(accessToken);
User user = userService.read(Long.parseLong(userId));

Authentication authentication = new UsernamePasswordAuthenticationToken(userId, null, null); //인증객체 생성
Authentication authentication = new UsernamePasswordAuthenticationToken(user, null, null); //인증객체 생성
SecurityContextHolder.getContext().setAuthentication(authentication); //인증정보 저장
filterChain.doFilter(request, response);
} catch (Exception e) {

response.sendRedirect("/auth/login");
}
}
}
10 changes: 5 additions & 5 deletions src/main/java/com/example/holing/base/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ public CorsConfiguration getCorsConfiguration(HttpServletRequest request) {
}))
.csrf(csrf -> csrf.disable())
.authorizeHttpRequests(request ->
request.requestMatchers("/auth/**").permitAll()
.requestMatchers("/survey/self-test").permitAll()
.requestMatchers("/swagger-resources/**",
request.requestMatchers("/auth/**",
"/survey/self-test",
"/swagger-resources/**",
"/swagger-ui/**",
"/v3/api-docs/**",
"/webjars/**",
"/error").permitAll()
.anyRequest().authenticated())
.addFilterBefore(new JwtFilter(userService, jwtProvider), UsernamePasswordAuthenticationFilter.class)
.exceptionHandling(hp -> hp
.authenticationEntryPoint(customAuthenticationEntryPoint))
// .exceptionHandling(hp -> hp
// .authenticationEntryPoint(customAuthenticationEntryPoint))
.formLogin(Customizer.withDefaults())
.build();
}
Expand Down

0 comments on commit 60de82e

Please sign in to comment.