简体   繁体   English

在Spring Security中将XML配置转换为Java配置以实现全局方法安全

[英]Converting XML configuration to java configuration in spring security for global method security

I am trying to convert xml configuration to java config 我正在尝试将xml配置转换为java config

My XML configuration like this 我的XML配置是这样的

<security:global-method-security secured-annotations="enabled" pre-post-annotations="enabled" access-decision-manager-ref="methodAccessDecisionManager">
        <security:expression-handler ref="methodExpressionHandler"/>
</security:global-method-security>

I am tried to convert with annotation 我尝试使用注释转换

@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)

But I am not getting how I can convert and use with global security method access-decision-manager-ref="methodAccessDecisionManager" and <security:expression-handler ref="methodExpressionHandler"/> 但是我不知道如何转换和使用全局安全性方法access-decision-manager-ref="methodAccessDecisionManager"<security:expression-handler ref="methodExpressionHandler"/>

You could write a custom method security configruation, see Spring Security Reference : 您可以编写自定义方法安全性配置,请参阅Spring Security Reference

5.10.2 GlobalMethodSecurityConfiguration 5.10.2 GlobalMethodSecurityConfiguration

Sometimes you may need to perform operations that are more complicated than are possible with the @EnableGlobalMethodSecurity annotation allow. 有时您可能需要执行比@EnableGlobalMethodSecurity批注允许的操作更复杂的操作。 For these instances, you can extend the GlobalMethodSecurityConfiguration ensuring that the @EnableGlobalMethodSecurity annotation is present on your subclass. 对于这些实例,可以扩展GlobalMethodSecurityConfiguration ,以确保@EnableGlobalMethodSecurity批注出现在子类中。 For example, if you wanted to provide a custom MethodSecurityExpressionHandler , you could use the following configuration: 例如,如果您想提供一个自定义的MethodSecurityExpressionHandler ,则可以使用以下配置:

 @EnableGlobalMethodSecurity(prePostEnabled = true) public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration { @Override protected MethodSecurityExpressionHandler createExpressionHandler() { // ... create and return custom MethodSecurityExpressionHandler ... return expressionHandler; } } 

For additional information about methods that can be overridden, refer to the GlobalMethodSecurityConfiguration Javadoc. 有关可以覆盖的方法的其他信息,请参考GlobalMethodSecurityConfiguration Javadoc。

Your modified code: 您修改的代码:

@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {

    @Autowired
    private AccessDecisionManager accessDecisionManager;

    @Autowired
    private MethodSecurityExpressionHandler methodSecurityExpressionHandler;

    protected MethodSecurityExpressionHandler createExpressionHandler() {
        return methodSecurityExpressionHandler;
    }

    protected AccessDecisionManager accessDecisionManager() {
        return accessDecisionManager;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM