-
카테고리
-
세부 분야
백엔드
-
해결 여부
미해결
/h2-console 403 에러
23.04.05 00:18 작성 조회수 1.69k
0
@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 이 떠요.
무슨 문제인지 모르겠습니다 ㅠㅠ
답변을 작성해보세요.
1
김재현
2023.08.23
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
return (web) -> web.ignoring()
.requestMatchers(new AntPathRequestMatcher("/h2-console/**"))
.requestMatchers(new AntPathRequestMatcher("/favicon.ico"));
}
이렇게 작성하면 잘 동작합니다!
1
박진호
2023.05.05
이렇게 한번 해보세요.
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();
}
}
siyeon0209
질문자2023.05.07
답변 감사합니다!
.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 문제인 것 같은데 여전히 잘 모르겠습니다.
답변 2