當前位置: 首頁>>代碼示例>>Java>>正文


Java Subject.getPrincipal方法代碼示例

本文整理匯總了Java中org.apache.shiro.subject.Subject.getPrincipal方法的典型用法代碼示例。如果您正苦於以下問題:Java Subject.getPrincipal方法的具體用法?Java Subject.getPrincipal怎麽用?Java Subject.getPrincipal使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.apache.shiro.subject.Subject的用法示例。


在下文中一共展示了Subject.getPrincipal方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: authorize

import org.apache.shiro.subject.Subject; //導入方法依賴的package包/類
public void authorize(ResourcePermission context) {
  Subject currentUser = getSubject();
  if (currentUser == null) {
    return;
  }

  if (context == null) {
    return;
  }

  if (context.getResource() == Resource.NULL && context.getOperation() == Operation.NULL) {
    return;
  }

  try {
    currentUser.checkPermission(context);
  } catch (ShiroException e) {
    String msg = currentUser.getPrincipal() + " not authorized for " + context;
    logger.info(msg);
    throw new NotAuthorizedException(msg, e);
  }
}
 
開發者ID:ampool,項目名稱:monarch,代碼行數:23,代碼來源:IntegratedSecurityService.java

示例2: login

import org.apache.shiro.subject.Subject; //導入方法依賴的package包/類
@RequestMapping(value = "/tlogin", method = RequestMethod.POST)
public String login(String username, String password, HttpServletRequest request) {

    //String validateCode = (String) ServletActionContext.getRequest().getSession().getAttribute("key");
    // if (StringUtils.isNotBlank(checkcode) && checkcode.equals(validateCode)) {
    // 使用shiri方式
    // 獲得當前對象的狀態:未認證
    Subject subject = SecurityUtils.getSubject();
    // 用戶名密碼令牌對象
    AuthenticationToken token = new UsernamePasswordToken(username,
            password);
    try {
        subject.login(token);
    } catch (Exception e) {
        e.printStackTrace();
        return "login";
    }
    User user = (User) subject.getPrincipal();
    // user放入session
    request.getSession().setAttribute("loginUser", user);
    return "index";
}
 
開發者ID:mmdsyl,項目名稱:BLOG-Microservice,代碼行數:23,代碼來源:TestController.java

示例3: index

import org.apache.shiro.subject.Subject; //導入方法依賴的package包/類
@ApiOperation(value = "後台首頁")
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index(ModelMap modelMap) {
	// 已注冊係統
	UpmsSystemExample upmsSystemExample = new UpmsSystemExample();
	upmsSystemExample.createCriteria()
			.andStatusEqualTo((byte) 1);
	List<UpmsSystem> upmsSystems = upmsSystemService.selectByExample(upmsSystemExample);
	modelMap.put("upmsSystems", upmsSystems);
	// 當前登錄用戶權限
	Subject subject = SecurityUtils.getSubject();
	String username = (String) subject.getPrincipal();
	UpmsUser upmsUser = upmsApiService.selectUpmsUserByUsername(username);
	List<UpmsPermission> upmsPermissions = upmsApiService.selectUpmsPermissionByUpmsUserId(upmsUser.getUserId());
	modelMap.put("upmsPermissions", upmsPermissions);
	return "/manage/index.jsp";
}
 
開發者ID:ChangyiHuang,項目名稱:shuzheng,代碼行數:18,代碼來源:ManageController.java

示例4: onAccessDenied

import org.apache.shiro.subject.Subject; //導入方法依賴的package包/類
@Override
protected boolean onAccessDenied(ServletRequest request,
		ServletResponse response) throws Exception {
	
		Subject subject = getSubject(request, response);  
        if (null == subject.getPrincipal()) {//表示沒有登錄,重定向到登錄頁麵  
            saveRequest(request);
            WebUtils.issueRedirect(request, response, ShiroUtils.LOGIN_URL);  
        } else {
    		if(ShiroUtils.isAjax(request)){
    			Map<String, Object> result = new HashMap<String, Object>();
    			result.put("status", "401");
    			result.put("message", "sorry,您沒有權限");
    			result.put("url", ShiroUtils.UNAUTHORIZED);
    			ShiroUtils.writeJson(response, result);
    		}else
    		{
    			if (StringUtils.hasText(ShiroUtils.UNAUTHORIZED)) {//如果有未授權頁麵跳轉過去  
	                WebUtils.issueRedirect(request, response, ShiroUtils.UNAUTHORIZED);  
	            } else {//否則返回401未授權狀態碼  
	                WebUtils.toHttp(response).sendError(HttpServletResponse.SC_UNAUTHORIZED);  
	            } 
    		}
        }  
	return Boolean.FALSE;
}
 
