2016-07-07 2 views
2

Я создаю приложение с Spring REST (без web.xml). REST работают нормально, но мне нужно добавить несколько ограничений безопасности, которые легко добавить через web.xml, но поскольку я использую Spring 4 без web.xml, так что мне нужна помощь в добавлении web.xml part через конфигурацию Java.Конфигурация Java для ограничений безопасности в приложении Spring REST

Мой 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> 


Мне нужна помощь в настройке этого web.xml через конфигурацию Java. Вероятно, это можно добавить через Spring Security, но не знаю, как это сделать.

ответ

4

Как вы можете реализовать безопасность с вашими пользовательскими ограничениями, используя @ Configuration и переопределив метод configure класса WebSecurityConfigurerAdapter.

@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(); 

     } 
} 
Смежные вопросы