Spring security “forward:” directive can't forward to login form

前端 未结 3 1585
梦谈多话
梦谈多话 2020-12-09 00:35

After a user creates their account, I want to log that user on automatically.

I have standard form logins being handled by Springs filter on /postlogin.

3条回答
  •  眼角桃花
    2020-12-09 00:58

    Spring authentication via REST (Jersey)

    Just to illustrate @David's answer (simplified as much as possible):

    @POST
    @Path("login")
    public Response login(@FormParam("login") String login, @FormParam("pass") String pass)
    {
        if (yourCheck(login, pass))
        {
            List authorities = new ArrayList();
            authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
            Authentication auth = new UsernamePasswordAuthenticationToken(login, pass, authorities);
            SecurityContextHolder.getContext().setAuthentication(auth);
    
            // IMPORTANT: Do not pass any data in the response body
    
            // show empty 200 page (suitable for REST clients)
            return Response.ok().build();
            // or redirect to your home page (for web UI)
            return Response.temporaryRedirect(new URI("/homepage/")).build();
        }
        else
        {
            return Response.status(Status.UNAUTHORIZED).build();
        }
    }
    

提交回复
热议问题