인터넷에서 spring security를 검색하면 한글 자료는 대부분 xml 설정으로 되어있다.

먼저 web.xml에 DelegateFilterProxy를 설정하는 것부터 나온다.

그리고 나머지 설정을 외우거나 복붙해서 만든다.

반면 java 설정은 자동완성과 클래스를 들여다 보기때문에 공부하는데 더 좋다.

설정도 web.xml에 필터를 등록할 필요 없다.


먼저 필요한 dependency 설정을 한다.
        <dependency>
  		<groupId>org.springframework.security</groupId>
  		<artifactId>spring-security-web</artifactId>
  		<version>5.0.0.RELEASE</version>
  	</dependency>
  	<dependency>
  		<groupId>org.springframework.security</groupId>
  		<artifactId>spring-security-config</artifactId>
  		<version>5.0.0.RELEASE</version>
  	</dependency>
  	<dependency>
  		<groupId>org.springframework.security</groupId>
  		<artifactId>spring-security-taglibs</artifactId>
  		<version>5.0.0.RELEASE</version>
  	</dependency>
자바 설정은 다음 클래스로 시작한다.

@Configuration

@EnableWebSecurity

public class CustomSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter


클래스로 시작하면 된다.


클래스 멤버는 다음과 같다.


아이디와 패스워드 권한을 설정한다. 시작은 인메모리 방식으로~~
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
          .withUser("user1").password("user1Pass")
          .authorities("ROLE_USER");
    }

    }

경로에 권한 설정과 로그인 페이지 설정을 한다. 로그인 페이지 설정을 안하면 브라우저 prompt로 인증을 요구한다. 로그인 페이지 url을 설정안하면 디폴트 로그인 페이지가 나온다.
        @Override
	protected void configure(HttpSecurity http) throws Exception {
		
		logger.info("http: "+http);
		
		http.httpBasic(); 
		http.authorizeRequests().antMatchers("/main/**").hasRole("USER");		
		http.formLogin().loginPage("/login")
			.usernameParameter("username").passwordParameter("password")
			.permitAll()
			.and();
	}
spring-security 시작은 단순하다.


+ Recent posts