2014-06-05 91 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

像魔術一樣工作謝謝您。 –