在Spring中注入已登录用户
嗨,我希望我的用户通过由spring保护的URL登录。URL将包含用户名和密码。我尝试通过控制器向customAuthenticationManager发送用户名和密码,然后签入CustomAuthenticationProvider并返回UsernamePasswordAuthenticationToken。当我选中isauthenticated标志时,它会显示true,但当我尝试访问安全页面时,它会将我重定向到登录页面。我哪里出错了?这不是最好的方法,但请尝试以下方法:在Spring中注入已登录用户,spring,spring-security,Spring,Spring Security,嗨,我希望我的用户通过由spring保护的URL登录。URL将包含用户名和密码。我尝试通过控制器向customAuthenticationManager发送用户名和密码,然后签入CustomAuthenticationProvider并返回UsernamePasswordAuthenticationToken。当我选中isauthenticated标志时,它会显示true,但当我尝试访问安全页面时,它会将我重定向到登录页面。我哪里出错了?这不是最好的方法,但请尝试以下方法: public voi
public void login(HttpServletRequest request, String userName, String password)
{
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(userName, password);
// Authenticate the user
Authentication authentication = authenticationManager.authenticate(authRequest);
SecurityContext securityContext = SecurityContextHolder.getContext();
securityContext.setAuthentication(authentication);
// Create a new session and add the security context.
HttpSession session = request.getSession(true);
session.setAttribute("SPRING_SECURITY_CONTEXT", securityContext);
}
我想我需要创建会话,但不知道如何创建。在URL中提供用户名/密码并不真正安全。最好使用基本或摘要身份验证,这是由Spring Security提供的开箱即用的支持。如果您真的必须/想要使用您的解决方案,请删除控制器并创建一个过滤器,并将其添加到Spring Security过滤器链中。现在我没有太多选择,但肯定会尝试在将来实现您告诉的方法!!有人不使用会话就可以这样做吗