본문 바로가기

Web/SpringSecurity

SpringSecurity

dependency 

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-security</artifactId>
</dependency>

 

기본적인 login, logout은 spring security에서 제공해준다.

default value

username: user

password: WAS 띄울 때 콘솔에 출력된다. (application 띄울 때마다 바뀜)

로그엔 보안 관련 정보가 남으면 안 된다.

 

configuration

@Configuration, @EnableWebSecurity 어노테이션을 사용하고, WebSecurityConfigurerAdapter를 상속한다.

@Configuration
@EnableWebSecurity
public class SpringConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .mvcMatchers("/", "info").permitAll()
                .mvcMatchers("/admin").hasRole("ADMIN")
                .anyRequest().authenticated()      // 기타 등등에 대한 요청은 인증만 되면 요청할 수 있다.
                .and()
            .formLogin()
                .and()
            .httpBasic();
    }
}