UserRealm.java
3.2 KB
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
package com.framework.shiro;
import java.util.HashMap;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.fastjson.JSONObject;
import com.eport.rest.entity.EptUserInfoEntity;
import com.eport.rest.service.EptMenuService;
import com.eport.rest.service.EptUserInfoService;
import com.google.common.base.Objects;
import com.google.common.collect.Maps;
public class UserRealm extends AuthorizingRealm {
@Autowired
private ShiroFilterFactoryBean shiroBean;
@Autowired
private EptUserInfoService userService;
@Autowired
private EptMenuService menuService;
/**
* 返回当前subject的授权信息 交由shiro的Authorizer鉴权
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
String loginAccount = (String) principals.getPrimaryPrincipal();
HashMap<String, Object> par = Maps.newHashMap();
par.put("account", loginAccount);
HashMap<String, Object> user = userService.findByAccount(par);
if(user!=null){
EptUserInfoEntity userInfo = JSONObject.parseObject(JSONObject.toJSONString(user), EptUserInfoEntity.class);
}
return authorizationInfo;
}
/**
* 返回用户身份认证信息 交由shiro的Authenticator验证
*
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
String loginAccount = (String) token.getPrincipal();
HashMap<String, Object> par = Maps.newHashMap();
par.put("account", loginAccount);
HashMap<String, Object> user = userService.findByAccount(par);
if (user == null) {
throw new UnknownAccountException();
}
// user.setLastLoginTime(new Date());
// userService.save(user);
Subject subject = SecurityUtils.getSubject();
SimpleAuthenticationInfo authenticationInfo = null;
EptUserInfoEntity userInfo = JSONObject.parseObject(JSONObject.toJSONString(user), EptUserInfoEntity.class);
authenticationInfo = new SimpleAuthenticationInfo(userInfo.getAccount(), userInfo.getPassword(), getName());
if(!Objects.equal(userInfo.getStatus(), 1)){
throw new LockedAccountException();
}
// 创建会话
System.out.println("==================="+userInfo.getUserId());
subject.getSession().setAttribute("userInfo", userInfo);
// 缓存用户菜单权限
// subject.getSession().setAttribute("menuList",
// roleService.findAllFunctionByRole(user.getRole()));
return authenticationInfo;
}
}