• WebSecurityConfig的重要性


    当你发布的应用不能直接请求时,多数原因是因为受到WebSecurityConfig.java的拦截。

    可以通过修改此文件对某些请求进行放行。

    例:

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.security.config.annotation.web.builders.HttpSecurity;
    import org.springframework.security.config.annotation.web.builders.WebSecurity;
    import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
    import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
    import org.springframework.security.web.firewall.HttpFirewall;
    import org.springframework.security.web.firewall.StrictHttpFirewall;
    
    @Configuration
    @EnableWebSecurity
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    
        private final Logger log = LoggerFactory.getLogger(WebSecurityConfig.class);
    
        private final WebProperties webProperties;
    
        public WebSecurityConfig(WebProperties webProperties) {
        	this.webProperties = webProperties;
        }
    
    	@Override
    	public void configure(WebSecurity web) throws Exception {
    		web.ignoring().antMatchers("/video/**").antMatchers("/resources/**").antMatchers("/publics/**")
    				.antMatchers("/health-check").antMatchers("/**");
    		web.httpFirewall(allowUrlEncodedSlashHttpFirewall());
    	}
    
    	@Override
    	protected void configure(HttpSecurity http) throws Exception {
    		http.authorizeRequests().antMatchers("/admin/**").hasRole("ADMIN").antMatchers("/user/**").hasRole("USER")
    				.anyRequest().authenticated();
    
    		//http.cors().configurationSource(request -> new CorsConfiguration().applyPermitDefaultValues());
    	}
    	
    	@Bean
    	public HttpFirewall allowUrlEncodedSlashHttpFirewall() {
    	    StrictHttpFirewall firewall = new StrictHttpFirewall();
    	    firewall.setAllowUrlEncodedSlash(true);    
    	    return firewall;
    	}
    
    //
    //    @Bean
    //    public CorsFilter corsFilter() {
    //
    //        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    //        CorsConfiguration config = webProperties.getCors();
    //
    //        if (config.getAllowedOrigins() != null && !config.getAllowedOrigins().isEmpty()) {
    //
    //        	log.debug("Registering CORS filter");
    //
    //            source.registerCorsConfiguration("/api/**", config);
    //            source.registerCorsConfiguration("/management/**", config);
    //            source.registerCorsConfiguration("/v2/api-docs", config);
    //        }
    //
    //        return new CorsFilter(source);
    //    }
    
    }
    

      

  • 相关阅读:
    Maven学习笔记
    [学习笔记] 网络流
    [Contest on 2021.11.3] 女子口阿
    [杂题合集] 25 岁小伙突然没了心跳,他的习惯很多年轻人都有!
    CSP 2021 提高组游记
    [题目小结] 可持久化数据结构
    [学习笔记] 无向图和有向图的连通分量
    [Contest on 2021.10.17] HustOJ 就是个 **
    [Contest on 2021.10.15] 细思极恐
    妖怪寺外,灯火通明
  • 原文地址:https://www.cnblogs.com/wzihan/p/14108516.html
Copyright © 2020-2023  润新知