🚨 h2 페이지가 작동하지 않습니다 -> 401에러
해결 방법
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests(a -> a
.antMatchers("/", "/error", "/webjars/**","/h2-console/**" ).permitAll()
.anyRequest().authenticated()
)
.exceptionHandling(e -> e
.authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED))
)
.oauth2Login()
.and().logout().logoutSuccessUrl("/").permitAll();
}
.antMatchers 에 h2-console 추가해주면 된다. 인증 면제 처리를 해야 한다.
커스텀한 configure를 사용할 것이기 때문에 super.configure(http)를 사용하면 안된다.
super.configure(http)를 지우면 해결된다.
https://github.com/h2database/h2database/issues/3363
에러로그가 거의 날아가서 ㅠ
org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "Create....."; expected "identifier";
대충 이런 내용이였는데 Table의 이름이 예약어라서 바꿔야하는 경우였다.
나는 @Table(name="UserDB") 를 사용해서 수정했다.
@Getter
@NoArgsConstructor
@Entity
@Table(name="UserDB")
public class User extends BaseTimeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private String email;
@Builder
public User (String name, String email) {
this.name = name;
this.email = email;
}
public User update(String name) {
this.name = name;
return this;
}
}
(1) application.properties에 아래와 같은 줄이 들어있는 지 확인하기
spring.h2.console.enabled=true
(2) Configuration 파일에 아래와 같이 추가 작성하기
(스프링 시큐리티에서 h2-console 막아버렸기 때문에 발생한 에러 -> 무시하도록 설정)
@Override
public void configure(WebSecurity web)throws Exception{
web.ignoring().antMatchers("/h2-console/**");
}