相关疑难解决方法(0)

Spring Boot + Spring Security 应用程序中 POST/PUT/DELETE 请求的 403 响应

我在 spring boot rest 应用程序中使用 spring security。获取请求工作正常,但 POST/PUT/DELETE 请求给出“403 Forbidden”。下面是我的代码片段。UI 是 Angular 6

@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserService userService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        CustomAuthorizationFilter customAuthorizationFilter = new CustomAuthorizationFilter(authenticationManager());
        customAuthorizationFilter.setUserService(userService);
        http.cors().and().authorizeRequests().anyRequest().authenticated().and().addFilter(customAuthorizationFilter);
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/v2/api-docs", "/configuration/ui", "/swagger-resources", "/configuration/security",
                "/swagger-ui.html", "/webjars/**");
    }

    @Bean
    public CorsConfigurationSource corsConfigurationSource() {
        final CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOrigins(Arrays.asList("*"));
        configuration.setAllowedMethods(Arrays.asList("HEAD", "GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS")); …
Run Code Online (Sandbox Code Playgroud)

spring spring-mvc spring-security spring-boot

3
推荐指数
1
解决办法
4594
查看次数

标签 统计

spring ×1

spring-boot ×1

spring-mvc ×1

spring-security ×1