2012-09-06 101 views
13

在XML配置,我可以用security命名空間中啓用安全性的支持,如:春:註釋等價的安全:認證管理和安全的全球法安全

<security:authentication-manager alias="authenticationManager"> 
    <security:authentication-provider ref="authenticator" /> 
</security:authentication-manager> 

<security:global-method-security pre-post-annotations="enabled" /> 

<bean id="authenticator" 
    class="org.springframework.security.authentication.TestingAuthenticationProvider" /> 

我嘗試使用沒有XML的Spring,只有@Configuration類。什麼是上面的XML示例這種配置的簡單Java等價物?

+0

可能重複[基於代碼的Spring安全配置](http://stackoverflow.com/questions/8849162/code-based-spring-安全配置) – Xaerxess

回答

12

編輯:在2013年12月Spring Security 3.2 was releasedJava Configuration was implemented,所以上面的XML大致等同於:

@Configuration 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(final AuthenticationManagerBuilder auth) 
     throws Exception { 
    auth.authenticationProvider(authenticator()); 
    super.configure(auth); 
    } 

    @Bean 
    public AuthenticationProvider authenticator() { 
    return new TestingAuthenticationProvider(); 
    } 

} 

舊答案從2012年:

不幸的是,沒有任何。 Check this answer(發佈半年前的春季安全lead dev):

當前有沒有簡單的方法做的春季安全配置 與Java配置。你必須知道的命名空間做什麼背後 幕後因此很難而且容易出錯。出於這個原因,我 建議與Spring Security的命名空間 配置堅持。

一個選項是向一個非官方項目 - Scalasec - 它提供了基於Scala的配置和在this post on SpringSource blog被描述。同樣,由於項目似乎已被放棄,因此不推薦將其用於生產。我想有一天嘗試這個項目,但目前沒有空閒時間:(

+0

它沒有完全放棄。我已經和它最近修修補補,仍然喜歡用它來命名空間。沒有什麼在那裏,它支持對方法的安全性創建代理,雖然,但這是不是一個真正的大問題因爲你可以輕鬆地將一個最小的XML文件混合到一個Java配置中,並且只涉及一個元素 –

+0

@LukeTaylor你可以更新Github上的存儲庫嗎?我一定要檢查當前版本 – Xaerxess

+0

我沒有'除了更新sbt版本之外,我一直在對代碼本身進行許多修改,我會很快推出這些修改。 –