| | |
| | | package com.stylefeng.guns.modular.system.controller.system; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.baomidou.mybatisplus.mapper.EntityWrapper; |
| | | import com.google.code.kaptcha.Constants; |
| | | import com.stylefeng.guns.core.base.controller.BaseController; |
| | | import com.stylefeng.guns.core.common.constant.Const; |
| | |
| | | import com.stylefeng.guns.core.util.KaptchaUtil; |
| | | import com.stylefeng.guns.core.util.SinataUtil; |
| | | import com.stylefeng.guns.core.util.ToolUtil; |
| | | import com.stylefeng.guns.modular.system.dao.UserMapper; |
| | | import com.stylefeng.guns.modular.system.model.Menu; |
| | | import com.stylefeng.guns.modular.system.model.Relation; |
| | | import com.stylefeng.guns.modular.system.model.User; |
| | | import com.stylefeng.guns.modular.system.service.IMenuService; |
| | | import com.stylefeng.guns.modular.system.service.IRelationService; |
| | | import com.stylefeng.guns.modular.system.service.IUserService; |
| | | import com.stylefeng.guns.modular.system.util.AESUtil; |
| | | import com.stylefeng.guns.modular.system.util.AESUtils; |
| | | import com.stylefeng.guns.modular.system.util.RedisUtil; |
| | | import com.stylefeng.guns.modular.system.warpper.LoginUser; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.codec.binary.Base64; |
| | | import org.apache.shiro.authc.SimpleAuthenticationInfo; |
| | | import org.apache.shiro.authc.UsernamePasswordToken; |
| | | import org.apache.shiro.subject.Subject; |
| | |
| | | import org.springframework.web.bind.annotation.RequestMethod; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpSession; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | import static com.stylefeng.guns.core.support.HttpKit.getIp; |
| | | |
| | |
| | | * @author fengshuonan |
| | | * @Date 2017年1月10日 下午8:25:24 |
| | | */ |
| | | @Slf4j |
| | | @Controller |
| | | public class LoginController extends BaseController { |
| | | |
| | |
| | | @Autowired |
| | | private RedisUtil redisUtil; |
| | | |
| | | @Autowired |
| | | private IShiro shiro; |
| | | |
| | | @Autowired |
| | | private UserMapper userMapper; |
| | | |
| | | private Map<String, Integer> loginFailures = new HashMap<>(); |
| | | |
| | | @Autowired |
| | | private IRelationService relationService; |
| | | |
| | | |
| | | |
| | |
| | | * 跳转到主页 |
| | | */ |
| | | @RequestMapping(value = "/", method = RequestMethod.GET) |
| | | public String index(Model model) { |
| | | public String index(Model model, HttpServletRequest request) throws UnsupportedEncodingException { |
| | | //session中解析用户数据 |
| | | HttpSession session = request.getSession(); |
| | | String onconParam = edu.yale.its.tp.cas.client.Util.getOnconParam(session); |
| | | onconParam = new String(Base64.decodeBase64(onconParam), "UTF-8"); |
| | | LoginUser loginUser = JSON.parseObject(onconParam, LoginUser.class); |
| | | log.info("session解析结果:" + JSON.toJSONString(loginUser)); |
| | | //获取菜单列表 |
| | | List<Integer> roleList = ShiroKit.getUser().getRoleList(); |
| | | if (roleList == null || roleList.size() == 0) { |
| | | ShiroKit.getSubject().logout(); |
| | | model.addAttribute("tips", "该用户没有角色,无法登陆"); |
| | | return "/login.html"; |
| | | User user = userMapper.getByAccount(loginUser.getImUser()); |
| | | if (null == user) { |
| | | log.info("用户不存在"); |
| | | return null; |
| | | } |
| | | ShiroUser shiroUser = shiro.shiroUser(user); |
| | | List<Integer> roleList = shiroUser.getRoleList(); |
| | | if (roleList == null || roleList.size() == 0) { |
| | | roleList.add(0); |
| | | } |
| | | |
| | | List<Relation> relations = relationService.selectList(new EntityWrapper<Relation>().in("roleid", roleList)); |
| | | Set<Long> collect = relations.stream().map(Relation::getMenuid).collect(Collectors.toSet()); |
| | | List<Menu> menuList = menuService.selectBatchIds(collect); |
| | | List<MenuNode> menus = menuService.getMenusByRoleIds(roleList); |
| | | List<MenuNode> titles = MenuNode.buildTitle(menus); |
| | | titles = ApiMenuFilter.build(titles); |
| | | |
| | | |
| | | Integer id = ShiroKit.getUser().getId(); |
| | | User user = userService.selectById(id); |
| | | if (SinataUtil.isEmpty(user)){ |
| | | ShiroKit.getSubject().logout(); |
| | | model.addAttribute("tips", "请重新登录"); |
| | | return "/login.html"; |
| | | } |
| | | //设置上级ID |
| | | if (SinataUtil.isNotEmpty(user.getObjectId())){ |
| | | ShiroKit.getUser().setObjectId(user.getObjectId()); |
| | | } |
| | | //设置角色 |
| | | ShiroKit.getUser().setRoleType(user.getRoleType()); |
| | | shiroUser.setMenuIds(menuList.stream().map(Menu::getUrl).collect(Collectors.toList())); |
| | | |
| | | model.addAttribute("titles", titles); |
| | | |
| | |
| | | @RequestMapping(value = "/login", method = RequestMethod.GET) |
| | | public String login(Model model) { |
| | | model.addAttribute("number",0); |
| | | if (ShiroKit.isAuthenticated() || ShiroKit.getUser() != null) { |
| | | if (ShiroKit.isAuthenticated() || shiroExtUtil.getUser() != null) { |
| | | return REDIRECT + "/"; |
| | | } else { |
| | | return "/login.html"; |
| | |
| | | // 登录成功,错误次数值改0 |
| | | loginFailures.put(username, 0); |
| | | |
| | | ShiroUser shiroUser = ShiroKit.getUser(); |
| | | ShiroUser shiroUser = shiroExtUtil.getUser(); |
| | | super.getSession().setAttribute("shiroUser", shiroUser); |
| | | super.getSession().setAttribute("username", shiroUser.getAccount()); |
| | | |
| | |
| | | */ |
| | | @RequestMapping(value = "/logout", method = RequestMethod.GET) |
| | | public String logOut() { |
| | | LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), getIp())); |
| | | ShiroKit.getSubject().logout(); |
| | | deleteAllCookie(); |
| | | return REDIRECT + "/login"; |
| | | return REDIRECT + "/"; |
| | | } |
| | | } |