如何在Spring security Java配置中最好地组织URL授权
我有一个声明了大量URL授权的应用程序。大概有200多个。我已经按照spring安全文档的建议,在Java配置中用如何在Spring security Java配置中最好地组织URL授权,spring,security,url,spring-security,authorization,Spring,Security,Url,Spring Security,Authorization,我有一个声明了大量URL授权的应用程序。大概有200多个。我已经按照spring安全文档的建议,在Java配置中用WebSecurity配置适配器配置了这些。也就是说,它们太多了,以至于很难在一个类中维护它们 我已经提出了一个目前正在运行的解决方案,但它的维护水平仍然不理想。我想知道是否有其他人遇到过这个问题,以及您提出了什么解决方案 当前解决方案: 由于URL太多,我将它们拆分为几个类,如下所示: public class UrlConfig implements Customizer<
WebSecurity配置适配器配置了这些。也就是说,它们太多了,以至于很难在一个类中维护它们
我已经提出了一个目前正在运行的解决方案,但它的维护水平仍然不理想。我想知道是否有其他人遇到过这个问题,以及您提出了什么解决方案
当前解决方案:
由于URL太多,我将它们拆分为几个类,如下所示:
public class UrlConfig implements Customizer<ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry> {
@Override
public void customize(ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry requests) {
buildUrlPartition().stream().forEachOrdered(config -> config.configure(requests));
}
SortedSet<UrlConfigSection> buildUrlPartition() {
SortedSet<UrlConfigSection> urlPartition = new TreeSet<>();
urlPartition.add(new CustomUrlsConfig());
urlPartition.add(new DeleteUrlsConfig());
urlPartition.add(new AddUrlsConfig());
urlPartition.add(new EditUrlsConfig());
urlPartition.add(new ListUrlsConfig());
urlPartition.add(new CatchAllUrlsConfig());
return urlPartition;
}
}
维护顺序的抽象类是UrlConfigSection
。顺序被指定为在更具体的something/something/something/code>URL路径之前,不会选择更通用的URLsomething/something/**
路径
public abstract class UrlConfigSection implements Comparable<UrlConfigSection> {
public abstract Integer getOrder();
public abstract void configure(ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry requests);
public int compareTo(UrlConfigSection urlPartition) {
return this.getOrder().compareTo(urlPartition.getOrder());
}
... // implements the equals and hashcode methods. etc..
}
公共抽象类UrlConfigSection实现了可比较的{
公共抽象整数getOrder();
公共抽象无效配置(ExpressionUrlAuthorizationConfigurer.ExpressionInterceptUrlRegistry请求);
public int compareTo(UrlConfigSection urlPartition){
返回这个.getOrder().compareTo(urlPartition.getOrder());
}
…//实现equals和hashcode方法等。。
}
URL类的示例如下所示:
public class CatchAllUrlsConfig extends UrlConfigSection {
@Override
public Integer getOrder() {
return Integer.MAX_VALUE; // this is so that it's always sorted as the last "catch all" urls.
}
@Override
public void configure(ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry requests) {
requests.antMatchers("sample url...").hasAnyAuthority("SOMEROLE");
..... // more declarations
}
}
公共类CatchAllUrlsConfig扩展了urlconfig部分{
@凌驾
公共整数getOrder(){
return Integer.MAX_VALUE;//这样它总是被排序为最后一个“catch all”URL。
}
@凌驾
public void configure(ExpressionUrlAuthorizationConfigurer.ExpressionInterceptUrlRegistry请求){
请求。antMatchers(“示例url…”)。hasAnyAuthority(“SOMEROLE”);
……更多声明
}
}
这是catch-all URL配置,请注意,它将始终作为最后一个URL进行排序。其他URL类将有一个特定的编号
目前这一切都在进行中,但是由于类的顺序,这有一个特定的维护方面。现在我需要跟踪这些数字以及它们在排序集中的位置。有更好的方法吗?有没有人遇到过类似的问题?你是如何解决的
public class CatchAllUrlsConfig extends UrlConfigSection {
@Override
public Integer getOrder() {
return Integer.MAX_VALUE; // this is so that it's always sorted as the last "catch all" urls.
}
@Override
public void configure(ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry requests) {
requests.antMatchers("sample url...").hasAnyAuthority("SOMEROLE");
..... // more declarations
}
}