8:45 spring security 3.1.5 설정 방법 (버전 안 맞춰서 안될때)
spring security 3.1.5 버전 방식입니다.
기존에 implement 하지 않고 클래스를 @configuration 해서
구성 파일로 인식하게 만들고 해당 메서드를 @bean 을 주입시켜 사용하는 방식입니다.
처음 참조할 부분은 여기를 참조 하시면 됩니다. 처음 설정 방법 :: 3.1.5
방법 1
SecurityConfig::SecurityFilterChain 메서드를 수정하기
@Bean
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.csrf(AbstractHttpConfigurer::disable)
.exceptionHandling((handling) ->
handling.authenticationEntryPoint(jwtAuthenticationEntryPoint)
.accessDeniedHandler(jwtAccessDeniedHandler)
)
.headers((header) ->
header.frameOptions(
HeadersConfigurer.FrameOptionsConfig::sameOrigin
)
)
.authorizeHttpRequests((registry) ->
registry.requestMatchers("/api/hello").permitAll()
.requestMatchers("/api/authentication").permitAll()
.requestMatchers("/api/signup").permitAll()
.anyRequest().authenticated()
);
return httpSecurity.build();
}
방법 2
JwtSecurityConfig에 메서드를 하나 추가 한다.
public HttpSecurity configureAndReturn(HttpSecurity httpSecurity) {
httpSecurity.addFilterBefore(
new JwtFilter(tokenProvider),
UsernamePasswordAuthenticationFilter.class
);
return httpSecurity;
}SecurityConfig::SecurityFilterChain 메서드를 수정하기
@Bean
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
return new JwtSecurityConfig(tokenProvider).configureAndReturn(
httpSecurity
.csrf(AbstractHttpConfigurer::disable)
.exceptionHandling((handling) ->
handling.authenticationEntryPoint(jwtAuthenticationEntryPoint)
.accessDeniedHandler(jwtAccessDeniedHandler)
)
.headers((header) ->
header.frameOptions(
HeadersConfigurer.FrameOptionsConfig::sameOrigin
)
)
.authorizeHttpRequests((registry) ->
registry.requestMatchers("/api/hello").permitAll()
.requestMatchers("/api/authentication").permitAll()
.requestMatchers("/api/signup").permitAll()
.anyRequest().authenticated()
)
).build();
}
개인적으로는 방법 1이 깔끔하다고 느낍니다.
回答 2
0
안녕하세요 🙂
Spring Boot 3.4.0 (SNAPSHOT) 버전에 맞춰 샘플 코드를 업데이트했습니다.
아래 링크에서 Java와 Kotlin 버전의 최신 샘플 코드를 확인하실 수 있으니 참고 부탁드립니다.
Java : https://github.com/SilverNine/spring-boot-jwt-tutorial
Kotlin : https://github.com/SilverNine/spring-boot-jwt-tutorial-kotlin
0
코드 설정에서 몇 가지 빠진 부분이 있어 댓글로 달아요.
게시글 수정이 안되네요...
### 방법 1, 2 둘다 넣어주세요 ###
.sessionManagement((session) -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
### 방법 1 에서만 넣어주세요 !체인 메서드 마지막 부분에 추가할 코드! ###
.apply(new JwtSecurityConfig(tokenProvider))
0
안녕하세요! 기존에 depreciated 된 부분을 제외하고, 람다식으로 새롭게 올려주신 방법 잘 찹고하였습니다.
그런데 댓글로 추가 달아주신, .apply(new JwtSecurityConfig(tokenProvider)); 또한 'apply(C)' is "deprecated since version 6.2 and marked for removal" 라고 뜨는데... 혹시 어떻게 고쳐야하는지 알 수 있을까요?
spring boot 3.x 버전 강의도 만들어주시면 안될까요?
0
67
1
3강 secret key 관련해서 질문있습니다
0
65
1
JwtFilter 에 TokenProvider 선언 시 final 키워드 빠진 이유
0
76
1
/api/authenticate 포스트맨 401 에러
0
223
1
Spring boot 3.x버전에서 data.sql 오류 발생할 경우
4
398
1
/api/hello 접근 시 401 나올 때 해결법
2
301
2
소스코드 전체 볼수 있을까요?
0
397
2
머이렇게 안되는게많노 ㅠ
1
757
2
스프링부트 3.x 버전 data.sql 삽입 오류 발생할 경우 해결 방법
6
1350
2
postman 결과가 다릅니다
0
354
2
body값이 비었습니다.
0
399
2
jjwt 버전을 올렸더니 jwt가 유효하지 않다고 합니다
0
3590
1
Refresh Token
0
505
1
유저 권한 설정
0
368
2
setAuthentication
0
496
1
postman에서 오류가 납니다..
0
1766
3
Spring boot 3.1.5 기준 학습 정리 파일 공유
1
1082
4
/api/hello에 접근이 안됩니다 ㅠㅠ
0
1082
2
mysql 설정로 실습시
0
873
2
유효한 JWT 토큰이 없습니다
0
667
2
2:00 에서 저처럼 버전 안 맞춰서 해서 헤매는 분들 이걸로 해보세요.
0
1275
3
JWT String argument cannot be null or empty.
0
2156
2
new User 생성자 오류 발생하는 분들...
6
501
2
requestMatchers 관련 에러 뜨시는 분들 참고하세요
6
2598
3