開發者ID:wjggwm,項目名稱:webside,代碼行數:27,代碼來源:PermissionFilter.java

示例5: onAccessDenied

import org.apache.shiro.subject.Subject; //導入方法依賴的package包/類
@Override
protected boolean onAccessDenied(ServletRequest request,
		ServletResponse response) throws Exception {
	
		Subject subject = getSubject(request, response);  
        if (subject.getPrincipal() == null) {//表示沒有登錄,重定向到登錄頁麵  
            saveRequest(request);  
            WebUtils.issueRedirect(request, response, ShiroUtils.LOGIN_URL);  
        } else {  
        	if(ShiroUtils.isAjax(request)){
    			Map<String, Object> result = new HashMap<String, Object>();
    			result.put("status", "401");
    			result.put("message", "sorry,您沒有權限");
    			result.put("url", ShiroUtils.UNAUTHORIZED);
    			ShiroUtils.writeJson(response, result);
    		}else
    		{
    			if (StringUtils.hasText(ShiroUtils.UNAUTHORIZED)) {//如果有未授權頁麵跳轉過去  
	                WebUtils.issueRedirect(request, response, ShiroUtils.UNAUTHORIZED);  
	            } else {//否則返回401未授權狀態碼  
	                WebUtils.toHttp(response).sendError(HttpServletResponse.SC_UNAUTHORIZED);  
	            } 
    		}
        }  
	return false;
}
 
開發者ID:wjggwm,項目名稱:webside,代碼行數:27,代碼來源:RoleFilter.java

示例6: isAccessAllowed

import org.apache.shiro.subject.Subject; //導入方法依賴的package包/類
@Override
protected boolean isAccessAllowed(ServletRequest request,
		ServletResponse response, Object mappedValue) {
	Subject subject = getSubject(request, response);

	// 如果 isAuthenticated 為 false 證明不是登錄過的,同時 isRememberd 為true 證明是沒登陸直接通過記住我功能進來的
	if (!subject.isAuthenticated() && subject.isRemembered()) {
		// 獲取session的USERSESSION屬性來看session是不是空的
		if(null == ShiroAuthenticationManager.getSessionAttribute(ShiroUtils.USERSESSION))
		{
			// 初始化
			UserEntity userEntity = (UserEntity) subject.getPrincipal();
			ShiroAuthenticationManager.setSessionAttribute(ShiroUtils.USERSESSION, userEntity);
		}
	}

	// 這個方法本來隻返回 subject.isAuthenticated() 現在我們加上 subject.isRemembered()
	// 讓它同時也兼容remember這種情況
	return subject.isAuthenticated() || subject.isRemembered();
}
 
開發者ID:wjggwm,項目名稱:webside,代碼行數:21,代碼來源:RememberMeFilter.java

示例7: isAccessAllowed

import org.apache.shiro.subject.Subject; //導入方法依賴的package包/類
@Override
protected boolean isAccessAllowed(ServletRequest req, ServletResponse resp, Object mappedValue) {
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) resp;
    // 是登錄頁麵 則直接返回true
    if (isLoginRequest(request, response)) {
        return true;
    }
    // 獲取主題
    Subject subject = getSubject(request, response);
    //如果用戶【已認證】 直接跳過
    if (subject.isAuthenticated()) {
        return true;
    }
    // 如果用戶【未認證】 但是用戶使用【記住我】功能
    if (subject.isRemembered()) {
        // 獲取rememberMe的用戶名稱
        Object principal = subject.getPrincipal();
        if (null != principal) {
            //從session中 獲取登錄用戶信息
            Session session = subject.getSession(true);
            MMSnsCommonUserEntity commonUser = (MMSnsCommonUserEntity) session.getAttribute(MMSnsCommonUserEntity.MMSNS_COMMON_USER);
            //如果session不保存用戶信息;則從數據庫中獲取
            if (commonUser == null) {
                List<MMSnsCommonUserEntity> commonUsers = commonUserService.getCommonUserByCondition(principal.toString(), principal.toString(),principal.toString(),null,null);
                if (commonUser != null && commonUsers.size() > 0) {
                    session.setAttribute(MMSnsCommonUserEntity.MMSNS_COMMON_USER, commonUsers.get(0));
                }
            }
            return true;
        }
    }
    return false;
}
 
