簡體   English   中英

基於Spring-security.xml代碼的配置

[英]Spring-security.xml code based configuration

我正在尋找有關如何對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放入代碼中? 還是我應該忽略它們。

在這些URL中可以找到Spring Security XML模式,而以.xsd結尾的URL是XML模式本身。

您是否嘗試訪問http://www.springframework.org/schema/security 如果是這樣,您將看到一些XSD文件,它們是XML模式。

從XML模式推薦/規范

XML模式表示共享的詞匯表,並允許機器執行人們制定的規則。 它們提供了一種更詳細地定義XML文檔的結構,內容和語義的方法。

XML模式描述XML文檔的結構 在其他作品中,XML模式將幫助您並確保您的XML配置是有效的XML。

由於您現在使用的是基於代碼的配置,因此您可以忽略(不必要),該架構現在是Java代碼,接口,方法等。

暫無
暫無

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

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