- 작성시간 : 2016/02/02 17:35
- 퍼머링크 : geneus.egloos.com/3538311
- 덧글수 : 0
1. 사건의 발단.
- Spring boot로 뭔가를 만들던 중이었다.
- Thymeleaf를 사용해서 front단을 구현했었다.
- front단의 javascript에서 angular JS로 http통신하려하는데, POST가 안됨.. 아니, 이런식으로 접근하는 GET도 안됨.
- post man으로도 시도해봤다. (크롬 브라우저에서 사용가능한 확장도구)
매번 angular js이던 postman이던, post url로 접속할때마다 콘솔창에 나오는 에러메세지.
완전 돌아버리는줄..
메소드를 제공하지 않는다고 한다. 아예 Post URL, Get URL로 접근조차 안되는 상황. 이거, parameter문제도 아니고 아예 접근이 안되는 spring 설정 문제부터 바꿔야 하는것이었다.
2. 원인
Spring 3버젼대부터 들어가는 CSRF Protection이라고 한다.
( 출처 : http://stackoverflow.com/questions/32468054/spring-mvc-request-method-post-not-supported)
3. 해결
이를 해결하기위해서 2번의 출처에서처럼 web.xml을 넣어서 csrf disalbe해주면 된다고 함.
하지만, spring boot에서는 web.xml이 없다. 그건 그냥 spring에서 있었던 환경설정.
이를 위해서 찾아보니 SpringBootServletInitializer 를 사용해서 뭐 작성하면 한다고 하는데, ..아 또 Configuration을 만들어보고 연구해봐 야한다 ㅠㅠ
4. 다른 해결.
나는 login 기능이 필요했기 때문에, WebSecurityConfigurerAdapter 를 사용하고 있었다.
자동으로 상속 구현해야하는 configure에서 csrf().disable()을 추가하면 된다는 은혜로운 글을 봤다.
(출처 : http://stackoverflow.com/questions/21231057/how-to-configure-spring-4-0-with-spring-boot-and-spring-security-openid)
그렇게 추가 한 후, postman으로 연결하니,,어이없을정도로 잘된다.. 흑흑, 나의 일주일 ㅠㅠㅠ
나의 configure메소드는 아래와 같다. 맨 아래에 csrf().disable()추가했을뿐인데 완전 잘됨 ㅠㅠ
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/register", "/login", "/javascripts/**").permitAll()
.anyRequest().authenticated()
.and().formLogin().loginPage("/login")
.failureUrl("/login?error").permitAll()
.and()
.logout().logoutSuccessUrl("/login?logout").permitAll()
.and().csrf()
.and().exceptionHandling().accessDeniedPage("/403")
.and().csrf().disable();
}
태그 : spring, springboot, ide, intellij, thymeleaf, jquery, angularjs, angular, postman, googleextension
덧글