From 0d5daac9e879e5216827bff5bdff887d6c4d59b0 Mon Sep 17 00:00:00 2001 From: meng <154040976@qq.com> Date: Tue, 11 Dec 2018 21:20:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=20=20=20?= =?UTF-8?q?=20=20=20=20=20=20=20=20--=E5=90=AC=E4=B8=80=E9=A6=96=E6=AD=8C?= =?UTF-8?q?=EF=BC=8C=E5=9B=9E=E5=BF=86=E4=B8=80=E6=AE=B5=E5=BE=80=E4=BA=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/len/base/CurrentMenu.java | 3 + .../main/java/com/len/base/CurrentRole.java | 8 +- len-sys/src/main/java/com/len/App.java | 13 - .../com/len/controller/LoginController.java | 12 +- .../com/len/controller/RoleController.java | 5 +- .../java/com/len/core/quartz/JobTask.java | 2 +- .../java/com/len/core/shiro/LoginRealm.java | 48 +- .../com/len/service/impl/RoleServiceImpl.java | 7 +- .../len/service/impl/SysUserServiceImpl.java | 410 +++++++++--------- 9 files changed, 223 insertions(+), 285 deletions(-) delete mode 100644 len-sys/src/main/java/com/len/App.java diff --git a/len-core/src/main/java/com/len/base/CurrentMenu.java b/len-core/src/main/java/com/len/base/CurrentMenu.java index 5aec787..e527a87 100644 --- a/len-core/src/main/java/com/len/base/CurrentMenu.java +++ b/len-core/src/main/java/com/len/base/CurrentMenu.java @@ -57,4 +57,7 @@ public class CurrentMenu implements Serializable { this.menuType = menuType; this.num = num; } + + public CurrentMenu() { + } } diff --git a/len-core/src/main/java/com/len/base/CurrentRole.java b/len-core/src/main/java/com/len/base/CurrentRole.java index 23618c7..84d0fd6 100644 --- a/len-core/src/main/java/com/len/base/CurrentRole.java +++ b/len-core/src/main/java/com/len/base/CurrentRole.java @@ -2,6 +2,7 @@ package com.len.base; import java.io.Serializable; +import lombok.Data; import lombok.Getter; import lombok.Setter; import lombok.ToString; @@ -11,9 +12,7 @@ import lombok.ToString; * @date 2017/12/30. * @email 154040976@qq.com */ -@Getter -@Setter -@ToString +@Data public class CurrentRole implements Serializable { /** @@ -32,4 +31,7 @@ public class CurrentRole implements Serializable { this.roleName = roleName; this.remark = remark; } + + public CurrentRole() { + } } diff --git a/len-sys/src/main/java/com/len/App.java b/len-sys/src/main/java/com/len/App.java deleted file mode 100644 index cca333a..0000000 --- a/len-sys/src/main/java/com/len/App.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.len; - -/** - * Hello world! - * - */ -public class App -{ - public static void main( String[] args ) - { - System.out.println( "Hello World!" ); - } -} diff --git a/len-sys/src/main/java/com/len/controller/LoginController.java b/len-sys/src/main/java/com/len/controller/LoginController.java index 817e94f..731dbf0 100644 --- a/len-sys/src/main/java/com/len/controller/LoginController.java +++ b/len-sys/src/main/java/com/len/controller/LoginController.java @@ -15,7 +15,6 @@ import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.ExcessiveAttemptsException; import org.apache.shiro.authc.IncorrectCredentialsException; import org.apache.shiro.authc.UnknownAccountException; -import org.apache.shiro.session.Session; import org.apache.shiro.subject.Subject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @@ -40,10 +39,10 @@ import java.util.List; public class LoginController { @Autowired - private SysUserService userService; + private MenuService menuService; @Autowired - private MenuService menuService; + SysUserService userService; @GetMapping(value = "") public String loginInit() { @@ -66,7 +65,6 @@ public class LoginController { Subject sub = SecurityUtils.getSubject(); Boolean flag2 = sub.isRemembered(); boolean flag = sub.isAuthenticated() || flag2; - Session session = sub.getSession(); if (flag) { return "/main/main"; } @@ -84,11 +82,11 @@ public class LoginController { @ApiOperation(value = "/login", httpMethod = "POST", notes = "登录method") @PostMapping(value = "/login") public String login(SysUser user, Model model, String rememberMe, HttpServletRequest request) { - /*String codeMsg = (String) request.getAttribute("shiroLoginFailure"); + String codeMsg = (String) request.getAttribute("shiroLoginFailure"); if ("code.error".equals(codeMsg)) { model.addAttribute("message", "验证码错误"); return "/login"; - }*/ + } CustomUsernamePasswordToken token = new CustomUsernamePasswordToken(user.getUsername().trim(), user.getPassword(), "UserLogin"); Subject subject = ShiroUtil.getSubject(); @@ -96,6 +94,8 @@ public class LoginController { try { subject.login(token); if (subject.isAuthenticated()) { + userService.setMenuAndRoles(token.getUsername()); + token.getUsername(); return "redirect:/main"; } } catch (UnknownAccountException | IncorrectCredentialsException e) { diff --git a/len-sys/src/main/java/com/len/controller/RoleController.java b/len-sys/src/main/java/com/len/controller/RoleController.java index 80d16a7..a34e19b 100644 --- a/len-sys/src/main/java/com/len/controller/RoleController.java +++ b/len-sys/src/main/java/com/len/controller/RoleController.java @@ -16,9 +16,6 @@ import com.len.util.BeanUtil; import com.len.util.JsonUtil; import com.len.util.ReType; import io.swagger.annotations.ApiOperation; - -import java.util.List; - import org.apache.commons.lang3.StringUtils; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.springframework.beans.factory.annotation.Autowired; @@ -29,6 +26,8 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; +import java.util.List; + /** * @author zhuxiaomeng * @date 2017/12/19. diff --git a/len-sys/src/main/java/com/len/core/quartz/JobTask.java b/len-sys/src/main/java/com/len/core/quartz/JobTask.java index 37be416..398251c 100644 --- a/len-sys/src/main/java/com/len/core/quartz/JobTask.java +++ b/len-sys/src/main/java/com/len/core/quartz/JobTask.java @@ -97,7 +97,7 @@ public class JobTask { .withMisfireHandlingInstructionDoNothing(); CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(triggerKey) .withDescription(createTime).withSchedule(schedBuilder).build(); - Class clazz = null; + JobDetail jobDetail = scheduler.getJobDetail(jobKey); HashSet triggerSet = new HashSet<>(); triggerSet.add(trigger); diff --git a/len-sys/src/main/java/com/len/core/shiro/LoginRealm.java b/len-sys/src/main/java/com/len/core/shiro/LoginRealm.java index 8605299..7a09545 100644 --- a/len-sys/src/main/java/com/len/core/shiro/LoginRealm.java +++ b/len-sys/src/main/java/com/len/core/shiro/LoginRealm.java @@ -1,15 +1,9 @@ package com.len.core.shiro; -import com.alibaba.fastjson.JSONArray; import com.len.base.CurrentMenu; import com.len.base.CurrentRole; import com.len.base.CurrentUser; -import com.len.entity.SysMenu; -import com.len.entity.SysRole; import com.len.entity.SysUser; -import com.len.service.MenuService; -import com.len.service.RoleMenuService; -import com.len.service.RoleUserService; import com.len.service.SysUserService; import com.len.util.JWTUtil; import org.apache.commons.lang3.StringUtils; @@ -17,15 +11,12 @@ import org.apache.shiro.authc.*; import org.apache.shiro.authz.AuthorizationInfo; import org.apache.shiro.authz.SimpleAuthorizationInfo; import org.apache.shiro.realm.AuthorizingRealm; -import org.apache.shiro.session.Session; import org.apache.shiro.subject.PrincipalCollection; -import org.apache.shiro.subject.Subject; import org.apache.shiro.util.ByteSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; import java.util.Set; @@ -40,15 +31,6 @@ public class LoginRealm extends AuthorizingRealm { @Autowired private SysUserService userService; - @Autowired - private MenuService menuService; - - @Autowired - private RoleUserService roleUserService; - - @Autowired - private RoleMenuService roleMenuService; - /** * 获取授权 @@ -75,7 +57,7 @@ public class LoginRealm extends AuthorizingRealm { info.addRole(cRole.getId()); } for (CurrentMenu cMenu : cUser.getCurrentMenuList()) { - if (!StringUtils.isEmpty(cMenu.getPermission())){ + if (!StringUtils.isEmpty(cMenu.getPermission())) { info.addStringPermission(cMenu.getPermission()); } } @@ -94,7 +76,6 @@ public class LoginRealm extends AuthorizingRealm { @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { - UsernamePasswordToken upToken = (UsernamePasswordToken) authenticationToken; String username = (String) authenticationToken.getPrincipal(); SysUser s = null; try { @@ -104,33 +85,6 @@ public class LoginRealm extends AuthorizingRealm { } if (s == null) { throw new UnknownAccountException("账户密码不正确"); - } else { - CurrentUser currentUser = new CurrentUser(s.getId(), s.getUsername(), s.getAge(), s.getEmail(), s.getPhoto(), s.getRealName()); - Subject subject = ShiroUtil.getSubject(); - /**角色权限封装进去*/ - //根据用户获取菜单 - List menuList = new ArrayList<>(new HashSet<>(menuService.getUserMenu(s.getId()))); - JSONArray json = menuService.getMenuJsonByUser(menuList); - Session session = subject.getSession(); - session.setAttribute("menu", json); - CurrentMenu currentMenu = null; - List currentMenuList = new ArrayList<>(); - List roleList = new ArrayList<>(); - for (SysMenu m : menuList) { - currentMenu = new CurrentMenu(m.getId(), m.getName(), m.getPId(), m.getUrl(), m.getOrderNum(), m.getIcon(), m.getPermission(), m.getMenuType(), m.getNum()); - currentMenuList.add(currentMenu); - roleList.addAll(m.getRoleList()); - } - roleList = new ArrayList<>(new HashSet<>(roleList)); - List currentRoleList = new ArrayList<>(); - CurrentRole role = null; - for (SysRole r : roleList) { - role = new CurrentRole(r.getId(), r.getRoleName(), r.getRemark()); - currentRoleList.add(role); - } - currentUser.setCurrentRoleList(currentRoleList); - currentUser.setCurrentMenuList(currentMenuList); - session.setAttribute("curentUser", currentUser); } ByteSource byteSource = ByteSource.Util.bytes(username); return new SimpleAuthenticationInfo(username, s.getPassword(), byteSource, getName()); diff --git a/len-sys/src/main/java/com/len/service/impl/RoleServiceImpl.java b/len-sys/src/main/java/com/len/service/impl/RoleServiceImpl.java index 42771f9..6867a90 100644 --- a/len-sys/src/main/java/com/len/service/impl/RoleServiceImpl.java +++ b/len-sys/src/main/java/com/len/service/impl/RoleServiceImpl.java @@ -35,12 +35,7 @@ public class RoleServiceImpl extends BaseServiceImpl implements record=super.addValue(record,true); return roleMapper.insert(record); } - - /* @Override - public int insertSelective(SysRole record) { - return roleMapper.insertSelective(record); - }*/ - + @Override public SysRole selectByPrimaryKey(String id) { return roleMapper.selectByPrimaryKey(id); diff --git a/len-sys/src/main/java/com/len/service/impl/SysUserServiceImpl.java b/len-sys/src/main/java/com/len/service/impl/SysUserServiceImpl.java index 3389c32..fbb8d33 100644 --- a/len-sys/src/main/java/com/len/service/impl/SysUserServiceImpl.java +++ b/len-sys/src/main/java/com/len/service/impl/SysUserServiceImpl.java @@ -1,206 +1,204 @@ -package com.len.service.impl; - -import com.len.base.BaseMapper; -import com.len.base.impl.BaseServiceImpl; -import com.len.entity.SysRole; -import com.len.entity.SysRoleUser; -import com.len.entity.SysUser; -import com.len.exception.MyException; -import com.len.mapper.SysRoleUserMapper; -import com.len.mapper.SysUserMapper; -import com.len.service.RoleService; -import com.len.service.RoleUserService; -import com.len.service.SysUserService; -import com.len.util.Checkbox; -import com.len.util.JsonUtil; -import com.len.util.Md5Util; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.apache.commons.lang3.StringUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -/** - * @author zhuxiaomeng - * @date 2017/12/4. - * @email 154040976@qq.com - */ -@Service -public class SysUserServiceImpl extends BaseServiceImpl implements SysUserService { - - @Autowired - SysUserMapper sysUserMapper; - - @Autowired - SysRoleUserMapper sysRoleUserMapper; - - @Autowired - RoleService roleService; - - @Autowired - RoleUserService roleUserService; - @Override - public BaseMapper getMappser() { - return sysUserMapper; - } - - - @Override - public SysUser login(String username) { - return sysUserMapper.login(username); - } - - - - @Override - public int deleteByPrimaryKey(String id) { - return sysUserMapper.deleteByPrimaryKey(id); - } - - @Override - public int insert(SysUser record) { - return sysUserMapper.insert(record); - } - - @Override - public int insertSelective(SysUser record) { - - String pwd= Md5Util.getMD5(record.getPassword().trim(),record.getUsername().trim()); - record.setPassword(pwd); - return super.insertSelective(record); - } - - @Override - public SysUser selectByPrimaryKey(String id) { - return sysUserMapper.selectByPrimaryKey(id); - } - - @Override - public int updateByPrimaryKeySelective(SysUser record) { - return super.updateByPrimaryKeySelective(record); - } - - @Override - public int updateByPrimaryKey(SysUser record) { - return sysUserMapper.updateByPrimaryKey(record); - } - - @Override - public List selectByCondition(SysRoleUser sysRoleUser) { - return sysRoleUserMapper.selectByCondition(sysRoleUser); - } - - /** - * 分页查询 - * @param - * @return - */ - @Override - public List selectListByPage(SysUser sysUser) { - return sysUserMapper.selectListByPage(sysUser); - } - - @Override - public int count() { - return sysUserMapper.count(); - } - - @Override - public int add(SysUser user) { - //密码加密 - String pwd= Md5Util.getMD5(user.getPassword().trim(),user.getUsername().trim()); - user.setPassword(pwd); - return sysUserMapper.add(user); - } - - @Override - public JsonUtil delById(String id,boolean flag) { - if (StringUtils.isEmpty(id)) { - return JsonUtil.error("获取数据失败"); - } - JsonUtil j=new JsonUtil(); - try { - SysUser sysUser = selectByPrimaryKey(id); - if("admin".equals(sysUser.getUsername())){ - return JsonUtil.error("超管无法删除"); - } - SysRoleUser roleUser=new SysRoleUser(); - roleUser.setUserId(id); - int count=roleUserService.selectCountByCondition(roleUser); - if(count>0){ - return JsonUtil.error("账户已经绑定角色,无法删除"); - } - if (flag) { - //逻辑 - sysUser.setDelFlag(Byte.parseByte("1")); - updateByPrimaryKeySelective(sysUser); - } else { - //物理 - sysUserMapper.delById(id); - } - j.setMsg("删除成功"); - } catch (MyException e) { - j.setMsg("删除失败"); - j.setFlag(false); - e.printStackTrace(); - } - return j; - - } - - @Override - public int checkUser(String username) { - return sysUserMapper.checkUser(username); - } - - @Override - public List getUserRoleByJson(String id){ - List roleList=roleService.selectListByPage(new SysRole()); - SysRoleUser sysRoleUser =new SysRoleUser(); - sysRoleUser.setUserId(id); - List kList= selectByCondition(sysRoleUser); - System.out.println(kList.size()); - List checkboxList=new ArrayList<>(); - Checkbox checkbox=null; - for(SysRole sysRole:roleList){ - checkbox=new Checkbox(); - checkbox.setId(sysRole.getId()); - checkbox.setName(sysRole.getRoleName()); - for(SysRoleUser sysRoleUser1 :kList){ - if(sysRoleUser1.getRoleId().equals(sysRole.getId())){ - checkbox.setCheck(true); - } - } - checkboxList.add(checkbox); - } - return checkboxList; - } - - @Override - public int rePass(SysUser user) { - return sysUserMapper.rePass(user); - } - - @Override - public List getUserByRoleId(String roleId,int page,int limit) - { - Map map = new HashMap<>(); - map.put("roleId",roleId); - map.put("page",(page-1)*limit ); - map.put("limit",limit); - return sysUserMapper.getUserByRoleId(map); - } - - @Override - public int countUserByRoleId(String roleId,int page,int limit) - { - Map map = new HashMap<>(); - map.put("roleId",roleId); - map.put("page",(page-1)*limit ); - map.put("limit",limit); - return sysUserMapper.countUserByRoleId(map); - } -} +package com.len.service.impl; + +import com.alibaba.fastjson.JSONArray; +import com.len.base.BaseMapper; +import com.len.base.CurrentMenu; +import com.len.base.CurrentRole; +import com.len.base.CurrentUser; +import com.len.base.impl.BaseServiceImpl; +import com.len.core.shiro.ShiroUtil; +import com.len.entity.SysMenu; +import com.len.entity.SysRole; +import com.len.entity.SysRoleUser; +import com.len.entity.SysUser; +import com.len.exception.MyException; +import com.len.mapper.SysRoleUserMapper; +import com.len.mapper.SysUserMapper; +import com.len.service.MenuService; +import com.len.service.RoleService; +import com.len.service.RoleUserService; +import com.len.service.SysUserService; +import com.len.util.BeanUtil; +import com.len.util.Checkbox; +import com.len.util.JsonUtil; +import com.len.util.Md5Util; +import java.util.ArrayList; +import java.util.List; +import org.apache.commons.lang3.StringUtils; +import org.apache.shiro.session.Session; +import org.apache.shiro.subject.Subject; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; + +/** + * @author zhuxiaomeng + * @date 2017/12/4. + * @email 154040976@qq.com + */ +@Service +public class SysUserServiceImpl extends BaseServiceImpl implements SysUserService { + + @Autowired + SysUserMapper sysUserMapper; + + @Autowired + SysRoleUserMapper sysRoleUserMapper; + + @Autowired + RoleService roleService; + + @Autowired + RoleUserService roleUserService; + + @Autowired + MenuService menuService; + + private static final String ADMIN = "admin"; + + @Override + public BaseMapper getMappser() { + return sysUserMapper; + } + + + @Override + public SysUser login(String username) { + return sysUserMapper.login(username); + } + + + @Override + public int deleteByPrimaryKey(String id) { + return sysUserMapper.deleteByPrimaryKey(id); + } + + @Override + public int insert(SysUser record) { + return sysUserMapper.insert(record); + } + + @Override + public int insertSelective(SysUser record) { + + String pwd = Md5Util.getMD5(record.getPassword().trim(), record.getUsername().trim()); + record.setPassword(pwd); + return super.insertSelective(record); + } + + @Override + public SysUser selectByPrimaryKey(String id) { + return sysUserMapper.selectByPrimaryKey(id); + } + + @Override + public int updateByPrimaryKeySelective(SysUser record) { + return super.updateByPrimaryKeySelective(record); + } + + @Override + public int updateByPrimaryKey(SysUser record) { + return sysUserMapper.updateByPrimaryKey(record); + } + + @Override + public List selectByCondition(SysRoleUser sysRoleUser) { + return sysRoleUserMapper.selectByCondition(sysRoleUser); + } + + /** + * 分页查询 + * + * @param + * @return + */ + @Override + public List selectListByPage(SysUser sysUser) { + return sysUserMapper.selectListByPage(sysUser); + } + + @Override + public int count() { + return sysUserMapper.count(); + } + + @Override + public int add(SysUser user) { + //密码加密 + String pwd = Md5Util.getMD5(user.getPassword().trim(), user.getUsername().trim()); + user.setPassword(pwd); + return sysUserMapper.add(user); + } + + @Override + public JsonUtil delById(String id, boolean flag) { + if (StringUtils.isEmpty(id)) { + return JsonUtil.error("获取数据失败"); + } + JsonUtil j = new JsonUtil(); + try { + SysUser sysUser = selectByPrimaryKey(id); + if (ADMIN.equals(sysUser.getUsername())) { + return JsonUtil.error("超管无法删除"); + } + SysRoleUser roleUser = new SysRoleUser(); + roleUser.setUserId(id); + int count = roleUserService.selectCountByCondition(roleUser); + if (count > 0) { + return JsonUtil.error("账户已经绑定角色,无法删除"); + } + if (flag) { + //逻辑 + sysUser.setDelFlag(Byte.parseByte("1")); + updateByPrimaryKeySelective(sysUser); + } else { + //物理 + sysUserMapper.delById(id); + } + j.setMsg("删除成功"); + } catch (MyException e) { + j.setMsg("删除失败"); + j.setFlag(false); + e.printStackTrace(); + } + return j; + + } + + @Override + public int checkUser(String username) { + return sysUserMapper.checkUser(username); + } + + @Override + public List getUserRoleByJson(String id) { + List roleList = roleService.selectListByPage(new SysRole()); + SysRoleUser sysRoleUser = new SysRoleUser(); + sysRoleUser.setUserId(id); + List kList = selectByCondition(sysRoleUser); + + List checkboxList = new ArrayList<>(); + Checkbox checkbox; + for (SysRole sysRole : roleList) { + checkbox = new Checkbox(); + checkbox.setId(sysRole.getId()); + checkbox.setName(sysRole.getRoleName()); + for (SysRoleUser sysRoleUser1 : kList) { + if (sysRoleUser1.getRoleId().equals(sysRole.getId())) { + checkbox.setCheck(true); + break; + } + } + checkboxList.add(checkbox); + } + return checkboxList; + } + + @Override + public int rePass(SysUser user) { + return sysUserMapper.rePass(user); + } +}