我正在使用Spring Boot Security通过LDAP验证用户的应用程序。默认情况下,此配置会将未经授权的用户重定向到登录页面。我想调整此配置以实现两件事:
我该怎么做?我看到了其他一些问题,这些问题给了我提示,但我仍然无法弄清楚。
这篇文章的最高答案似乎很相关,但是他链接到了这样做的XML方法。我想用Java做到这一点。Spring Security-需要403错误,不能重定向
任何帮助将非常感激!
这是我当前的设置:
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");
}
找到了似乎可行的解决方案(到目前为止,至少)
@Bean
public AuthenticationEntryPoint delegatingEntryPoint() {
final LinkedHashMap<RequestMatcher, AuthenticationEntryPoint> map = new LinkedHashMap();
map.put(new AntPathRequestMatcher("/"), new LoginUrlAuthenticationEntryPoint("/login"));
map.put(new AntPathRequestMatcher("/api_v1/**"), new Http403ForbiddenEntryPoint());
final DelegatingAuthenticationEntryPoint entryPoint = new DelegatingAuthenticationEntryPoint(map);
entryPoint.setDefaultEntryPoint(new LoginUrlAuthenticationEntryPoint("/login"));
return entryPoint;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
//delegates based on url (api vs root)
http.exceptionHandling().authenticationEntryPoint(delegatingEntryPoint());
http
.authorizeRequests()
.antMatchers("/css/**").permitAll()
.anyRequest().authenticated();
http
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/", true)
.permitAll()
.and()
.httpBasic()
.and()
.csrf().disable()
.logout()
.logoutSuccessUrl("/login");
}
希望这可以帮助某人。我知道我花了很长时间才找到答案。:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句