2016-07-07 5 views
2

Spring REST(web.xmlなし)でアプリケーションを構築しています。 RESTの呼び出しはうまくいきますが、私はweb.xmlを使って追加するのは簡単ですが、Spring 4をweb.xmlなしで使用しているので、私はweb.xml部分Java構成を使用します。Spring RESTアプリケーションにおけるセキュリティ制約のJava構成

マイweb.xmlの

<security-role> 
    <role-name>all</role-name> 
</security-role> 
<security-constraint> 
    <web-resource-collection> 
     <web-resource-name>test</web-resource-name> 
     <url-pattern>/*</url-pattern> 
    </web-resource-collection> 
    <auth-constraint> 
     <role-name>all</role-name> 
    </auth-constraint> 
</security-constraint> 


私は、Java構成を通じて、こののweb.xmlを構成する上で助けを必要としています。おそらく、これはSpring Securityを通じて追加することができますが、その方法はわかりません。

答えて

4

これは、@ Configurationを使用してカスタム制約でセキュリティを実装し、WebSecurityConfigurerAdapterクラスのconfigureメソッドをオーバーライドする方法です。

@Configuration 
    public class SecurityConfiguration extends WebSecurityConfigurerAdapter { 


     @Autowired 
     DataSource datasource; 
     Logger logger = LoggerFactory.getLogger(getClass()); 

     @Override 
     protected void configure(HttpSecurity http) throws Exception { 

      http.httpBasic().and().authorizeRequests().antMatchers("/public/**") 
        .permitAll().antMatchers("/admin/**").hasAuthority("admin") 
        .antMatchers("/user/**").hasAuthority("user") 
        .and() 
        .logout() 
        // Logout requires form submit. Bypassing the same. 
        .logoutRequestMatcher(new AntPathRequestMatcher("/logout")) 
        .logoutSuccessUrl("/index.html").and() 
        .addFilterAfter(new CsrfHeaderFilter(), CsrfFilter.class) 
        .csrf().disable(); 

     } 
} 
関連する問題