弹簧安全性 - 如何提供拦截URL的列表

时间:2016-10-08 16:44:45

标签: java xml spring model-view-controller spring-security

在我的春季项目中拥有大量用户和用户级别。因此需要提供拦截网址列表及其访问控制。目前它是用spring security xml文件编写的。我怎么能让它变得简单呢?

任何建议都将受到赞赏

1 个答案:

答案 0 :(得分:1)

如果您想将它们放在应用程序属性中,那么可以使用WebSecurityConfigurerAdapter

application.properties

中的

url1 = /admin
url2 = /reservation/**

===

@Configuration
@EnableWebSecurity
@PropertySource("classpath:application.properties")
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Value("${url1}")
private String url1;

@Value("${url1}")
private String url2;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
      //..
    }

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

      http.authorizeRequests()
        .antMatchers(url1).access("hasRole('ROLE_ADMIN')")
        .antMatchers(url2).access("hasRole('ROLE_USER') ;

    }
}