本文整理汇总了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);
}