Commit d3d5a3ff by fengshuonan

完善登录的逻辑

parent 008536d3
......@@ -53,6 +53,7 @@ public enum BizExceptionEnum implements AbstractBaseExceptionEnum {
/**
* 账户问题
*/
NOT_LOGIN(401, "当前用户未登录"),
USER_ALREADY_REG(401, "该用户已经注册"),
NO_THIS_USER(400, "没有此用户"),
USER_NOT_EXISTED(400, "没有此用户"),
......
......@@ -17,8 +17,10 @@ package cn.stylefeng.guns.core.shiro;
import cn.stylefeng.guns.core.common.constant.Const;
import cn.stylefeng.guns.core.common.constant.factory.ConstantFactory;
import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.modular.system.entity.User;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
......@@ -92,6 +94,19 @@ public class ShiroKit {
}
/**
* 获取ShiroUser,不为空的
*
* @return ShiroUser
*/
public static ShiroUser getUserNotNull() {
if (isGuest()) {
throw new ServiceException(BizExceptionEnum.NOT_LOGIN);
} else {
return (ShiroUser) getSubject().getPrincipals().getPrimaryPrincipal();
}
}
/**
* 从shiro获取session
*/
public static Session getSession() {
......
......@@ -15,26 +15,19 @@
*/
package cn.stylefeng.guns.modular.system.controller;
import cn.stylefeng.guns.core.common.exception.InvalidKaptchaException;
import cn.stylefeng.guns.core.log.LogManager;
import cn.stylefeng.guns.core.log.factory.LogTaskFactory;
import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.core.shiro.ShiroUser;
import cn.stylefeng.guns.core.util.KaptchaUtil;
import cn.stylefeng.guns.modular.system.service.NoticeService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.google.code.kaptcha.Constants;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;
import java.util.Map;
import static cn.stylefeng.roses.core.util.HttpContext.getIp;
......@@ -47,31 +40,32 @@ import static cn.stylefeng.roses.core.util.HttpContext.getIp;
@Controller
public class LoginController extends BaseController {
@Autowired
private NoticeService noticeService;
/**
* 跳转到主页
*
* @author fengshuonan
* @Date 2018/12/23 5:41 PM
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public String index(Model model) {
List<Long> roleList = ShiroKit.getUser().getRoleList();
//获取当前用户角色列表
List<Long> roleList = ShiroKit.getUserNotNull().getRoleList();
if (roleList == null || roleList.size() == 0) {
ShiroKit.getSubject().logout();
model.addAttribute("tips", "该用户没有角色,无法登陆");
return "/login.html";
}
//主页包含通知列表
List<Map<String, Object>> notices = noticeService.list(null);
model.addAttribute("noticeList", notices);
return "/index.html";
}
/**
* 跳转到登录页面
*
* @author fengshuonan
* @Date 2018/12/23 5:41 PM
*/
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login() {
......@@ -84,6 +78,9 @@ public class LoginController extends BaseController {
/**
* 点击登录执行的动作
*
* @author fengshuonan
* @Date 2018/12/23 5:42 PM
*/
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String loginVali() {
......@@ -92,27 +89,21 @@ public class LoginController extends BaseController {
String password = super.getPara("password").trim();
String remember = super.getPara("remember");
//验证验证码是否正确
if (KaptchaUtil.getKaptchaOnOff()) {
String kaptcha = super.getPara("kaptcha").trim();
String code = (String) super.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
if (ToolUtil.isEmpty(kaptcha) || !kaptcha.equalsIgnoreCase(code)) {
throw new InvalidKaptchaException();
}
}
Subject currentUser = ShiroKit.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken(username, password.toCharArray());
//如果开启了记住我功能
if ("on".equals(remember)) {
token.setRememberMe(true);
} else {
token.setRememberMe(false);
}
//执行shiro登录操作
currentUser.login(token);
ShiroUser shiroUser = ShiroKit.getUser();
//登录成功,记录登录日志
ShiroUser shiroUser = ShiroKit.getUserNotNull();
LogManager.me().executeLog(LogTaskFactory.loginLog(shiroUser.getId(), getIp()));
ShiroKit.getSession().setAttribute("sessionFlag", true);
......@@ -122,10 +113,13 @@ public class LoginController extends BaseController {
/**
* 退出登录
*
* @author fengshuonan
* @Date 2018/12/23 5:42 PM
*/
@RequestMapping(value = "/logout", method = RequestMethod.GET)
public String logOut() {
LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), getIp()));
LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUserNotNull().getId(), getIp()));
ShiroKit.getSubject().logout();
deleteAllCookie();
return REDIRECT + "/login";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment