2014-09-04 92 views
0

我正在尋找如何執行spring-security.xml文件的基於代碼的配置的示例。這是我用來指導自己的標準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 
    } 
} 

但是,如果你在春天-security.xml文件看到有這些URL基於代碼配置類

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

如何在代碼中放置這些網址?或者我應該忽略它們。

回答

1

這些URL是您查找彈性安全XML模式的地方,以.xsd結尾的URL是XML模式本身。

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

From XML schema recommendation/specification

XML模式表達共同的詞彙和讓機器攜帶由人做出來 規則。它們提供了更詳細地定義XML文檔的結構,內容和語義的手段。

An XML schema describes the structure of an XML document。在其他作品中,XML模式將幫助並保證您的XML配置是有效的XML。

正如你現在正在使用基於代碼的配置,你可以忽略的,是沒有必要的,在模式現在是Java代碼,接口,方法等

相關問題