Spring Legacy Security 설정법-3

김두현·2023년 3월 16일

SpringSecurity

목록 보기
3/3
post-thumbnail

Security Config

Security Config란?

사실상 Security Config에서 Spring Security에 대한 설정은 거의 다 한다고 봐도 무방합니다.
WebSecurityConfigurerAdapter를 상속받아 사용하는 클래스 입니다.
여기서 Security FilterChain이 등록되기도 합니다.
보안 정책, 로그인, 권한 등등 많은 일을 Security에서 합니다.(보안 정책에 관한건 더 자세히 다루겠음)
여기선 로그인, 권한 설정 등만 다룰 것 입니다.
(참고로 본인의 미니 프로젝트를 진행 하면서 소스코드를 기록하는거라 코드가 매우 본인 주관적임)

@Configuration
@EnableWebSecurity(debug = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
	...
}
  • @Configuration : 스프링 설정 클래스임을 나타내는 어노테이션
  • @EnableWebSecurity(debug = true) : WebSecurityConfigurerAdapter를 상속받아 웹 보안을 활성화 하겠다는 어노테이션

HttpSecurity http

Spring Security에서 보안, 권한 등을 설정하는 메소드 입니다.
지금은 권한만 다룰 예정입니다.

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		// TODO Auto-generated method stub
		http.csrf().disable();
		
		http.authorizeRequests()
				.antMatchers("/login").permitAll()
				.antMatchers("/signup").permitAll()
				.antMatchers("/join").permitAll()
				.antMatchers("/main").hasAnyRole("USER","ADMIN")
				.antMatchers("/admin").hasRole("ADMIN")
				.anyRequest().authenticated()
				.and()
				.formLogin()
				.loginPage("/login")
				.defaultSuccessUrl("/main")
		;
	}
  • csrf().disable() 로 csrf라는 보안정책을 꺼두었는데 이부분은 나중에 따로 다루겠습니다.
  • authorizeRequests : url에 대한 권한을 부여하겠다 선언하는 함수(인가)
  • antMatchers : url을 설정하는 함수
  • permitAll : 권한 없이도 앞의 antMatchers의 url을 접속할 수 있게 하는 함수
  • hasAnyRole : 권한을 설정하여 그 권한만이 antMatchers의 url을 접속할 수 있게 하는 함수
    (hasRole로 단일 권한 부여 가능)
  • anyRequest : 특정 url말고 모든 요청을 정의
  • authenticated : 로그인한 모든 요청을 허용
  • formLogin : form 형태의 로그인을 하겠다
  • loginPage : login페이지의 url을 설정하겠다(안하면 security 자체 로그인 페이지 설정)
  • defaultSuccessUrl : 로그인 성공하면 로그인 성공 화면을 출력 하겠다

WebSecurity web

Spring Security에서 web 보안에 관련된 부분 입니다.
본인도 이부분은 이해가 잘 가지않아 관련하여 이해도가 높으신 분은 댓글로 설명 부탁드립니다.

	@Override
	public void configure(WebSecurity web) {
		web.ignoring().antMatchers("/css/**", "/js/**", "/img/**");
	}
  • ignoring : 웹 보안을 허용

AuthenticationManagerBuilder auth

Spring Security에서 로그인 인증을 관리하는 메소드 입니다.
이부분에 대해선 이후 챕터에서 다룰 예정입니다.(로그인과 관련 되어서 설명하기 위해)
기본적인 메소드 형식은 코드만 보고 넘어가 주시면 됩니다.

	@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		// TODO Auto-generated method stub
		auth.authenticationProvider(authenticationProvider());
	}

이렇게 Spring Security에서 없어서는 안되는 SecurityConfig에 대해 알아보았습니다.
궁굼한점이나 틀린부분 있으면 댓글로 욕해주세요.

profile
기억코딩 - 코딩을 대신 기억해드림

0개의 댓글