简体   繁体   English

当他尝试进入登录页面时,使用Java spring-security重定向已经登录的用户

[英]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.

相关问题 防止spring-security将无效的URL重定向到登录页面 - Preventing spring-security to redirect invalid urls to login page 用户已登录时从CAS登录页面重定向 - redirect from CAS login page when user is already logged in 春季安全错误登录的用户 - spring-security wrong logged user 春季安全性:登录用户页面时获得http状态405 - Spring-security: Got http status 405 when login into user page 当匿名用户尝试经过身份验证的操作时,更改 spring 安全登录重定向 - Change spring security login redirect when anonymous user tries authenticated action 在超时时将用户重定向到登录页面Spring Security - Redirect user to login page on timeout Spring Security Spring-Security:登录后重定向到上一页(尝试了其他SO链接) - Spring-Security : Redirect to previous page after login(Tried other SO links) 如果用户尚未登录Java servlet,如何重定向到jsp登录页面? - How to redirect to jsp login page, if user does not logged in already, Java servlet? 验证过滤器,以防止用户已经登录的情况下进入登录页面 - Authentication filter to prevent user from going to login page if he is already logged in 在Spring-Security中注销用户时从会话中删除用户登录凭据 - Removing user login credentials from session when user logout in spring-security
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM