spring-security-5.0版本的xml基本配置

首先先上全部的xml的配置,代码如下



    SpringSecurity安全配置
    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
            
        
        
        
        
        
        
        
        
        
        
        
    
    
        
            
                /workorder/adminuploadattachment.html
                /face/
            
        
    
    
    
        
    
    
    
        
        
    
    
        
        
        
        
    

    
        
         
    

    
        
    
    
    
    
        
        
        
            
                
                
                    
                
                
                    
                        
                            JSESSIONID
                            remember-me
                        
                    
                
                
            
        
    
    



    
    
        
        
    
    
        
            
                
                    
                    
                    
                
                
                
                    
                
            
        
    
    
    
    
    
        
        
        
    
    
    
    
    
        
            
            
        
        
    
    
    
    
    
    
    
    

    
        
    
    
        
    
    
    
        
        
        
        
        
    
    
    
    
    
    
    
        
        
        
        
        
        
    
    
    
    
    
    
    
        
        
         
        
        
    

    
        
    

    
        
        
    
    
    

首先来说http配置:
1.use-expressions:是否启用intercept-url元素的access属性对Spring EL表达式的支持,但是在配置中并没有使用到。
2.auto-config:是否启用默认配置,在配置中设置为false,基本都采用自定义配置。

  1. entry-point-ref:第三方登录入口,认证不通过则抛出一个异常给ExceptionTranslationFilter,由它进行通过entry-point-ref设置的入口点进行处理,通过此配置可以重定向到其他页面。
    4.authentication-manager-ref:引用的AuthenticationManager,指定身份验证bean,我觉得可以算作核心了。
    5.session过滤器,代码如下:
  

主要功能为当用户session过期用户却继续访问时,系统会跳转至指定路径。
6.access-denied-handler:访问失败才会进AccessDeniedHandler,如果是未登录或者会话超时等,不会触发AccessDeniedHandler,而是会直接跳转到登录页面
7.custom-filter:自定义登录登出记住我过滤器。
8.开启csrf,csrf生成的token是存储在cookies中,防止跨域攻击,所以并不能防止重复提交。
9.为了页面中可以使用iframe,配置header:


            

下面说一下注册的bean
1.csrfSecurityRequestMatcher:这个bean控制了那些路径不需要csrf限制,比如说一些接口、富文本编辑之类。

    private Pattern allowedMethods = Pattern.compile("^(GET|HEAD|TRACE|OPTIONS)$");
    private RegexRequestMatcher unprotectedMatcher = new RegexRequestMatcher("/unprotected", null);
    @Override
    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;
                }
            }
        }
        if (allowedMethods.matcher(request.getMethod()).matches()) {
            return false;
        }
        return !unprotectedMatcher.matches(request);
    }

可以看一下代码,除了指定路径外,还对GET、HEAD、TRACE、OPTIONS免除了csrf过滤,主要是针对post请求进行过滤。

2.accessDeniedHandler:访问失败才会进AccessDeniedHandler,如果是未登录或者会话超时等,不会触发AccessDeniedHandler,而是会直接跳转到登录页面。
3.rememberMeFilter:配置的记住我过滤器,注入身份验证bean以及rememberMeServices。
4.rememberMeServices:注入userDetailsService和token存储。
5.tokenRepository:token处理方式,在配置中选择在数据库中进行存储
6.rememberMeAuthenticationProvider:token的处理类,key要一致,官方说明如下:

This implementation supports the simpler approach described in [Section 18.2, “Simple Hash-Based Token Approach”](https://docs.spring.io/spring-security/site/docs/5.0.0.RELEASE/reference/htmlsingle/#remember-me-hash-token "18.2 Simple Hash-Based Token Approach"). `TokenBasedRememberMeServices` generates a `RememberMeAuthenticationToken`, which is processed by `RememberMeAuthenticationProvider`. A `key` is shared between this authentication provider and the `TokenBasedRememberMeServices`. In addition, `TokenBasedRememberMeServices` requires A UserDetailsService from which it can retrieve the username and password for signature comparison purposes, and generate the `RememberMeAuthenticationToken` to contain the correct `GrantedAuthority` s. Some sort of logout command should be provided by the application that invalidates the cookie if the user requests this. `TokenBasedRememberMeServices` also implements Spring Security’s `LogoutHandler` interface so can be used with `LogoutFilter` to have the cookie cleared automatically.

The beans required in an application context to enable remember-me services are as follows:













Don’t forget to add your `RememberMeServices` implementation to your `UsernamePasswordAuthenticationFilter.setRememberMeServices()` property, include the `RememberMeAuthenticationProvider` in your `AuthenticationManager.setProviders()` list, and add `RememberMeAuthenticationFilter` into your `FilterChainProxy` (typically immediately after your `UsernamePasswordAuthenticationFilter`).

7.logoutFilter:自定义登出过滤器,之所以配置这么复杂是为了配合remember-me功能、以及用户只能在一处登录的功能、csrf过滤用能使用。
8.csrfTokenRepository:csrfToken仓库,用户访问后会生成csrf token存在用户浏览器的cookies中,会对用户每次符合验证规则的请求进行token校验,防止跨域攻击,但是不能防止重复提交。在一个会话中,token是一致的。
9.myFilter:继承AbstractSecurityInterceptor,负责处理HTTP资源的安全性。整个过程需要依赖AuthenticationManager、AccessDecisionManager和FilterInvocationSecurityMetadataSource。
10.authSuccess:自定义的表单登录成功处理器。
11.logoutSuccessHandler:登出成功处理器
12.authenticationManager:用户信息认证管理器。
13.passwordEncoder:根据自定义加密方式进行比对
14.userDetailsService:自定义用户信息类。
15.accessDecisionManager:访问决策器,定义了资源可以被哪些角色访问。
16.securityMetadataSource:资源-角色对应关系。
17.messageSource:security的国际化加载
18.authenticationEntryPoint:第三方登录入口,自定义该bean主要是为了ajax登录。
19.ajaxLoginFilter:ajax登录过滤器。
20.ajaxFailureHandler:ajax登录失败处理器
21.ajaxSuccessHandler:ajax登录成功处理器
下面就类似了,当初定义了三个登录不同的界面,所以写才的如此麻烦。
httpSessionRequestCache:可以理解为缓存控制。

个人觉得spring-security的精髓在于自定义,可以通过自定义来满足几乎所有的业务需求。
在之后的文章中会详细说明各个自定义bean。
以上个人拙见,欢迎大家指教。

你可能感兴趣的:(spring-security-5.0版本的xml基本配置)