mirror of
https://github.com/dqjdda/Aurora.git
synced 2018-11-06 05:33:29 +00:00
新增在线用户管理,更新sql文件,部分代码优化
This commit is contained in:
@@ -17,7 +17,6 @@ import org.springframework.web.bind.annotation.GetMapping;
|
||||
@EnableScheduling
|
||||
@SpringBootApplication
|
||||
@EnableTransactionManagement
|
||||
@EnableConfigurationProperties({AuroraProperties.class})
|
||||
/**
|
||||
* @author 郑杰
|
||||
* @date 2018/08/21 11:54:10
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package me.aurora.app.rest.system;
|
||||
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.aurora.annotation.Log;
|
||||
import me.aurora.domain.ResponseEntity;
|
||||
import me.aurora.service.SessionService;
|
||||
import org.apache.shiro.authz.annotation.Logical;
|
||||
import org.apache.shiro.authz.annotation.RequiresPermissions;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author 郑杰
|
||||
* @date 2018/09/21 15:49:14
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/online")
|
||||
public class SessionController {
|
||||
|
||||
@Autowired
|
||||
private SessionService sessionService;
|
||||
|
||||
@GetMapping("/index")
|
||||
public ModelAndView online() {
|
||||
return new ModelAndView("/system/online/index");
|
||||
}
|
||||
|
||||
@Log("获取在线用户")
|
||||
@GetMapping(value = "/getOnlineInfo")
|
||||
public Map getOnlineInfo(){
|
||||
return sessionService.getOnlineInfo();
|
||||
}
|
||||
|
||||
@Log("踢出用户")
|
||||
@RequiresPermissions(value={"admin", "user:all","user:logout"}, logical= Logical.OR)
|
||||
@DeleteMapping("/forceLogout")
|
||||
public ResponseEntity forceLogout(String id) {
|
||||
sessionService.forceLogout(id);
|
||||
return ResponseEntity.ok();
|
||||
}
|
||||
}
|
||||
@@ -16,8 +16,6 @@ import org.springframework.data.domain.Sort;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -52,6 +50,7 @@ public class PictureController {
|
||||
* @param limit
|
||||
* @return
|
||||
*/
|
||||
@Log("查询全部图片")
|
||||
@RequiresPermissions(value={"admin", "picture:all","picture:select"}, logical= Logical.OR)
|
||||
@GetMapping(value = "/getPictureInfo")
|
||||
public Map getLogInfo(@RequestParam(value = "username",required = false) String username,
|
||||
@@ -83,6 +82,7 @@ public class PictureController {
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
@Log("上传图片")
|
||||
@RequiresPermissions (value={"admin", "picture:all","picture:upload"}, logical= Logical.OR)
|
||||
@PostMapping(value = "/upload")
|
||||
public ResponseEntity uploadAccessory(@RequestParam MultipartFile file) throws Exception {
|
||||
|
||||
@@ -22,9 +22,9 @@ public class GlobalExceptionHandler {
|
||||
* @return
|
||||
*/
|
||||
@ExceptionHandler(value = AuthorizationException.class)
|
||||
public String handleAuthorizationException() {
|
||||
public ModelAndView handleAuthorizationException() {
|
||||
log.error("没有权限访问");
|
||||
return "/exception/403";
|
||||
return new ModelAndView("/exception/403");
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,7 +5,7 @@ import org.apache.shiro.session.SessionListener;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
/**会话管理
|
||||
* @author 郑杰
|
||||
* @date 2018/08/22 14:42:27
|
||||
*/
|
||||
|
||||
@@ -40,7 +40,7 @@ public class RedisConfig extends CachingConfigurerSupport {
|
||||
private int port;
|
||||
|
||||
@Value("${spring.redis.password}")
|
||||
private String password = "";
|
||||
private String password;
|
||||
|
||||
@Value("${spring.redis.timeout}")
|
||||
private String timeout;
|
||||
|
||||
@@ -45,7 +45,7 @@ public class ShiroConfig {
|
||||
private int port;
|
||||
|
||||
@Value("${spring.redis.password}")
|
||||
private String password = "";
|
||||
private String password;
|
||||
|
||||
@Value("${spring.redis.timeout}")
|
||||
private String timeout;
|
||||
@@ -78,6 +78,7 @@ public class ShiroConfig {
|
||||
return shiroFilterFactoryBean;
|
||||
}
|
||||
|
||||
|
||||
@Bean(name = "lifecycleBeanPostProcessor")
|
||||
public static LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
|
||||
// shiro 生命周期处理器
|
||||
@@ -136,10 +137,15 @@ public class ShiroConfig {
|
||||
securityManager.setRememberMeManager(rememberMeManager());
|
||||
// 配置 缓存管理类 cacheManager
|
||||
securityManager.setCacheManager(cacheManager());
|
||||
// session管理
|
||||
securityManager.setSessionManager(sessionManager());
|
||||
return securityManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* 会话管理
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public RedisSessionDAO redisSessionDAO() {
|
||||
RedisSessionDAO redisSessionDAO = new RedisSessionDAO();
|
||||
|
||||
@@ -4,6 +4,8 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import lombok.*;
|
||||
import org.hibernate.annotations.CreationTimestamp;
|
||||
import org.hibernate.annotations.UpdateTimestamp;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.persistence.*;
|
||||
import java.io.Serializable;
|
||||
@@ -24,7 +26,7 @@ public class Role implements Serializable {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
|
||||
@NotBlank(groups = {New.class,Update.class})
|
||||
@Column(nullable = false)
|
||||
private String name;
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package me.aurora.domain;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 在线用户管理
|
||||
* @author 郑杰
|
||||
* @date 2018/09/21 15:38:06
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
public class UserOnline implements Serializable{
|
||||
|
||||
/**
|
||||
* session id
|
||||
*/
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private String id;
|
||||
|
||||
/**
|
||||
* 用户id
|
||||
*/
|
||||
private String userId;
|
||||
|
||||
/**
|
||||
* 用户名称
|
||||
*/
|
||||
private String username;
|
||||
|
||||
/**
|
||||
* 用户主机地址
|
||||
*/
|
||||
private String host;
|
||||
|
||||
/**
|
||||
* 用户登录时系统IP
|
||||
*/
|
||||
private String systemHost;
|
||||
|
||||
/**
|
||||
* 状态
|
||||
*/
|
||||
private String status;
|
||||
|
||||
/**
|
||||
* session创建时间
|
||||
*/
|
||||
private Date startTimestamp;
|
||||
|
||||
/**
|
||||
* session最后访问时间
|
||||
*/
|
||||
private Date lastAccessTime;
|
||||
|
||||
/**
|
||||
* 超时时间
|
||||
*/
|
||||
private Long timeout;
|
||||
|
||||
/**
|
||||
* 所在地
|
||||
*/
|
||||
private String location;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package me.aurora.service;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author 郑杰
|
||||
* @date 2018/09/21 15:42:27
|
||||
*/
|
||||
public interface SessionService {
|
||||
|
||||
Map getOnlineInfo();
|
||||
|
||||
boolean forceLogout(String sessionId);
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package me.aurora.service.impl;
|
||||
|
||||
import cn.hutool.http.HttpStatus;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.aurora.config.exception.AuroraException;
|
||||
import me.aurora.domain.User;
|
||||
import me.aurora.domain.UserOnline;
|
||||
import me.aurora.service.SessionService;
|
||||
import me.aurora.util.AddressUtils;
|
||||
import me.aurora.util.HttpContextUtils;
|
||||
import me.aurora.util.PageUtil;
|
||||
import org.apache.shiro.session.Session;
|
||||
import org.apache.shiro.session.mgt.eis.SessionDAO;
|
||||
import org.apache.shiro.subject.SimplePrincipalCollection;
|
||||
import org.apache.shiro.subject.support.DefaultSubjectContext;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Shiro Session 对象管理
|
||||
* @author 郑杰
|
||||
* @date 2018/09/21 15:43:02
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class SessionServiceImpl implements SessionService {
|
||||
|
||||
@Autowired
|
||||
private SessionDAO sessionDAO;
|
||||
|
||||
@Autowired
|
||||
ObjectMapper mapper;
|
||||
|
||||
@Override
|
||||
public Map getOnlineInfo() {
|
||||
List<UserOnline> list = new ArrayList<>();
|
||||
Collection<Session> sessions = sessionDAO.getActiveSessions();
|
||||
for (Session session : sessions) {
|
||||
UserOnline userOnline = new UserOnline();
|
||||
User user;
|
||||
SimplePrincipalCollection principalCollection;
|
||||
if (session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY) == null) {
|
||||
continue;
|
||||
} else {
|
||||
principalCollection = (SimplePrincipalCollection) session
|
||||
.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
|
||||
user = (User) principalCollection.getPrimaryPrincipal();
|
||||
userOnline.setUsername(user.getUsername());
|
||||
userOnline.setUserId(user.getId().toString());
|
||||
}
|
||||
userOnline.setId((String) session.getId());
|
||||
userOnline.setHost(session.getHost());
|
||||
userOnline.setStartTimestamp(session.getStartTimestamp());
|
||||
userOnline.setLastAccessTime(session.getLastAccessTime());
|
||||
Long timeout = session.getTimeout();
|
||||
if (timeout == 0L) {
|
||||
userOnline.setStatus("0");
|
||||
} else {
|
||||
userOnline.setStatus("1");
|
||||
}
|
||||
String address = AddressUtils.getCityInfo(userOnline.getHost());
|
||||
userOnline.setLocation(address);
|
||||
userOnline.setTimeout(timeout);
|
||||
list.add(userOnline);
|
||||
}
|
||||
return PageUtil.buildPage(list,Long.parseLong(list.size()+""));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean forceLogout(String sessionId) {
|
||||
HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
|
||||
String id = request.getSession().getId();
|
||||
if(id.equals(sessionId)){
|
||||
throw new AuroraException(HttpStatus.HTTP_BAD_REQUEST,"不能踢出自己!");
|
||||
}
|
||||
try {
|
||||
Session session = sessionDAO.readSession(sessionId);
|
||||
session.setTimeout(0);
|
||||
session.stop();
|
||||
sessionDAO.delete(session);
|
||||
} catch (Exception e) {
|
||||
log.error("踢出用户失败", e);
|
||||
throw new AuroraException(HttpStatus.HTTP_INTERNAL_ERROR,"踢出用户失败!");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user