我改變了我的觀點,並開始思考另一種解決方案。如果在春季安全開始之前我可以知道我的控制器,我可以通過處理它們來配置它。基本上,我創造了我實現的處理器和小出價簡化版本是:
@Component
public class PermitAllProcessor {
private static final Logger logger = LoggerFactory.getLogger(PermitAllProcessor.class);
@Autowired
AbstractHandlerMethodMapping abstractHandlerMethodMapping;
public String[] process() {
Map<RequestMappingInfo, HandlerMethod> handlerMethods = abstractHandlerMethodMapping.getHandlerMethods();
ArrayList<String> urls = Lists.newArrayList();
for(Map.Entry<RequestMappingInfo, HandlerMethod> entry : handlerMethods.entrySet()) {
PermitAll annotation = getPermitAll(entry.getValue());
if(annotation != null) {
Set<String> patterns = entry.getKey().getPatternsCondition().getPatterns();
urls.addAll(patterns);
}
}
logger.info("Permitted urls as follows: {}",
StringUtils.collectionToDelimitedString(urls, ",", "[", "]"));
return urls.toArray(new String[urls.size()]);
}
private PermitAll getPermitAll(HandlerMethod value) {
PermitAll annotation = value.getMethodAnnotation(PermitAll.class);
if(annotation == null){
annotation = value.getBeanType().getAnnotation(PermitAll.class);
}
return annotation;
}
}
基本上
.authorizeRequests()
.antMatchers(permitAllProcessor.process()).permitAll()
.anyRequest().authenticated();
隨着春季安全項目負責人,我可以證實這是不可能與現有的基礎設施。在我的選擇中,最好的方法是不使用基於URL的安全性,並創建默認的MethodSecurityMetadataSource,以默認要求用戶進行身份驗證。這確保了所有的安全性都發生在這些方法上。示例請參閱Jsr250MethodSecurityMetadataSource。您可以擴展GlobalMethodSecurityConfiguration並覆蓋methodSecurityMetadataSource以指定您的自定義MethodSecurityMetadataSource。 –
@RobWinch我正在考慮添加JSR250可能會有訣竅。這就是我爲什麼報告SEC-2432的原因。 – Cemo
@axtavt這是否意味着當spring安全應用web安全規則時,我沒有處理程序信息?我認爲我錯過了一些東西,因爲我已經檢查過投票是否有必要的信息,例如方法註釋。我會很高興,如果你可以擴大一點出價。我正在考慮將Jsr250Voter添加到我的選民決策者中。 – Cemo