2014-06-05 2 views
1

모든 사용자가 끝점에 POST 요청을 보낼 수 있지만 GET을 시스템의 등록 된 사용자로만 제한하는 나머지 응용 프로그램 사양이 있습니다. 엔드 포인트의 모든 메소드를 보호하는 것과는 대조적으로 (POST 또는 PUT)와 같은 엔드 포인트의 특정 메소드를 노출하고 (GET 또는 UPDATE)와 같은 다른 메소드를 제한하는 방법이 있습니까?휴식 끝점에 게시 허용 Springboot

답변

4

확실히. HttpSecurity를 ​​정의 할 때 보안 할 HTTP 메소드를 지정할 수 있습니다.

@Configuration 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http. 
      csrf().disable(). 
      sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS). 
      and(). 
      authorizeRequests(). 
      antMatchers(HttpMethod.GET, "/rest/v1/session/login").permitAll(). 
      antMatchers(HttpMethod.POST, "/rest/v1/session/register").permitAll(). 
      antMatchers(HttpMethod.GET, "/rest/v1/session/logout").authenticated(). 
      antMatchers(HttpMethod.GET, "/rest/v1/**").hasAuthority("ADMIN"). 
      antMatchers(HttpMethod.POST, "/rest/v1/**").hasAuthority("USER"). 
      antMatchers(HttpMethod.PATCH, "/rest/v1/**").hasAuthority("USER"). 
      antMatchers(HttpMethod.DELETE, "/rest/v1/**").hasAuthority("USER"). 
      anyRequest().permitAll(); 
    } 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth 
       .inMemoryAuthentication() 
       .withUser('admin').password('secret').roles('ADMIN'); 
    } 

    @Bean 
    @Override 
    AuthenticationManager authenticationManagerBean() throws Exception { 
     return super.authenticationManagerBean() 
    } 
} 
+1

감사합니다. –