開發者ID:babymm,項目名稱:mmsns,代碼行數:35,代碼來源:MMSnsRememberAuthenticationFilter.java

示例8: onLoginSuccess

import org.apache.shiro.subject.Subject; //導入方法依賴的package包/類
@Override
protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest request, ServletResponse response) throws Exception {
	if (isLoginRequest(request, response)) {
		String email = (String)subject.getPrincipal();
		String newToken = tokenRepository.createAuthenticationToken(email);
		HTTP.writeAsJSON(response,
				MessageBean.STATUS, HTTP.Status.OK.toInt(),
				MessageBean.MESSAGE, Messages.Status.OK.toString(),
				MessageBean.TOKEN, newToken,
				MessageBean.EMAIL, email);
		return false;
	} else {
		return true;
	}
}
 
開發者ID:auslides,項目名稱:stateless-shiro,代碼行數:16,代碼來源:BearerTokenAuthenticatingFilter.java

示例9: login

import org.apache.shiro.subject.Subject; //導入方法依賴的package包/類
@ApiOperation(value = "登錄")
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login(HttpServletRequest request) {
    Subject subject = SecurityUtils.getSubject();
    Session session = subject.getSession();
    String serverSessionId = session.getId().toString();
    // 判斷是否已登錄,如果已登錄,則回跳
    String code = RedisUtil.get(ZHENG_UPMS_SERVER_SESSION_ID + "_" + serverSessionId);
    // code校驗值
    if (StringUtils.isNotBlank(code)) {
        // 回跳
        String backurl = request.getParameter("backurl");
        String username = (String) subject.getPrincipal();
        if (StringUtils.isBlank(backurl)) {
            backurl = "/";
        } else {
            if (backurl.contains("?")) {
                backurl += "&upms_code=" + code + "&upms_username=" + username;
            } else {
                backurl += "?upms_code=" + code + "&upms_username=" + username;
            }
        }
        LOGGER.debug("認證中心帳號通過,帶code回跳:{}", backurl);
        return "redirect:" + backurl;
    }
    return "/sso/login.jsp";
}
 
開發者ID:ChangyiHuang,項目名稱:shuzheng,代碼行數:28,代碼來源:SSOController.java

示例10: login

import org.apache.shiro.subject.Subject; //導入方法依賴的package包/類
@ApiOperation(value = "登錄")
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login(HttpServletRequest request) {
    Subject subject = SecurityUtils.getSubject();
    Session session = subject.getSession();
    String serverSessionId = session.getId().toString();
    // 判斷是否已登錄,如果已登錄,則回跳
    String code = RedisUtil.get(ZHENG_UPMS_SERVER_SESSION_ID + "_" + serverSessionId);
    // code校驗值
    if (StringUtils.isNotBlank(code)) {
        // 回跳
        String backurl = request.getParameter("backurl");
        String username = (String) subject.getPrincipal();
        if (StringUtils.isBlank(backurl)) {
            backurl = "/";
        } else {
            if (backurl.contains("?")) {
                backurl += "&upms_code=" + code + "&upms_username=" + username;
            } else {
                backurl += "?upms_code=" + code + "&upms_username=" + username;
            }
        }
        _log.debug("認證中心帳號通過,帶code回跳:{}", backurl);
        return "redirect:" + backurl;
    }
    return "/sso/login.jsp";
}
 
開發者ID:youngMen1,項目名稱:-Spring-SpringMVC-Mybatis-,代碼行數:28,代碼來源:SSOController.java

示例11: getActiveUser

import org.apache.shiro.subject.Subject; //導入方法依賴的package包/類
/**
 * 獲取用戶登錄之後的用戶相關信息
 * @return	<ul>
 * 				<li>登陸了,則返回 {@link ActiveUser}
 * 				<li>未登陸,返回null
 * 			</ul>
 */
public ActiveUser getActiveUser() {
	//從shiro的session中取activeUser
	Subject subject = SecurityUtils.getSubject();
	//取身份信息
	ActiveUser activeUser = (ActiveUser) subject.getPrincipal();
	if(activeUser != null){
		return activeUser;
	}else{
		return null;
	}
}
 
開發者ID:xnx3,項目名稱:wangmarket,代碼行數:19,代碼來源:BaseController.java

