首页 > 解决方案 > 防止spring security通过引用访问实体

问题描述

我的问题是 react 沿着不同的路径发出请求以获取 css、js、图像等,而 spring 阻止它们并返回 401。如果您明确指定路径,一切正常。工作设置:

http.cors().and().csrf().disable()
                .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                .authorizeRequests()
                .antMatchers("/admin").hasRole("ADMIN")
                .antMatchers("/").permitAll()
                .antMatchers("/favicon.ico").permitAll()
                .antMatchers("/static/**").permitAll()
                .antMatchers("/manifest.json").permitAll()
                .antMatchers("/logo192.png").permitAll()
                .antMatchers("/api/auth/**").permitAll()
                .antMatchers("/api/test/**").permitAll()
                .anyRequest().authenticated();

我想通过手动输入沿路径的每个可加载对象来解决问题。如果我在spring security设置中设置了值“/**”,那么用户就可以从数据库中接收到所有表的json,例如localhost访问打开users表:localhost:8080/users显示列表json 格式的用户数,以及 Spring MVC Config 的其余表

@Configuration
public class MvcSecurityConfig implements WebMvcConfigurer {
    @Value("${path.frontend}")
    private String frontendPath;
    @Value("${frontendStaticResourcesPathPatterns}")
    private String[] frontendStaticResourcesPathPatterns;
    private static final String BASE_API_PATH = "/";

    public void addResourceHandlers(ResourceHandlerRegistry registry){
        String pathToFrontend = "file:" + this.frontendPath;
        String pathToIndexHTML = pathToFrontend + "/index.html";

        registry
                .addResourceHandler(frontendStaticResourcesPathPatterns)
                .setCachePeriod(0)
                .addResourceLocations(pathToFrontend);

        registry.addResourceHandler("/", "/**")
                .setCachePeriod(0)
                .addResourceLocations(pathToIndexHTML)
                .resourceChain(true)
                .addResolver(new PathResourceResolver() {
                    @Override
                    protected Resource getResource(String resourcePath, Resource location) throws IOException {
                        if (resourcePath.startsWith(BASE_API_PATH) || resourcePath.startsWith(BASE_API_PATH.substring(1))) {
                            return null;
                        }
                        return location.exists() && location.isReadable() ? location : null;
                    }
                });

    }

}

春季安全配置

@EnableWebSecurity
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    UserDetailsServiceImpl userDetailsService;

    @Autowired
    private AuthEntryPointJwt unauthorizedHandler;

    @Bean
    public AuthTokenFilter authenticationJwtTokenFilter() {
        return new AuthTokenFilter();
    }

    @Override
    public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
        authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

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

        http.cors().and().csrf().disable()
                .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                .authorizeRequests()
                .antMatchers("/admin").hasRole("ADMIN")
                .antMatchers("/").permitAll()
                .antMatchers("/api/auth/**").permitAll()
                .antMatchers("/api/test/**").permitAll()
                .anyRequest().authenticated();

        http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);

    }
}

标签: springspring-bootspring-mvcspring-security

解决方案


推荐阅读