/h2-console 403 에러
@Configuration
public class SecurityConfig {
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
return (web) -> web.ignoring()
.requestMatchers("/h2-console/**", "/favicon.ico");
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.requestMatchers("/api/hello").permitAll()
.anyRequest().authenticated()
.and()
.headers().frameOptions().disable()
.and()
.csrf().ignoringRequestMatchers("/h2-console/**").disable();
return http.build();
}
}
http://localhost:8080/h2-console 와 http://localhost:8080/favicon.ico 는 403 에러가 뜨고,
http://localhost:8080/api/hello 는 200 이 떠요.
무슨 문제인지 모르겠습니다 ㅠㅠ
답변 3
1
이렇게 한번 해보세요.
package me.silvernine.jwttutorial.config;
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.WebSecurityCustomizer;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
//@EnableWebSecurity
@Configuration // 어노테이션 없으면 작동하지 않음
public class SecurityConfig {
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
return (web) -> web.ignoring()
.requestMatchers("/h2-console/**", "/favicon.ico");
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.requestMatchers("/api/hello").permitAll()
.and()
.csrf().ignoringRequestMatchers(new AntPathRequestMatcher("/h2-console/**"))
.and()
.headers().frameOptions().disable()
;
return http.build();
}
}
0
답변 감사합니다!
.authorizeRequests()
.requestMatchers("/api/hello").permitAll()
.anyRequest().authenticated() // 나머지 API 는 모두 인증 필요.anyRequest().authenticated() 가 들어가야 나머지 API들로 접근할 때 인증 제한을 둘 수 있는 걸로 알고 있습니다. 그런데 답변자님의 코드에는 .anyRequest().authenticated() 이 없어서 /h2-console/** 로 접근할 수 있는 것 같아요.
강의 코드에서는 .anyRequest().authenticated() 이 있는대도 /h2-console/** 로 접근할 수 있어서, 다음과 같이 바꿔 보았습니다.
@Configuration // 어노테이션 없으면 작동하지 않음
public class SecurityConfig {
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
return (web) -> web.ignoring()
.requestMatchers("/h2-console/**", "/favicon.ico");
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.requestMatchers("/api/hello").permitAll()
.anyRequest().authenticated() // 추가
.and()
.csrf().ignoringRequestMatchers(new AntPathRequestMatcher("/h2-console/**"))
.and()
.headers().frameOptions().disable()
;
return http.build();
}
}이렇게 변경하고 작동하면 또 /h2-console/** 으로 접근이 불가능 하네요 ㅠㅠ
webSecurityCustomizer 문제인 것 같은데 여전히 잘 모르겠습니다.
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
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
return (web) -> web.ignoring()
.requestMatchers(new AntPathRequestMatcher("/h2-console/**"))
.requestMatchers(new AntPathRequestMatcher("/favicon.ico"));
}이렇게 작성하면 잘 동작합니다!
spring boot 3.x 버전 강의도 만들어주시면 안될까요?
0
70
1
3강 secret key 관련해서 질문있습니다
0
69
1
JwtFilter 에 TokenProvider 선언 시 final 키워드 빠진 이유
0
78
1
/api/authenticate 포스트맨 401 에러
0
225
1
Spring boot 3.x버전에서 data.sql 오류 발생할 경우
4
401
1
/api/hello 접근 시 401 나올 때 해결법
2
306
2
소스코드 전체 볼수 있을까요?
0
399
2
머이렇게 안되는게많노 ㅠ
1
760
2
스프링부트 3.x 버전 data.sql 삽입 오류 발생할 경우 해결 방법
6
1354
2
postman 결과가 다릅니다
0
363
2
body값이 비었습니다.
0
400
2
jjwt 버전을 올렸더니 jwt가 유효하지 않다고 합니다
0
3599
1
Refresh Token
0
507
1
유저 권한 설정
0
370
2
setAuthentication
0
499
1
postman에서 오류가 납니다..
0
1774
3
Spring boot 3.1.5 기준 학습 정리 파일 공유
1
1090
4
/api/hello에 접근이 안됩니다 ㅠㅠ
0
1084
2
mysql 설정로 실습시
0
877
2
유효한 JWT 토큰이 없습니다
0
669
2
8:45 spring security 3.1.5 설정 방법 (버전 안 맞춰서 안될때)
1
2284
2
2:00 에서 저처럼 버전 안 맞춰서 해서 헤매는 분들 이걸로 해보세요.
0
1279
3
JWT String argument cannot be null or empty.
0
2170
2
new User 생성자 오류 발생하는 분들...
6
504
2