示例12: isAccessAllowed

import org.apache.shiro.subject.Subject; //導入方法依賴的package包/類
@Override
protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
	// 是登錄頁麵 則直接返回true
	if (isLoginRequest(request, response)) {
		return true;
	} else {
		// 獲取主題
		Subject subject = getSubject(request, response);
		// 如果用戶【未認證】 但是用戶使用【記住我】功能
		if (!subject.isAuthenticated() && subject.isRemembered()) {
			// 獲取rememberMe的用戶名稱
			Object principal = subject.getPrincipal();
			if (null != principal) {
				//從session中 獲取登錄用戶信息
				//Session session = subject.getSession(true);
				//User sessionUser = (User) session.getAttribute(User.SESSION_USER);
				
				//如果session不保存用戶信息;則從數據庫中獲取
				//if (sessionUser == null) {
				//	sessionUser = userService.getUserByName(String.valueOf(principal));
				//	subject.getSession().setAttribute(User.SESSION_USER, sessionUser);
				//}
				return true;
			}
		}
	}
	return true;
}
 
開發者ID:babymm,項目名稱:mumu,代碼行數:29,代碼來源:RememberAuthenticationFilter.java

示例13: isAccessAllowed

import org.apache.shiro.subject.Subject; //導入方法依賴的package包/類
@Override
public boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue)
		throws IOException {
	String[] permissions = getPermissions(request);
	log.info("請求權限-->>" + permissions[0]);

	//管理員擁有一切權限
	Subject subject = getSubject(request, response);
	Object principal = subject.getPrincipal();
	if(principal!=null&&"admin".equals(String.valueOf(principal))){
		return true;
	}
	return super.isAccessAllowed(request, response, permissions);
}
 
開發者ID:babymm,項目名稱:mumu,代碼行數:15,代碼來源:UserPermissionsAuthorizationFilter.java

示例14: loginUser

import org.apache.shiro.subject.Subject; //導入方法依賴的package包/類
@RequestMapping("/loginUser")
public String loginUser(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        HttpSession session) {
    UsernamePasswordToken token = new UsernamePasswordToken(username, password);
    Subject subject = SecurityUtils.getSubject();
    try {
        subject.login(token);
        User user = (User) subject.getPrincipal();
        session.setAttribute("user", user);
        return "index";
    } catch (Exception e) {
        return "login";
    }
}
 
開發者ID:fuyunwang,項目名稱:SpringBootShiro,代碼行數:16,代碼來源:IndexController.java

示例15: getMenu

import org.apache.shiro.subject.Subject; //導入方法依賴的package包/類
@RequestMapping(value = "/menu/getList",method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value = "獲取當前登錄用戶有權限的菜單")
public Object getMenu(){
    // 已注冊係統
    UpmsSystemExample upmsSystemExample = new UpmsSystemExample();
    upmsSystemExample.createCriteria().andStatusEqualTo((byte) 1);
    List<UpmsSystem> upmsSystems = upmsSystemService.selectByExample(upmsSystemExample);

    // 當前登錄用戶權限
    Subject subject = SecurityUtils.getSubject();
    String username = (String) subject.getPrincipal();
    UpmsUser upmsUser = upmsApiService.selectUpmsUserByUsername(username);
    List<UpmsPermission> upmsPermissions = upmsApiService.selectUpmsPermissionByUpmsUserId(upmsUser.getUserId());

    JSONArray jsonArr = new JSONArray();
    for(UpmsSystem upmsSystem : upmsSystems) {
        JSONObject rootNode = new JSONObject();
        rootNode.put("name", upmsSystem.getSystemId());
        rootNode.put("title", upmsSystem.getTitle());
        rootNode.put("description", upmsSystem.getDescription());
        rootNode.put("icon", upmsSystem.getIcon());
        rootNode.put("order", upmsSystem.getOrders());
        rootNode.put("basePath", upmsSystem.getBasepath());
        rootNode.put("banner", upmsSystem.getBanner());
        jsonArr.add(menuTreeFactory(rootNode,upmsPermissions, true));
    }

    return new UpmsResult(UpmsResultConstant.SUCCESS, jsonArr);
}
 
開發者ID:sunzhen086,項目名稱:lambo,代碼行數:31,代碼來源:UpmsApiController.java


注:本文中的org.apache.shiro.subject.Subject.getPrincipal方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。