gpt4 book ai didi

Spring Security 禁用登录页面/重定向

转载 作者:IT老高 更新时间:2023-10-28 13:49:01 24 4
gpt4 key购买 nike

有没有办法禁用 Spring Security 和登录页面的重定向。我的要求指定登录应该是导航菜单的一部分。

例子:

enter image description here

因此没有专门的登录页面。登录信息需要通过 Ajax 提交。如果发生错误,它应该返回指定错误的 JSON 并使用正确的 HTTP 状态代码。如果身份验证检查它应该返回 200,然后 javascript 可以从那里处理它。

我希望这是有道理的,除非有任何更简单的方法可以使用 Spring Security 来实现这一点。我对 Spring Security 没有太多经验。我认为这必须是一种常见的做法,但我没有找到太多。

当前的 spring 安全配置

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;

@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/", "/public/**").permitAll()
.antMatchers("/about").permitAll()
.anyRequest().fullyAuthenticated()
.and()
.formLogin()
.loginPage("/login")
.failureUrl("/login?error")
.usernameParameter("email")
.permitAll()
.and()
.logout()
.logoutUrl("/logout")
.deleteCookies("remember-me")
.logoutSuccessUrl("/")
.permitAll()
.and()
.rememberMe();
}

@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(userDetailsService)
.passwordEncoder(new BCryptPasswordEncoder());
}

更新:

我尝试使用 HttpBasic(),但不管怎样,它都会要求登录凭据,而且它丑陋的浏览器弹出窗口对最终用户来说是 Not Acceptable 。看来我可能需要扩展 AuthenticationEntryPoint。

归根结底,我需要 Spring 安全性发送回 JSON,说明身份验证成功或失败。

最佳答案

重定向行为来自 SavedRequestAwareAuthenticationSuccessHandler其中is the default success handler .因此,删除重定向的一个简单解决方案是编写您自己的成功处理程序。例如

http.formLogin().successHandler(new AuthenticationSuccessHandler() {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
//do nothing
}
});

关于Spring Security 禁用登录页面/重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31714585/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com