Spring Security java配置与XML配置的对应和转换

先放官网文档 http://docs.spring.io/spring-security/site/docs/current/reference/html/   (最值得看!!!)

然后是别人写的spring mvc 以及spring security的java,xml配置对比的文档。http://hanqunfeng.iteye.com/blog/2114980

java配置代码如下

public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private MySavedRequestAwareAuthenticationSuccessHandler authenticationSuccessHandler;

    @Autowired
    private MyAuthenticationFailureHandler myAuthenticationFailureHandler;

  //  @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.formLogin()
                .successHandler(authenticationSuccessHandler)
                .failureHandler(myAuthenticationFailureHandler).and()
                .requiresChannel()
                .antMatchers("/login").requiresSecure()
                .antMatchers("/system1/**").requiresSecure()
                .antMatchers("/system2/**").requiresSecure().and()
                .authorizeRequests()
                .antMatchers("/login", "/myresource/**").permitAll()
                .antMatchers("/system1/**").hasRole("USER1")
                .antMatchers("/system2/**").hasRole("USER2")
                .antMatchers("/system3/**").hasAnyRole("USER1", "USER2")
                .anyRequest().authenticated();//.and()
    }

    ///这个函数主要说明需要认证的用户,密码,以及权限
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(new MyUserDetailService());
    }
}

XML配置代码如下

web.xml如下


    
        org.springframework.web.context.ContextLoaderListener
    
    
        config.MyServerInit
    
    
        contextConfigLocation
        
            classpath:applicationContext.xml,
            classpath:spring-security.xml
        
    
    
        mvc-dispatcher
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:mvc-dispatcher-servlet.xml
        
        1
    

    
        mvc-dispatcher
        /
    

    
        springSecurityFilterChain
        org.springframework.web.filter.DelegatingFilterProxy
    

    
        springSecurityFilterChain
        /*
    


mvc-dipatcher-servlet.xml 如下




    
    

    
    
    
    
    
    


    
    
    
        
        
        
    

applicationContext.xml的内容也是为空




最最关键代码来了。

spring-security.xml代码如下



    
    
    
    


    
        
        
        
        
        
        
        
        
        
    

    
        
    



其中的MyMatcher的代码如下

import org.springframework.security.web.util.matcher.RequestMatcher;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Created by zhangjiasong on 2016/12/14.
 */
public class MyMatcher implements RequestMatcher {
    private Pattern allowedMethods = Pattern
            .compile("^(GET|HEAD|TRACE|OPTIONS)$");

    public boolean matches(HttpServletRequest request) {

        if (execludeUrls != null && execludeUrls.size() > 0) {
            String servletPath = request.getServletPath();
            for (String url : execludeUrls) {
                if (servletPath.contains(url)) {
                    return false;
                }
            }
        }
        return !allowedMethods.matcher(request.getMethod()).matches();
    }

    /**
     * 需要排除的url列表
     */
    private List execludeUrls;

    public List getExecludeUrls() {
        return execludeUrls;
    }

    public void setExecludeUrls(List execludeUrls) {
        this.execludeUrls = execludeUrls;
    }
}

正在的对应就是spring-security.xml  借助官网文档。翻译还是很容易。

你可能感兴趣的:(java)