Security hasAnyRole

HARIBO·2021년 4월 22일
0

WebSecurityConfigurerAdapter를 상속받아 시큐리티를 설정하던 중 문제가 생겼다.

There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'POST' not supported
org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported

POST 기능이 작동하지 않았는데, HTML파일과 컨트롤러를 확인해 봐도 이상이 없었다.

@Override
protected void configure(HttpSecurity security) throws Exception{
	security.csrf().disable(); //RESTful사용을 위해 비활성화
	security.authorizeRequests()
		.antMatchers(HttpMethod.POST, "/index/request").hasAnyRole("GENERAL") 
		.antMatchers(HttpMethod.POST, "/index/request").hasAnyRole("ADMIN")

/index/request에서 POST기능은 "GENERAL", "ADMIN"역할을 가진 사용자만 사용할 수 있도록 만들 계획이었으나 따로 설정하니 제대로 작동하지 않았다. (GET기능도 저렇게 만들었는데 예외가 발생하지는 않고 권한에 따른 페이지가 표시되지 않았다.)

@Override
protected void configure(HttpSecurity security) throws Exception{
	security.csrf().disable(); //RESTful사용을 위해 비활성화
	security.authorizeRequests()
		.antMatchers(HttpMethod.POST, "/index/request").hasAnyRole("GENERAL", "ADMIN")

이런 식으로 한 줄로 설정하니 잘 작동했다.

0개의 댓글