-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[bug] spring security 403 error 해결 (#28)
- Loading branch information
Showing
9 changed files
with
232 additions
and
145 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
9 changes: 0 additions & 9 deletions
9
src/main/java/codesquad/fineants/spring/config/CacheConfig.java
This file was deleted.
Oops, something went wrong.
41 changes: 0 additions & 41 deletions
41
src/main/java/codesquad/fineants/spring/config/FilterConfig.java
This file was deleted.
Oops, something went wrong.
71 changes: 71 additions & 0 deletions
71
src/main/java/codesquad/fineants/spring/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,71 @@ | ||
package codesquad.fineants.spring.config; | ||
|
||
import codesquad.fineants.domain.jwt.JwtProvider; | ||
import codesquad.fineants.domain.member.MemberRepository; | ||
import codesquad.fineants.domain.oauth.support.AuthenticationContext; | ||
import codesquad.fineants.spring.api.member.service.OauthMemberRedisService; | ||
import codesquad.fineants.spring.filter.CorsFilter; | ||
import codesquad.fineants.spring.filter.JwtAuthorizationFilter; | ||
import codesquad.fineants.spring.filter.LogoutFilter; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
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.crypto.bcrypt.BCryptPasswordEncoder; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
@EnableWebSecurity | ||
public class SecurityConfig { | ||
private final JwtProvider jwtProvider; | ||
private final AuthenticationContext authenticationContext; | ||
private final ObjectMapper objectMapper; | ||
private final OauthMemberRedisService redisService; | ||
private final MemberRepository memberRepository; | ||
|
||
// 인증이 필요하지 않은 주소 | ||
private final String[] accessUrl = {"/api/auth/login", | ||
"/api/auth/signup", | ||
"/api/auth/**/authUrl", | ||
"/api/auth/**/login", | ||
"/api/auth/refresh/token", | ||
"/api/auth/logout", | ||
"/api/stocks/**"}; | ||
@Bean | ||
public BCryptPasswordEncoder passwordEncoder() { | ||
return new BCryptPasswordEncoder(); | ||
} | ||
|
||
|
||
@Bean | ||
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { | ||
/** | ||
* 기본 설정 | ||
*/ | ||
http.csrf().disable(); | ||
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); // 세션을 사용하지 않는다. | ||
http.formLogin().disable(); // 로그인 UI Form 미사용 | ||
http.httpBasic().disable(); // http basic 방식 미사용 (ID+PW로 인증), bearer token 방식 사용 (JWT 토큰) | ||
|
||
/** | ||
* 필터 추가 | ||
*/ | ||
http.addFilterBefore(new CorsFilter(), UsernamePasswordAuthenticationFilter.class); // cors 필터 | ||
http.addFilterBefore(new JwtAuthorizationFilter(jwtProvider, authenticationContext, objectMapper, redisService,memberRepository), UsernamePasswordAuthenticationFilter.class); // JWT 토큰으로 모든 접근에 대해 인증한다. | ||
http.addFilterBefore(new LogoutFilter(redisService, objectMapper), UsernamePasswordAuthenticationFilter.class); | ||
|
||
/** | ||
* 요청 허용 / 미허용 | ||
*/ | ||
http.authorizeRequests() | ||
.antMatchers(accessUrl).permitAll() | ||
.anyRequest().authenticated(); | ||
|
||
return http.build(); | ||
} | ||
} |
32 changes: 0 additions & 32 deletions
32
src/main/java/codesquad/fineants/spring/config/WebConfig.java
This file was deleted.
Oops, something went wrong.
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
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
23 changes: 23 additions & 0 deletions
23
src/main/java/codesquad/fineants/spring/filter/auth/CustomUserDetailService.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,23 @@ | ||
package codesquad.fineants.spring.filter.auth; | ||
|
||
import codesquad.fineants.domain.member.Member; | ||
import codesquad.fineants.domain.member.MemberRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class CustomUserDetailService implements UserDetailsService { | ||
private final MemberRepository memberRepository; | ||
|
||
@Override | ||
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException { | ||
System.out.println("PrincipalDetailService.loadUserByUsername"); | ||
Member member = memberRepository.findMemberByEmail(email) | ||
.orElseThrow(()-> new RuntimeException("사용자를 찾을 수 없습니다.")); | ||
return new CustomUserDetails(member); | ||
} | ||
} |
Oops, something went wrong.