【发布时间】:2016-10-26 06:36:56
【问题描述】:
我正在使用 Spring Boot Security 对应用程序的 LDAP 用户进行身份验证。默认情况下,此配置会将未经授权的用户重定向到登录页面。我想调整这个配置来实现两件事:
- 将尝试访问单页应用程序(在“/”处)的未经身份验证的用户重定向到登录。这已经适用于默认行为。
- 对于 api 调用(“/api/v1/...”),我想返回 403 错误消息而不是重定向。
我该怎么做?我已经看到其他一些问题给了我提示,但我仍然无法弄清楚。
这篇文章的最佳答案似乎是相关的,但他链接到了一种 XML 方法来执行此操作。我想用Java来做。 Spring Security - need 403 error, not redirect
任何帮助将不胜感激!
这是我目前的设置:
WebSecurityConfig.java
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/css/**").permitAll()
.anyRequest().authenticated();
http
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/", true)
.permitAll()
.and()
.httpBasic()
.and()
.csrf().disable()
.logout()
.logoutSuccessUrl("/login");
}
【问题讨论】:
标签: java authentication spring-security spring-boot ldap