简体   繁体   English

成功登录后重定向Spring Boot

[英]Spring Boot redirect after successfull Login

I am trying to redirect to a deeper folder structure after success login. 成功登录后,我试图重定向到更深的文件夹结构。

This inside my WebSecurityConfig: 这在我的WebSecurityConfig中:

    @Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
            .antMatchers("/user/**").hasRole("USER").anyRequest().permitAll()
            .and()
                .formLogin()
                .loginPage("/logon")
                    .usernameParameter("personContactEmail").passwordParameter("personRegPwd")
                .defaultSuccessUrl("/successauth", true) 
            .and()
                .logout()
                .logoutSuccessUrl("/")
            .and()
                .exceptionHandling().accessDeniedPage("/403")
            .and()
            .csrf();
}

And this inside the /successauth Request mapping: 而在/ successauth Request映射内部:

    @RequestMapping("/successauth")
public void afterLogon(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException 
{
    authentication = authenticationFacade.getAuthentication();

    if (authentication.isAuthenticated()) {
        RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
        redirectStrategy.sendRedirect(httpServletRequest, httpServletResponse, "/user/home");
    }
}

It does not want to redirect to /user/home. 它不想重定向到/ user / home。 This is the request mapping for /user/home: 这是/ user / home的请求映射:

    @RequestMapping(value = "/user/home", method = RequestMethod.GET)
public String userHome(HttpServletRequest request)
{
    return "/user/home";
}

JUst define this in your configure method the JUst在您的configure方法中定义

.defaultSuccessUrl("/user/home", true) 

to redirect user to home after login 登录后将用户重定向到家

UPDATE: 更新:

You can define custom AuthenticationSuccessHandler http://javapointers.com/tutorial/spring-custom-authenticationsuccesshandler-example-2/ 您可以定义自定义AuthenticationSuccessHandler http://javapointers.com/tutorial/spring-custom-authenticationsuccesshandler-example-2/

to redirect user. 重定向用户。 Also try to set csrf().disabled() in your config 也尝试在配置中设置csrf().disabled()

Sorry, my bad, I think! 对不起,我认为我很糟糕!

I defined my userRoles List as type String: 我将userRoles列表定义为String类型:

private List<String> userRoles;

Thus when accessing it, I had only the String value of the Object reference and not the Object itself. 因此,在访问它时,我只有对象引用的String值,而没有对象本身。 The Collection of GrantedAuthority was only String values and thus I think my authentication did not work and thus could not redirect. GrantedAuthority的集合只是String值,因此我认为我的身份验证不起作用,因此无法重定向。

After changing to List to UserRoles entity Object: 更改为List to UserRoles实体对象后:

private List<UserRoles> userRoles;

Everything just fell in place and worked perfectly. 一切都就位并且可以正常工作。 Thanks to all for answering. 感谢大家的回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM