2014-09-04 90 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代码,接口,方法等

相关问题