【问题标题】:Spring-security.xml code based configuration基于 Spring-security.xml 代码的配置
【发布时间】:2014-10-29 16:36:09
【问题描述】:

我正在寻找如何对 spring-security.xml 文件进行基于代码的配置的示例。这是我用来指导自己的标准 spring-security.xml 文件。

<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd">

<http auto-config="true">
    <intercept-url pattern="/admin**" access="ROLE_USER" />
    <form-login 
        login-page="/login" 
        default-target-url="/welcome" 
        authentication-failure-url="/login?error" 
        username-parameter="username"
        password-parameter="password" />
    <logout logout-success-url="/login?logout" />
    <!-- enable csrf protection -->
    <csrf/>
</http>

<authentication-manager>
    <authentication-provider>
      <user-service>
        <user name="mkyong" password="123456" authorities="ROLE_USER" />
      </user-service>
    </authentication-provider>
</authentication-manager>
</beans:beans>

这是一个基于代码的配置类,我也用它来指导自己

@EnableWebSecurity
@Configuration
public class CustomWebSecurityConfigurerAdapter extends
   WebSecurityConfigurerAdapter {
  @Autowired
  public void configureGlobal(AuthenticationManagerBuilder auth) {
    auth
      .inMemoryAuthentication()
        .withUser("user")  // #1
          .password("password")
          .roles("USER")
          .and()
        .withUser("admin") // #2
          .password("password")
          .roles("ADMIN","USER");
  }

  @Override
  public void configure(WebSecurity web) throws Exception {
    web
      .ignoring()
         .antMatchers("/resources/**"); // #3
  }

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http
      .authorizeUrls()
        .antMatchers("/signup","/about").permitAll() // #4
        .antMatchers("/admin/**").hasRole("ADMIN") // #6
        .anyRequest().authenticated() // 7
        .and()
    .formLogin()  // #8
        .loginUrl("/login") // #9
        .permitAll(); // #5
  }
}

但是如果你在 spring-security.xml 文件中看到有这些 URL

http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd"> 

如何将这些 URL 放入代码中?还是我应该忽略它们。

【问题讨论】:

    标签: java xml spring spring-mvc spring-security


    【解决方案1】:

    这些 URL 是您找到 Spring Security XML 架构的地方,而以 .xsd 结尾的 URL 是 XML 架构本身。

    您是否尝试访问http://www.springframework.org/schema/security?如果是这样,您将看到一些 XSD 文件,它们是 XML 模式。

    From XML schema recommendation/specification:

    XML Schemas 表达共享词汇并允许机器携带 出人所定的规矩。它们提供了一种定义 更详细地了解 XML 文档的结构、内容和语义。

    An XML schema describes the structure of an XML document。在其他工作中,XML 模式将帮助并保证您的 XML 配置是有效的 XML。

    由于您现在使用的是基于代码的配置,您可以忽略,不是必需的,schema 现在是 Java 代码、接口、方法等。

    【讨论】:

      猜你喜欢
      • 2012-02-09
      • 1970-01-01
      • 2014-02-16
      • 2014-07-05
      • 2019-04-22
      • 1970-01-01
      • 1970-01-01
      • 2013-03-13
      • 1970-01-01
      相关资源
      最近更新 更多