Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.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 bsic身份验证后Facelet上的getUserName_Spring_Jsf - Fatal编程技术网

Spring bsic身份验证后Facelet上的getUserName

Spring bsic身份验证后Facelet上的getUserName,spring,jsf,Spring,Jsf,嗨,我可以使用下面的代码获得关于我的bean的用户详细信息。但是我无法在我的页面上获取用户名 @Controller @SessionScoped public class HomeController extends SimpleUrlAuthenticationSuccessHandler { @Override public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse

嗨,我可以使用下面的代码获得关于我的bean的用户详细信息。但是我无法在我的页面上获取用户名

 @Controller
 @SessionScoped
 public class HomeController extends SimpleUrlAuthenticationSuccessHandler  {
 @Override
 public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication auth) throws IOException, ServletException {

UserDetails user = (UserDetails)auth.getPrincipal();
System.out.println("user::::"+user.getUsername());
  request.getSession().setAttribute("user",user.getUsername());

  setDefaultTargetUrl("/Home");


  super.onAuthenticationSuccess(request, response, auth);}}
我可以重定向,现在在另一个控制器中,我尝试通过

FacesContext facesCntxt = FacesContext.getCurrentInstance();
Map sessionMap = facesCntxt.getExternalContext().getSessionMap();
        String userName = (String)sessionMap.get("user");
到目前为止,它工作得很好。但是当我尝试在facelet中使用{param['user']}获取这个细节时,它没有返回任何值


我犯了什么错误?

您的代码有点混乱您有一个AuthenticationSuccessHandler,它是一个@Controller并使用JSF或CDI@SessionScoped?是哪一个

@Controller是Spring MVC中使用的特定原型@Component注释,AuthenticationSuccessHandler是Spring安全接口,而不是Spring MVC控制器。所以我希望有一个@Component,或者简单地用您的Spring安全配置在XML中注册它

@SessionScoped不会做任何事情,除非您在Spring上下文中编写了扩展。所以你应该移除它

如果您正确配置了Spring安全性,那么只需使用getUserPrincipal方法获取主体,就可以从HttpServletRequest获取用户名。请看如何做到这一点。因此,不需要对自己进行攻击,只需使用标准即可