Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.oauth2.client.web.OAuth2LoginAuthenticationFilter;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.cors.CorsConfiguration;
Expand Down Expand Up @@ -69,7 +70,9 @@ public CorsConfiguration getCorsConfiguration(HttpServletRequest request) {

//JWTFilter 추가
http
.addFilterBefore(new JWTFilter(jwtUtil), UsernamePasswordAuthenticationFilter.class);
.addFilterBefore(new JWTFilter(jwtUtil), UsernamePasswordAuthenticationFilter.class)
.addFilterAfter(new JWTFilter(jwtUtil), OAuth2LoginAuthenticationFilter.class);


//oauth2
http
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/org/example/springjwt/jwt/JWTFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public JWTFilter(JWTUtil jwtUtil) {
this.jwtUtil = jwtUtil;
}



@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
//cookie들을 불러온 뒤 Authorization Key에 담긴 쿠키를 찾음
Expand Down Expand Up @@ -79,5 +81,20 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
SecurityContextHolder.getContext().setAuthentication(authToken);

filterChain.doFilter(request, response);

String requestUri = request.getRequestURI();

if (requestUri.matches("^\\/login(?:\\/.*)?$")) {

filterChain.doFilter(request, response);
return;
}
if (requestUri.matches("^\\/oauth2(?:\\/.*)?$")) {

filterChain.doFilter(request, response);
return;
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
String token = jwtUtil.createJwt(username, role, 60*60*60L);

response.addCookie(createCookie("Authorization", token));
response.sendRedirect("http://localhost:3000/");
//response.sendRedirect("http://localhost:3000/");
response.sendRedirect("http://localhost:8080/my");
}

private Cookie createCookie(String key, String value) {
Expand Down