현의 개발 블로그

Spring Security 401 unauthorized error 본문

대외활동/오류

Spring Security 401 unauthorized error

hyun2371 2023. 6. 2. 20:47

비밀번호 암호화를 위해 Spring Security를 작성

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Bean
    public PasswordEncoder getPasswordEncoder(){
        return new BCryptPasswordEncoder();
    }
 }

 

문제) postman에서 요청을 할 때 401 unauthorized error 뜸

 

401 unauthorized error

클라이언트가 인증되지 않았기 때문에 요청을 정상적으로 처리할 수 없다.

 

해결)

SpringSecurity 설정 시 모든 요청마다 필터가 돌면서 권한을 확인한다.

아래 코드를 작성하여 URI 접근 권한을 모두 허용해줘야 한다.

 

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
	...
    @Override
    protected void configure(HttpSecurity http) throws Exception{
        http.csrf().disable();
        http.authorizeRequests()
                .anyRequest().permitAll();
    }
}

 

 

http.csrf().disable();을 해주지 않으면 403 에러가 뜬다.

 

해당 기능은 CSRF 방지 기능을 비활성화해준다.

CSRF(Cross-Site Request Forgery)란 다른 오리진을 가진 사이트에서 form 요청을 보내는 것이다.

 

예를 들어, 공격자가 로그인한 사용자의 권한을 사용해 다른 페이지에서도 패스워드를 변경할 수 있다.

 

 

http.csrf()를 비활성화한다면, JWT 등의 토큰을 사용하여 보안 취약점을 개선해야 한다.

'대외활동 > 오류' 카테고리의 다른 글

EC2 Remote host identification has changed 에러  (0) 2023.07.16
Comments