Package com.wizriver.config

Source Code of com.wizriver.config.SpringSecurityUtils

/**
* Copyright (c) 2005-2009 springside.org.cn
*
* Licensed under the Apache License, Version 2.0 (the "License");
*
* $Id: SpringSecurityUtils.java 763 2009-12-27 18:36:21Z calvinxiu $
*/
package com.wizriver.config;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import com.wizriver.entity.beans.VgUser;

/**
* SpringSecurity的工具类.
*
* @author calvin
*/
public class SpringSecurityUtils {

  /**
   * 取得当前用户的登录名, 如果当前用户未登录则返回空字符串.
   */
  public static String getCurrentUserName() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth == null) {
      return "";
    }
    return auth.getName();
  }
 
  public static VgUser getUser(){
    Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal == null) {
            return null;
        }
        VgUser vu = null ;
        try {
             vu = (VgUser) principal;
        } catch (Exception e) {
          //do nothing
        }
        return vu;
  }

  /**
   * 取得当前用户, 返回值为SpringSecurity的User类及其子类, 如果当前用户未登录则返回null.
   * @throws Exception
   */
//  @SuppressWarnings("unchecked")
//  public static <T extends User> T getCurrentUser() {
//    Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//    if (principal == null) {
//      return null;
//    }
//    return (T) principal;
//  }
 
     public static VgUser getCurrentVgUser() throws Exception {
          Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
          if (principal == null) {
              return null;
          }
          VgUser vu = null ;
          try {
                 vu = (VgUser) principal;
            } catch (Exception e) {
//                System.out.println("登陆超时,请重新登录!");
                HttpServletRequest request = ServletActionContext.getRequest();
                String path = request.getContextPath();
                String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
//              ServletActionContext.getResponse().sendRedirect("/j_spring_security_check");
                ServletActionContext.getResponse().sendRedirect(basePath+"vgadmin/vegagaAdmin.action");
            }
          return vu;
      }
}
TOP

Related Classes of com.wizriver.config.SpringSecurityUtils

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.