本文整理匯總了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);
}
}
示例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";
}
示例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";
}
示例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;
}
示例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;
}
示例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();
}
示例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;
}
示例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;
}
}
示例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";
}
示例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";
}
示例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;
}
}
示例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;
}
示例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);
}
示例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";
}
}
示例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);
}