Spring security Spring Security重定向到登录表单,而不是返回状态401
我需要Spring返回unauthorized(状态401),而不是将用户重定向到登录页面。原因是我正在做一个SPA应用程序后端,它已经内置了在收到http状态码401时重定向的机制。我正在使用的SpringBoot版本是1.3.0,但我也尝试过使用1.4.1,但仍然遇到同样的问题 我的安全配置是:Spring security Spring Security重定向到登录表单,而不是返回状态401,spring-security,spring-boot,Spring Security,Spring Boot,我需要Spring返回unauthorized(状态401),而不是将用户重定向到登录页面。原因是我正在做一个SPA应用程序后端,它已经内置了在收到http状态码401时重定向的机制。我正在使用的SpringBoot版本是1.3.0,但我也尝试过使用1.4.1,但仍然遇到同样的问题 我的安全配置是: @Configuration @EnableWebSecurity @EnableRedisHttpSession public class SecurityConfig extends WebSe
@Configuration
@EnableWebSecurity
@EnableRedisHttpSession
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
AuthenticationProvider authenticationProvider;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/loginPath").permitAll()
.anyRequest().authenticated()
.and()
.exceptionHandling().authenticationEntryPoint(
(request, response, authException) -> response.sendError(HttpServletResponse.SC_UNAUTHORIZED))
.and()
.formLogin().loginPage("/loginPath")
.successHandler(loginSuccessHandler()).permitAll()
.and()
.logout().deleteCookies("SESSION").permitAll()
.and()
.authenticationProvider(authenticationProvider)
.csrf().disable()
.httpBasic().disable();
}
...
}
谢谢你的帮助
编辑
存在“/error”的视图控制器映射,用于将用户重定向到分配了最高优先级的登录页面删除后,一切正常。
Spring Security似乎会返回401和/错误作为视图,但由于重定向配置和更高的优先级,它会被覆盖。我在一个空的Spring启动(1.4.1.RELEASE)中收到一个HTTP 401,配置完全相同。请发布完整配置。这是除了成功处理程序实现之外的全部配置…我怀疑这与issueStrange有任何关系。请您在某处(可能是Github)发布一个示例应用程序,演示您的问题,好吗?我没有看到任何错误。请参阅@Arpad扩展
AjaxawareLoginuLauthenticationEntryPoint
类的示例: