package com.yueke.gemini.shiro.realm;
/**
* Created by admin on 2019/2/23.
*/
import com.yueke.gemini.shiro.constants.Global;
import com.yueke.gemini.shiro.entity.Menu;
import com.yueke.gemini.shiro.entity.Role;
import com.yueke.gemini.shiro.entity.User;
import com.yueke.gemini.shiro.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 自定义的指定Shiro验证用户登录的类
* 在本例中定义了2个用户:jadyer和玄玉,jadyer具有admin角色和admin:manage权限,玄玉不具有任何角色和权限
* @User: coding99
*
*/
@Service
public class SystemAuthorizingRealm extends AuthorizingRealm {
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private UserService userService;
// @Autowired
// private RoleService roleService;
// @Autowired
// private MenuService menuService;
/**
* 为当前登录的Subject授予角色和权限
* 经测试:本例中该方法的调用时机为需授权资源被访问时
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
//获取当前登录的用户名,等价于(String)principals.fromRealm(this.getName()).iterator().next()
String currentAccount = (String) super.getAvailablePrincipal(principals);
List<String> roleNameList = new ArrayList<String>();
List<String> permissionList = new ArrayList<String>();
//从数据库中获取当前登录用户的详细信息
User user = getUser();
if (null != user) {
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
List<Role> roleList = null;// roleService.selectRoleByUserId(user.getId());
Map<String, Object> map = new HashMap<String, Object>();
map.put("userId", user.getId());
map.put("permission", "permission");
List<Menu> menuList = null; // menuService.selectMenuByUserId(map);
/*构建用户的角色集合*/
for (Role role : roleList) {
roleNameList.add(role.getRolename());
}
info.addRoles(roleNameList);
/*构建用户的权限代码集合*/
// for (Menu menu : menuList) {
// if (StringUtils.isNotBlank(menu.getPermission())) {
// if (StringUtils.isNotEmpty(menu.getPermission())) {
// String[] permissions = menu.getPermission().split(",");
// for (int i = 0; i < permissions.length; i++) {
// if (StringUtils.isNotEmpty(permissions[i])) {
// permissionList.add(permissions[i]);
// }
// }
// }
// }
// }
info.addStringPermissions(permissionList);
return info;
} else {
return null;
}
}
/**
* 验证当前登录的Subject
* 经测试:本例中该方法的调用时机为LoginController.login()方法中执行Subject.login()时
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
String username = token.getUsername();
User user = userService.findByName(username);
if (user == null) {
throw new UnknownAccountException("帐号找不到");
}
if ("0".equals(user.getStatus())) {
throw new LockedAccountException("msg:该已帐号禁止登录.");
}
SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(), getName());
setSession(Global.GLOBAL_USER, user);
return authenticationInfo;
}
/**
* 将一些数据放到ShiroSession中,以便于其它地方使用
* 比如Controller,使用时直接用HttpSession.getAttribute(key)就可以取到
*/
private void setSession(Object key, Object value) {
Subject currentUser = SecurityUtils.getSubject();
if (null != currentUser) {
Session session = currentUser.getSession();
System.out.println("Session默认超时时间为[" + session.getTimeout() + "]毫秒");
if (null != session) {
session.setAttribute(key, value);
}
}
}
private User getUser() {
Subject currentUser = SecurityUtils.getSubject();
Session session = currentUser.getSession();
User user = (User) session.getAttribute(Global.GLOBAL_USER);
return user;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
gemini-shiro.zip (59个子文件)
gemini-shiro
pom.xml 5KB
gemini-shiro.iml 4KB
src
test
java
main
resources
applicationContext-Servlet.xml 3KB
applicationContext-Shiro.xml 4KB
java
com
yueke
gemini
shiro
controller
HomeController.java 2KB
service
UserService.java 289B
impl
UserServiceImpl.java 409B
entity
Menu.java 167B
Permission.java 917B
Role.java 2KB
User.java 2KB
constants
Global.java 174B
realm
SystemAuthorizingRealm.java 5KB
UserRealm.java 2KB
webapp
user.jsp 2KB
WEB-INF
web.xml 2KB
403.jsp 391B
login.jsp 760B
权限.png 72KB
.idea
libraries
Maven__aopalliance_aopalliance_1_0.xml 514B
Maven__com_fasterxml_classmate_0_8_0.xml 522B
Maven__javax_inject_javax_inject_1.xml 511B
Maven__junit_junit_3_8_1.xml 462B
Maven__org_springframework_spring_aop_3_2_4_RELEASE.xml 609B
Maven__javax_servlet_jstl_1_2.xml 473B
Maven__org_springframework_spring_orm_3_2_4_RELEASE.xml 609B
Maven__javax_servlet_servlet_api_2_5.xml 522B
Maven__net_sf_ehcache_ehcache_2_7_2.xml 512B
Maven__mysql_mysql_connector_java_5_1_26.xml 574B
Maven__org_springframework_spring_tx_3_2_4_RELEASE.xml 602B
Maven__org_apache_shiro_shiro_spring_1_2_2.xml 555B
Maven__commons_beanutils_commons_beanutils_1_8_3.xml 594B
Maven__org_springframework_spring_jdbc_3_2_4_RELEASE.xml 616B
Maven__org_codehaus_jackson_jackson_core_asl_1_9_13.xml 606B
Maven__org_apache_shiro_shiro_web_1_2_2.xml 534B
Maven__org_springframework_spring_webmvc_3_2_4_RELEASE.xml 630B
Maven__org_apache_shiro_shiro_core_1_2_2.xml 541B
Maven__org_jboss_logging_jboss_logging_3_1_1_GA.xml 587B
Maven__org_springframework_spring_context_3_2_4_RELEASE.xml 637B
Maven__org_springframework_spring_expression_3_2_4_RELEASE.xml 658B
Maven__commons_pool_commons_pool_1_5_4.xml 539B
Maven__org_springframework_spring_beans_3_2_4_RELEASE.xml 623B
Maven__javax_persistence_javax_persistence_api_2_2.xml 608B
Maven__org_codehaus_jackson_jackson_mapper_asl_1_9_13.xml 620B
Maven__org_apache_commons_commons_lang3_3_8_1.xml 570B
Maven__org_springframework_spring_web_3_2_4_RELEASE.xml 609B
Maven__commons_logging_commons_logging_1_1_1.xml 572B
Maven__org_slf4j_slf4j_api_1_6_6.xml 506B
Maven__commons_dbcp_commons_dbcp_1_4.xml 525B
Maven__org_hibernate_hibernate_validator_5_0_1_Final.xml 634B
Maven__javax_validation_validation_api_1_1_0_Final.xml 611B
Maven__org_springframework_spring_core_3_2_4_RELEASE.xml 616B
workspace.xml 63KB
misc.xml 1KB
compiler.xml 789B
modules.xml 264B
encodings.xml 271B
copyright
profiles_settings.xml 76B
Shiro.md 2KB
共 59 条
- 1
资源评论
工程师知遇
- 粉丝: 13
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 汇编语言入门与编程实践-低层开发者的必备技能
- WatchIO二进制固件和刷机工具(无需源码编译).zip
- 提取网页核心信息:Python中的Readability与Date Extraction技术
- Swift语言教程:从基础语法到高级特性的全面讲解
- 表白代码(发射爱心).zip学习资料程序
- 常用工具合集(包括汉字转拼音工具、常用数据格式相互转换工具、尺寸相关的工具类).zip
- Delphi编程教程:从入门到精通Windows应用程序开发
- 视觉化编程入门指南:Visual Basic语言教程及其应用领域
- 纯代码实现的3d爱心.zip学习资料语言
- 儿童编程教育中Scratch语言的基础教学及实战示例
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功