LoginServiceImpl.java
4.43 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
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
package com.server.shiro.persistent.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.server.shiro.constants.Constants;
import com.server.shiro.persistent.bean.*;
import com.server.shiro.persistent.repository.UserRepository;
import com.server.shiro.persistent.service.LoginService;
import com.server.shiro.util.CommonUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
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 javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.*;
/**
* Created by yinbinhome@163.com on 2018/2/23.
* description:
*/
@Service
public class LoginServiceImpl implements LoginService {
private Logger logger = LoggerFactory.getLogger(LoginServiceImpl.class);
@Autowired
private UserRepository userRepository;
@PersistenceContext
private EntityManager entityManager;
/**
* 登录表单提交
*
* @param jsonObject
* @return
*/
@Override
public JSONObject authLogin(JSONObject jsonObject) {
String username = jsonObject.getString("username");
String password = jsonObject.getString("password");
Subject currentUser = SecurityUtils.getSubject();
Session session = currentUser.getSession();
String sessionId = session.getId().toString();
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
currentUser.login(token);
SysUser user= (SysUser) session.getAttribute(Constants.SESSION_USER_INFO);
UserModel usermodel=getModel(user);
usermodel.setSessionid(sessionId);
return CommonUtil.loginJson(usermodel);
}
/**
* 根据用户名和密码查询对应的用户
*
* @param username 用户名
* @param password 密码
* @return
*/
@Override
public SysUser getUser(String username, String password) {
userRepository.findAll();
return userRepository.getByUsernameIsAndPasswordIs(username, password);
}
/**
* 查询当前登录用户的权限等信息
*
* @return
*/
@Override
public SysUser getInfo() {
//从session获取用户信息
Session session = SecurityUtils.getSubject().getSession();
SysUser userInfo = (SysUser) session.getAttribute(Constants.SESSION_USER_INFO);
return userInfo;
}
/**
* 退出登录
*
* @return
*/
@Override
public void logout() {
try {
Subject currentUser = SecurityUtils.getSubject();
currentUser.logout();
} catch (Exception e) {
}
}
//根据SysUser转换前端Model
public UserModel getModel(SysUser user){
UserModel model=new UserModel();
model.setUsername(user.getUsername());
List<String> rules=new ArrayList<String>();
List<MenuModel> menus=getMenus(user.getMenus());
Set<SysRole> roles = (Set<SysRole>) user.getRoles();
for(SysRole r:roles){
for(SysRule rule:r.getRules()){
rules.add(rule.getRuleCode());
}
}
model.setMenus(menus);
model.setRules(rules);
return model;
}
public List<MenuModel> getMenus(Set<SysMenu> menus){
List<MenuModel> menu_1=new ArrayList<MenuModel>();
for(SysMenu menu:menus){
if(menu.getLevel()==1){
MenuModel model=new MenuModel();
model.setWeight(menu.getWeight());
model.setName(menu.getMenuName());
List<Map<String,String>> ms=new ArrayList<Map<String,String>>();
for(SysMenu m:menus){
if(m.getParentid()==menu.getId()){
Map<String,String> map=new HashMap<String,String>();
map.put("menu_name",m.getMenuName());
map.put("menu_code",m.getMenuCode());
map.put("menu_weight",m.getWeight()+"");
map.put("menu_url",m.getMenuUrl());
ms.add(map);
}
}
model.setMenus(ms);
menu_1.add(model);
}
}
return menu_1;
}
}