繁体   English   中英

成功登录后重定向Spring Boot

[英]Spring Boot redirect after successfull Login

成功登录后,我试图重定向到更深的文件夹结构。

这在我的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();
}

而在/ 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");
    }
}

它不想重定向到/ user / home。 这是/ user / home的请求映射:

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

JUst在您的configure方法中定义

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

登录后将用户重定向到家

更新:

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

重定向用户。 也尝试在配置中设置csrf().disabled()

对不起,我认为我很糟糕!

我将userRoles列表定义为String类型:

private List<String> userRoles;

因此,在访问它时,我只有对象引用的String值,而没有对象本身。 GrantedAuthority的集合只是String值,因此我认为我的身份验证不起作用,因此无法重定向。

更改为List to UserRoles实体对象后:

private List<UserRoles> userRoles;

一切都就位并且可以正常工作。 感谢大家的回答。

暂无
暂无

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

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