关于spring security的配置权限的configure方法

关于spring security的拦截机制中的configure方法

当部署一个项目需要使用spring security时,需要自行编写一个config类继承WebSecurityConfigurerAdapter,在其中配置项目中资源的访问权限。

1.public修饰的configure方法

源码:

	/**
	 * Override this method to configure {@link WebSecurity}. For example, if you wish to
	 * ignore certain requests.
	 */
	public void configure(WebSecurity web) throws Exception {
	}

如源码所示,你可以在这个方法里面去配置一些你项目不需要访问权限的资源请求。
这个public关键词修饰的configure方法中,应该是配置静态资源,因为在这个方法中配置的请求,spring security检测到你配置的请求属于此方法,请求将会忽略,不会进入spring security的框架进行解析和处理(即使你在原有的过滤器链上添加了crsf、xss等漏洞处理器)。
如果你将非静态资源路径配置到了此方法中,那么他人极有可能通过此链接来入侵你的项目。

重写:

@Override
    public void configure(WebSecurity security) {
    	 security.ignoring()
        .antMatchers("/**/*.html")
        .antMatchers("/**/*.htm")
        .antMatchers("/**/*.js")
        .antMatchers("/**/*.png")
        .antMatchers("/**/*.jpg")
        .antMatchers("/favicon.ico")
        .antMatchers("/**/*.css")
        .antMatchers("/images/**");
        }

2.protected修饰的configure方法

源码:

/**
	 * Override this method to configure the {@link HttpSecurity}. Typically subclasses
	 * should not invoke this method by calling super as it may override their
	 * configuration. The default configuration is:
	 *
	 * <pre>
	 * http.authorizeRequests().anyRequest().authenticated().and().formLogin().and().httpBasic();
	 * </pre>
	 *
	 * @param http the {@link HttpSecurity} to modify
	 * @throws Exception if an error occurs
	 */
	// @formatter:off
	protected void configure(HttpSecurity http) throws Exception {
		logger.debug("Using default configure(HttpSecurity). If subclassed this will potentially override subclass configure(HttpSecurity).");

		http
			.authorizeRequests()
				.anyRequest().authenticated()
				.and()
			.formLogin().and()
			.httpBasic();
	}
	// @formatter:on

源码中说明了如果没有重写此configure方法,那么将默认使用父类中的设置。
此方法中主要配置你的登录和权限控制、以及配置过滤器链。
在此方法中亦可配置资源请求的权限忽略(即不需要登录也可访问,但会经过spring security的过滤器链)。

重写:

	@Override
    protected void configure(HttpSecurity http) throws Exception {
        // 跨域攻击防护
    	http.cors().and().csrf();
        // 同源打开iframe
        http.headers().frameOptions().sameOrigin();
        //配置登录
        http.addFilterBefore(customAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class)
                .formLogin().loginPage("/authentication/require")
                .loginProcessingUrl("/authentication/login")
                .successHandler(authenticationSuccessHandler)
                .failureHandler(authenticationFailureHandler)
                .and()
                .logout().logoutUrl("/quit").logoutSuccessUrl("/").invalidateHttpSession(true)
                .and()
                .authorizeRequests()
                .antMatchers("/test/**").permitAll() //配置需要忽略的请求
                .antMatchers("/me").permitAll()
                .antMatchers("/authentication/require").permitAll()
                .anyRequest().authenticated();
         //xss防护过滤器
        http.addFilterBefore(xssFilter, UsernamePasswordAuthenticationFilter.class);
        //可信任站点过滤器
        http.addFilterBefore(refererFilter, UsernamePasswordAuthenticationFilter.class);
         //crsf防护过滤器
        http.addFilterBefore(roleFilter, UsernamePasswordAuthenticationFilter.class);
    }

上述中的配置表明了 /test/开头的请求,会进入spring security的crsf校验以及自定义添加的相关过滤器。

综上所述:

public修饰的configure和protected修饰的configure方法

共同点:都可以配置后台权限可以忽略的请求
区别:
1、public修饰的configure:
经常用来处理静态资源的忽略,因为此方法中配置的请求不会经过spring security的框架及自定义添加的过滤器链。容易被攻击。
2、protected修饰的configure:
用于处理动态资源的忽略,因为此方法中配置的请求将经过spring security的框架及自定义添加的过滤器链,只是不经过登录验证。可以保证动态资源的请求不容易被他人攻击,更加安全。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值