# 배경상황
Spring in Action 제5판 실습을 따라하던 중 131페이지에서 막혔다. 그 이유는 src/main/resource 경로의 data.sql과 schema.sql 파일의 쿼리문이 자동으로 읽히지 않았기 때문이다.
# 해결방법
하기와 같이 application.properties 파일과 SecurityConfig.java 파일을 변경한다.
- application.properties 파일
#h2 console 활성화 및 경로 설정
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
#h2 db 설정
spring.datasource.url=jdbc:h2:~test;
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
#hibernate 설정
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.hibernate.ddl-auto=create
- SecurityConfig.java 파일
package tacos.security;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
DataSource dataSource;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/design", "/orders")
.access("hasRole('ROLE_USER')")
.antMatchers("/", "/**").access("permitAll")
.antMatchers("/h2-console/**").permitAll() // 추가
.and()
.csrf() // 추가
.ignoringAntMatchers("/h2-console/**").disable() // 추가
.httpBasic();
/*.and()
.httpBasic();*/
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception{
/*auth.inMemoryAuthentication()
.withUser("user1")
.password("{noop}password1")
.authorities("ROLE_USER")
.and()
.withUser("user2")
.password("{noop}password2")
.authorities("ROLE_USER");
*/
auth
.jdbcAuthentication()
.dataSource(dataSource)
.usersByUsernameQuery("select username, password, enabled from users" +
"where username=?")
.authoritiesByUsernameQuery("select username, authority from authorities " +
"where username=?")
.passwordEncoder(new NoEncodingPasswordEncoder());
}
// Security 무시하기
public void configure(WebSecurity web)throws Exception{
web.ignoring().antMatchers("/h2-console/**");
}
}
그리고 http://localhost:8080/h2-console 접속시에 JDBC URL을 "jdbc:h2:~/test" 로 접속하면 된다.
# 결론
Spring boot에 디비를 연동해서 쓸 때는 application.properties 파일을 잘 설정해 주어야 하며, Spring Security의 접근 권한도 잘 설정해야 한다.
# 참조
'개발 오류' 카테고리의 다른 글
[IntelliJ] SpringBootApplication 에러 (0) | 2023.04.17 |
---|---|
Error - Can't create a java class file by intelliJ (0) | 2023.03.31 |
[Spring in Action] h2 database 접근 오류 (0) | 2021.11.07 |
Web server failed to start. Port 8080 was already in use. (0) | 2021.08.20 |
[STS4] import javax.validation 오류 (0) | 2021.08.20 |