首页 > 解决方案 > 为什么我不能将 POST 登录信息发送到 Spritg 身份验证服务器?

问题描述

我有带有 @EnableResourceServer 的 OAuth 支持的身份验证服务器。 应用配置

安全配置

授权服务器

我可以 GET 到 /login 并填写表格。当我将 POST 发送到 /login 时, POST /login 后出现 401 错误

标签: springauthenticationoauth-2.0

解决方案


你可能会从我的倾斜实验中得到帮助。我也面临同样的问题,添加HttpMethod.POSTantMatchers 帮助了我,你应该.authorizeRequests()在一开始就使用

 http
             .csrf().disable()
             .authorizeRequests().antMatchers("/login","/home","/failure").permitAll()
             .antMatchers(HttpMethod.POST,"/admin/**").hasRole("ADMIN") 
             .antMatchers(HttpMethod.PUT,"/admin/**").hasRole("ADMIN")
             .antMatchers(HttpMethod.GET,"/admin/**").hasRole("ADMIN")
             .antMatchers(HttpMethod.GET,"/user/**").hasAnyRole("ADMIN","USER")
             .antMatchers(HttpMethod.POST,"/user/**").hasAnyRole("ADMIN","USER")
             .anyRequest().authenticated()
             .and()
             .exceptionHandling().accessDeniedPage("/403")
             .and()
             .formLogin()
             .loginPage("/login")
             .loginProcessingUrl("/login")
             .usernameParameter("email")
             .passwordParameter("password")
             .defaultSuccessUrl("/home",true)
             .failureUrl("/failure")
             .and()
             .logout().logoutUrl("/logout").permitAll();

推荐阅读