簡體   English   中英

Spring:通過java配置在Controller層中啟用全局方法安全性

[英]Spring: Enable global method security in Controller layer by java config

我正在嘗試將我的xml servlet配置遷移到java配置。

以下配置是我的servlet配置 ,它在Controller層上啟用自定義安全注釋。

<security:global-method-security pre-post-annotations="enabled">
    <security:expression-handler ref="expressionHandler"/>
</security:global-method-security>

<bean id="expressionHandler" class="yyy.MyMethodSecurityExpressionHandler" />

我還有一個工作的spring security xml配置,這是為了被java配置替換,但現在不是。 這里有一些我的安全配置:

<bean id="authenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
        <property name="userDetailsService" ref="userDetailsService" />
    </bean>

<bean id="authenticationManager" class="org.springframework.security.authentication.ProviderManager">
    <constructor-arg>
         <ref bean="authenticationProvider"/>
    </constructor-arg>
</bean>

<security:authentication-manager>
    <security:authentication-provider user-service-ref="userDetailsService" />
</security:authentication-manager>

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

我想開始遷移我的servlet配置 ,在Controller層中啟用安全性@PreAuthorize@PostAuthorize標記。

我發現了這個注釋: @EnableGlobalMethodSecurity(prePostEnabled=true) ,但把它放在我的servlet配置上:

@Configuration
@ComponentScan(basePackages= {
        "....."         
})
@EnableGlobalMethodSecurity(prePostEnabled=true)

public class WebappServletConfig extends WebMvcConfigurationSupport {

我得到這個例外:

java.lang.IllegalArgumentException: Expecting to only find a single bean for type interface org.springframework.security.authentication.AuthenticationManager, but found []

而且我不知道如何設置我的自定義表達式處理程序!

有人提示嗎? 謝謝

更新(更新后的問題)

看來你遇到了SEC-2479 有幾種方法可以解決這個問題。 最簡單的方法是使用@Autowired的結果作為AuthenticationManager。 為此,您必須擴展GlobalMethodSecurityConfiguration並覆蓋authenticationManager方法。

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
    @Autowired
    private AuthenticationManager am;

    @Override
    protected AuthenticationManager authenticationManager() {
        return am;
    }
    @Override
    protected MethodSecurityExpressionHandler createExpressionHandler() {
        // ... create and return custom MethodSecurityExpressionHandler ...
        return expressionHander;
    }
}

原始答案

您需要配置某種身份驗證。 所以你需要有以下內容:

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth
        .inMemoryAuthentication()
            .withUser("user").password("password").roles("USER");
}

如果您不使用基於Web的安全性,則該引用提供了有關如何配置方法安全性表達式處理程序的示例

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
                .withUser("user").password("password").roles("USER");
    }
    @Override
    protected MethodSecurityExpressionHandler createExpressionHandler() {
        // ... create and return custom MethodSecurityExpressionHandler ...
        return expressionHander;
    }
}

如果您只想要一個自定義方法表達式處理程序來提供權限評估程序,那么您只需要創建一個PermissionEvaluator bean,如下所示:

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MethodSecurityConfig {
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
                .withUser("user").password("password").roles("USER");
    }
    @Bean
    public PermissionEvaluator permissionEvaluator() {
        // ... create and return custom PermissionEvaluator ...
        return permissionEvaluator;
    }
}

首先,你需要一個單獨的Configuration類

@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {...

你需要定義的地方

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(userDetailsService);
}

這應該有助於你的例外。

我不知道如何配置expressionHandler,但似乎你可以在configure(WebSecurity web)中調整它

@Override
public void configure(WebSecurity web) throws Exception {
    web.expressionHandler().....
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM