gpt4 book ai didi

spring - 在 Spring 中重定向已登录的用户

转载 作者:行者123 更新时间:2023-12-02 21:29:30 24 4
gpt4 key购买 nike

我使用此 Java 配置来通过 Spring Boot + Spring Security 管理 http 路由:

@Override   
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/home").permitAll()
.antMatchers("/public/**").permitAll()
.antMatchers("/signup").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/landing")
.permitAll()
//...
}

现在,我只有一个问题:如何将已登录的用户从/login 重定向到/landing 页面?我是否需要在 Controller 内部或配置类中检查这种情况,如上所示?

最佳答案

您可以在/login"端点中处理该问题。像这样的事情怎么样:

@RequestMapping("/login")
public String login(Principal principal) {
if (principal!=null && ((Authentication)principal).isAuthenticated()) {
return "forward:/landing";
}
return "login";
}

或者我想你可以添加一个过滤器(看起来有点矫枉过正)。

关于spring - 在 Spring 中重定向已登录的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22662994/

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