代码之家  ›  专栏  ›  技术社区  ›  Mahmoud Saleh

在没有用户名/密码的情况下进行编程登录?

  •  -1
  • Mahmoud Saleh  · 技术社区  · 15 年前

    大家好

    public static void autoLogin(User user, HttpServletRequest request,
       AuthenticationManager authenticationManager) {
    
      GrantedAuthority[] grantedAuthorities = new GrantedAuthority[] { new GrantedAuthorityImpl(
        user.getAuthority()) };
    
      UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
        user.getUserName(), user.getPassword(),
        grantedAuthorities);
    
      // generate session if one doesn't exist
      request.getSession();
    
      token.setDetails(new WebAuthenticationDetails(request));
      Authentication authenticatedUser = authenticationManager
        .authenticate(token);
    
      SecurityContextHolder.getContext().setAuthentication(authenticatedUser);
      // setting role to the session
      request
        .getSession()
        .setAttribute(
          HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY,
          SecurityContextHolder.getContext());
    
     }
    

    我想知道是否有可能进行编程登录,但没有用户名或密码身份验证,只是让这个用户进行身份验证。

    2 回复  |  直到 15 年前
        1
  •  1
  •   axtavt    15 年前

    你可以创建自己的子类 Authentication AuthenticationProvider 支持它并配置身份验证管理器以使用此提供程序的。

    身份验证 总是会回来的 true isAuthenticated() 进入之内 SecurityContext ,但这种方法绕过了 AuthenticationManager ,例如, AuthenticationSuccessEvent 不会出版)。

        2
  •  0
  •   Mahmoud Saleh    15 年前

    通过删除这些行

    token.setDetails(new WebAuthenticationDetails(request)); 
    Authentication authenticatedUser = authenticationManager .authenticate(token);
    
    推荐文章