Framework/SpringBoot

[SpringBoot]스프링 시큐리티란? (Spring Security)

  • -

스프링 시큐리티란?

Spring Security는 자바 기반 웹 애플리케이션을 위한 보안 서비스를 제공합니다.

Spring의 보안을 위한 표준 라이브러리로 사용됩니다.

 

 

스프링 시큐리티가 제공하는 기능은 다음과 같습니다.

  • 인메모리 사용자 하나를 포함하는 AuthenticationManager 빈
    • 사용자 이름은 user이고 암호는 콘솔에 출력됩니다
  • /css 나 /image 같은 공용 정적 리소스 위치를 위해 무시되는 경로, 다른 모든 엔드포인트에 대한 HTTP 기본 인증
  • 스프링의 ApplicationEventPublisher 인터페이스로 게시되는 스프링 이벤트
  • 자동 생성되는 기본 로그인 페이지

 

사용 예시

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/public/**").permitAll()
                .antMatchers("/admin/**").hasRole("ADMIN")
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login").permitAll()
                .and()
            .logout().permitAll();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
                .withUser("user").password("{noop}password").roles("USER")
                .and()
                .withUser("admin").password("{noop}admin").roles("ADMIN");
    }
}

시큐리티 사용 예시입니다.

 

 

 

 

 

 

Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.