[英]Redirect already logged in user with java spring-security when he tries to enter login page
Java Spring App Java Spring应用
_spring-security _spring-security
__WebSecurityConfigurerAdapter __WebSecurityConfigurerAdapter
(CUSTOM LOGIN PAGE) (自定义登录页面)
user is logged in and tries to go to the login page 用户登录并尝试转到登录页面
How to manage this in extending WebSecurityConfigurerAdapter class and controller? 如何在扩展WebSecurityConfigurerAdapter类和控制器中进行管理?
I've looked at several codes including custom handlers, but I can't seem to get my head around it. 我查看了包括自定义处理程序在内的几个代码,但似乎无法理解。
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/admin").hasRole("ADMIN")
.antMatchers("/login*").permitAll()
.antMatchers("/admin*").denyAll()
.and()
.csrf()
.disable()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/admin")
.failureUrl("/login?error=true")
.and()
.logout().logoutSuccessUrl("/login?logout=true");
http.headers().frameOptions().disable();
}
Expected : user is redirected to the specific url 预期的 :用户被重定向到特定的URL
Actual : user gets to the page and has login form 实际 :用户进入页面并具有登录表格
I'm aware of XML solutions and documentation but I didn't find the necessary information to understand what has to be done here. 我知道XML解决方案和文档,但是没有找到必要的信息来了解必须执行的操作。 Any help would be appreciated.
任何帮助,将不胜感激。
https://bach.mystories.vn/2018/06/12/redirect-logged-in-users-when-accessing-login-page/ https://bach.mystories.vn/2018/06/12/redirect-logged-in-users-when-accessing-login-page/
3 hours of search and I finally got the necessary simple info just after I posted the question so I might as well answer it myself: 经过3个小时的搜索,我在发布问题后终于得到了必要的简单信息,所以我自己也可以回答一下:
@Controller
public class MainController {
@GetMapping("/register")
public String register(Model model,Principal principal) {
if(principal!=null){
return "redirect:/";
}
else{
UserRegistrationDTO userRegistrationDTO = new UserRegistrationDTO();
model.addAttribute(userRegistrationDTO);
return "userregistration";
}
}
}
Adding principal parameter to the controller with redirect inside the function did the job 通过在函数内部使用重定向将主要参数添加到控制器即可完成工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.