1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//package com.project.shiro.util;
//
//import com.alibaba.fastjson.JSONObject;
//import com.project.model.core.Admin;
//import com.project.model.core.Menu;
//import com.project.model.core.Role;
//import com.project.service.core.AdminService;
//import com.project.service.core.MenuService;
//import org.apache.commons.codec.digest.DigestUtils;
//import org.apache.commons.collections.CollectionUtils;
//import org.apache.commons.lang.time.DateUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.apache.shiro.authc.*;
//import org.apache.shiro.authz.AuthorizationInfo;
//import org.apache.shiro.authz.Permission;
//import org.apache.shiro.authz.SimpleAuthorizationInfo;
//import org.apache.shiro.realm.AuthorizingRealm;
//import org.apache.shiro.subject.PrincipalCollection;
//
//import javax.annotation.Resource;
//import java.io.PrintWriter;
//import java.util.*;
//
///**
// * 认证
// */
//public class AuthenticationRealm extends AuthorizingRealm {
//
// @Resource(name = "adminServiceImpl")
// private AdminService adminService;
//
// @Resource(name = "menuServiceImpl")
// private MenuService menuService;
//
// /**
// * 获取认证信息
// */
// @Override
// protected AuthenticationInfo doGetAuthenticationInfo(org.apache.shiro.authc.AuthenticationToken token) {
// AuthenticationToken authToken = (AuthenticationToken) token;
// // 获取登录名、密码
// String username = authToken.getUsername();
// String password = new String(authToken.getPassword());
//
// if (username != null && password != null) {
// Admin admin = adminService.getByLoginName(username);
//
// if (admin == null) {
// throw new UnknownAccountException();
// }
//
// if (!admin.getStatus().equals(Admin.STATUS_ENUM.ENABLE.getValue())) {
// throw new DisabledAccountException();
// }
//
// //用户锁定
// if (admin.getIsLocked()) {
//
// //账号锁定分钟数
// Date lockedDate = admin.getLockedDate();
// Date unlockedDate = DateUtils.addMinutes(lockedDate, 10);
//
// //判断锁定时间是否已过
// if (new Date().after(unlockedDate)) {
// admin.setLoginFailCnt(0);
// admin.setIsLocked(false);
// admin.setLockedDate(null);
// adminService.update(admin);
// } else {
// throw new LockedAccountException();
// }
// }
//
// //密码不正确
// if (!DigestUtils.md5Hex(password).equals(admin.getLoginPwd())) {
// int loginFailCount = admin.getLoginFailCnt() + 1;
// if (loginFailCount >= 5) {
// admin.setIsLocked(true);
// admin.setLockedDate(new Date());
// }
// admin.setLoginFailCnt(loginFailCount);
// adminService.update(admin);
// throw new IncorrectCredentialsException();
// }
// admin.setLoginFailCnt(0);
// adminService.update(admin);
// return new SimpleAuthenticationInfo(new Principal(admin.getId(), username), password, getName());
// }
// throw new UnknownAccountException();
// }
//
// /**
// * 获取授权信息
// */
// @Override
// protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
//
// Principal principal = (Principal) principals.fromRealm(getName()).iterator().next();
//
// if (principal != null) {
// SimpleAuthorizationInfo authInfo = new SimpleAuthorizationInfo();
//
// //获取admin对象
// Admin adminTemp = new Admin();
// adminTemp.setId(principal.getId());
// List<Admin> admins = adminService.getAdminCascadeRole(adminTemp);
// Admin admin = admins.get(0);
//
// //获取用户的角色信息
// Set<String> roleSet = new HashSet<String>();
// for (Role role : admin.getRoles()) {
// if (role.getStatus().equals(Role.STATUS_ENUM.ENABLE.getValue())) {
// roleSet.add(role.getRoleCode());
// }
// }
//
// //根据角色ids获取权限信息
// List<Menu> menuList = menuService.findOrdinaryMenu(principal.getId());
// Set<String> menuSet = new HashSet<String>();
// for (Menu menu : menuList) {
// if (StringUtils.isNotBlank(menu.getCode())) {
// menuSet.add(menu.getCode());
// }
// }
//
// //将角色和资源放入授权对象中
// authInfo.addRoles(roleSet);
// authInfo.addStringPermissions(menuSet);
// return authInfo;
// }
//
// return null;
// }
//
// /**
// * 超级管理员自动获取所有权限
// */
//// @Override
//// public boolean isPermitted(PrincipalCollection principals, String permission) {
////// User user = ((User) principals.getPrimaryPrincipal());
////// if (Role.ADMIN_FLAG_SUPER_ADMIN == user.getRole().getAdminFlag()) {
////// return true;
////// }
////
//// return isPermitted(principals, getPermissionResolver().resolvePermission(permission));
//// }
//
// @Override
// public boolean isPermitted(PrincipalCollection principals, Permission permission) {
// AuthorizationInfo info = getAuthorizationInfo(principals);
// Collection<Permission> perms = getPermissions(info);
// if (CollectionUtils.isEmpty(perms)) {
// return false;
// }
//
// for (Permission perm : perms) {
// if (perm.implies(permission)) {
// return true;
// }
// }
//
// return false;
// }
//
// /**
// * 踢掉上一个登录的同名用户
// *
// * @param id 主键
// */
//
//// private void stopPreviousSession(Integer id) {
//// Collection<Session> sessions = sessionDAO.getActiveSessions();
//// Session currSession = SecurityUtils.getSubject().getSession();
//// Serializable sId = currSession.getId();
//// for (Session session : sessions) {
//// SimplePrincipalCollection collection = (SimplePrincipalCollection) session
//// .getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
//// if (collection == null) {
//// continue;
//// }
////
//// User u = (User) collection.getPrimaryPrincipal();
//// if (id.equals(u.getId())) {
//// if (sId.equals(session.getId())) {
//// continue;
//// }
////
//// session.stop();
//// break;
//// }
//// }
//// }
//
//
//}