春天 – 成功注册后自动登录

嘿,所有
我想在春季成功注册后进行自动登录
含义:
我有一个受保护的页面,需要登录才能访问它们
我想要注册后跳过登录页面并进行自动登录,以便用户可以看到受保护的页面,让我?
我正在使用spring 3.0,spring security 3.0.2
怎么办?
这可以通过弹簧安全性以下列方式完成(半psuedocode):

import org.springframework.security.web.savedrequest.RequestCache;
import org.springframework.security.web.savedrequest.SavedRequest;

@Controller
public class SignupController
{

    @Autowired
    RequestCache requestCache;

    @Autowired
    protected AuthenticationManager authenticationManager;

    @RequestMapping(value = "/account/signup/", method = RequestMethod.POST)
    public String createNewUser(@ModelAttribute("user") User user, BindingResult result,  HttpServletRequest request, HttpServletResponse response) {
        //After successfully Creating user
        authenticateUserAndSetSession(user, request);

        return "redirect:/home/";
    }

    private void authenticateUserAndSetSession(User user, HttpServletRequest request) {
        String username = user.getUsername();
        String password = user.getPassword();
        UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);

        // generate session if one doesn't exist
        request.getSession();

        token.setDetails(new WebAuthenticationDetails(request));
        Authentication authenticatedUser = authenticationManager.authenticate(token);

        SecurityContextHolder.getContext().setAuthentication(authenticatedUser);
    }
}

更新:仅包含如何在注册后创建会话

http://stackoverflow.com/questions/3813028/auto-login-after-successful-registeration

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:春天 – 成功注册后自动登录