【问题标题】:Spring: Annotation equivalent of security:authentication-manager and security:global-method-securitySpring:注解等价于 security:authentication-manager 和 security:global-method-security
【发布时间】:2014-09-07 16:13:04
【问题描述】:

在 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 等价物是什么?

【问题讨论】:

标签: spring spring-security


【解决方案1】:

编辑: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(半年前由 Spring Security lead dev发布):

目前没有简单的方法来进行 Spring Security 配置 使用 Java 配置。你必须知道命名空间在后面做什么 场景使其变得困难且容易出错。为此,我 建议坚持使用 Spring Security 命名空间 配置。

一种选择是为一个非官方项目——Scalasec——提供基于 Scala 的配置,并在this post on SpringSource blog 中进行了描述。同样,不建议将其用于生产,因为项目似乎已被放弃。我想有一天尝试这个项目,但目前没有空闲时间:(

【讨论】:

  • 还没有完全放弃。我最近一直在修改它,并且仍然更喜欢将它用于命名空间。虽然其中没有任何内容支持为方法安全创建代理,但这并不是一个真正的大问题,因为您可以轻松地将最小的 XML 文件混合到 Java 配置中,并且实际上只涉及一个元素。
  • @LukeTaylor 你能更新 Github 上的存储库吗?我肯定想检查当前版本。
  • 除了更新 sbt 构建之外,我没有对代码本身进行太多更改。我会尽快推送这些更改。
  • 到今天为止,有可能吗?谢谢。
  • 是的,今天有可能,看看:Java ConfigurationClass AuthenticationManagerBuilder
猜你喜欢
  • 1970-01-01
  • 2015-05-30
  • 1970-01-01
  • 1970-01-01
  • 2011-07-21
  • 1970-01-01
  • 2016-07-08
  • 1970-01-01
  • 2012-06-18
相关资源
最近更新 更多