在我的 spring 项目中,有很多用户和用户级别。因此需要提供拦截 url 及其访问控制的列表。目前它是用 spring security xml 文件编写的。我怎样才能让它变得简单?
如有任何建议,我们将不胜感激
最佳答案
如果您想将它们放入应用程序属性中,则可以使用 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') ;
}
}
关于java - spring security - 如何提供拦截 URL 的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39934850/