2013-12-11 84 views
1

我正在嘗試使用帶註釋的Spring Security java配置。基本上我想使用靜態配置的註解方法的組合。Spring Security和EnableGlobalMethodSecurity

http 
    .authorizeRequests() 
    .anyRequest().authenticated() 

而且

@Controller 
@RequestMapping({"/version", "/*/version"}) 
@PermitAll 
public class VersionController { 
// Implementation 
} 

不久,我想那@PermitAll必須比其他選民的優先級。這種情況可以使用@EnableGlobalMethodSecurity(jsr250Enabled = true)嗎?

回答

0

我改變了我的觀點,並開始思考另一種解決方案。如果在春季安全開始之前我可以知道我的控制器,我可以通過處理它們來配置它。基本上,我創造了我實現的處理器和小出價簡化版本是:

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

據我知道這是不可能的使用方法的安全性,實現自己的目標,因爲請求處理管道如下所示:

  1. 春季安全應用的網絡安全規則
  2. DispatcherServlet選擇hanlder方法
  3. 方法安全性適用於處理程序方法,如果它具有安全註釋

但是,您可以嘗試使用d ifferent方法(未測試):描述here

  • 用它來配置Spring Secuirty
    例如,選擇RequestMappingInfo■對於具有自定義@PermitAll註釋,並創造一切方法都處理方法

    • 提取元數據自定義RequestMatcher將返回true對這些方法的請求。然後配置Spring Security以允許每個人訪問匹配上述匹配器的URL。
  • +0

    隨着春季安全項目負責人,我可以證實這是不可能與現有的基礎設施。在我的選擇中,最好的方法是不使用基於URL的安全性,並創建默認的MethodSecurityMetadataSource,以默認要求用戶進行身份驗證。這確保了所有的安全性都發生在這些方法上。示例請參閱Jsr250MethodSecurityMetadataSource。您可以擴展GlobalMethodSecurityConfiguration並覆蓋methodSecurityMetadataSource以指定您的自定義MethodSecurityMetadataSource。 –

    +0

    @RobWinch我正在考慮添加JSR250可能會有訣竅。這就是我爲什麼報告SEC-2432的原因。 – Cemo

    +0

    @axtavt這是否意味着當spring安全應用web安全規則時,我沒有處理程序信息?我認爲我錯過了一些東西,因爲我已經檢查過投票是否有必要的信息,例如方法註釋。我會很高興,如果你可以擴大一點出價。我正在考慮將Jsr250Voter添加到我的選民決策者中。 – Cemo