Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Spring中注入已登录用户_Spring_Spring Security - Fatal编程技术网

在Spring中注入已登录用户

在Spring中注入已登录用户,spring,spring-security,Spring,Spring Security,嗨,我希望我的用户通过由spring保护的URL登录。URL将包含用户名和密码。我尝试通过控制器向customAuthenticationManager发送用户名和密码,然后签入CustomAuthenticationProvider并返回UsernamePasswordAuthenticationToken。当我选中isauthenticated标志时,它会显示true,但当我尝试访问安全页面时,它会将我重定向到登录页面。我哪里出错了?这不是最好的方法,但请尝试以下方法: public voi

嗨,我希望我的用户通过由spring保护的URL登录。URL将包含用户名和密码。我尝试通过控制器向customAuthenticationManager发送用户名和密码,然后签入CustomAuthenticationProvider并返回UsernamePasswordAuthenticationToken。当我选中isauthenticated标志时,它会显示true,但当我尝试访问安全页面时,它会将我重定向到登录页面。我哪里出错了?

这不是最好的方法,但请尝试以下方法:

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过滤器链中。现在我没有太多选择,但肯定会尝试在将来实现您告诉的方法!!有人不使用会话就可以这样做吗