提交 16cc73f9 编写于 作者: N nicky

用户权限初步实现

上级 bb6f1860
package com.muses.taoshop.manager.service;
import com.muses.taoshop.manager.entity.SysRole;
import java.util.List;
/**
* <pre>
* 角色业务接口
* </pre>
*
* @author nicky
* @version 1.00.00
* <pre>
* 修改记录
* 修改后版本: 修改人: 修改日期: 2018.10.21 21:57 修改内容:
* </pre>
*/
public interface ISysRoleService {
/**
* 获取所有用户角色
* @return
*/
List<SysRole> listUserRole(int userId);
}
package com.muses.taoshop.manager.service;
import com.muses.taoshop.manager.entity.SysRole;
import com.muses.taoshop.manager.entity.SysUser;
import java.util.Set;
......@@ -45,4 +46,11 @@ public interface ISysUserService {
* @return
*/
SysUser getUserInfoByUsername(String username);
/**
* 通过用户id获取用户角色集合
* @param userId
* @return
*/
Set<SysRole> getUserRoles(int userId);
}
package com.muses.taoshop.manager.mapper;
import com.muses.taoshop.common.core.database.annotation.MybatisRepository;
import com.muses.taoshop.manager.entity.SysRole;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* <pre>
* 角色操作Mapper接口
* </pre>
*
* @author nicky
* @version 1.00.00
* <pre>
* 修改记录
* 修改后版本: 修改人: 修改日期: 2018.10.21 21:55 修改内容:
* </pre>
*/
@MybatisRepository
public interface SysRoleMapper {
List<SysRole> listUserRole(@Param("userId")int userId);
}
......@@ -13,4 +13,5 @@ package com.muses.taoshop.manager.service;
* </pre>
*/
public class MenuServiceImpl {
}
package com.muses.taoshop.manager.service;
import com.muses.taoshop.manager.entity.SysRole;
import com.muses.taoshop.manager.mapper.SysRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* <pre>
* 角色业务类
* </pre>
*
* @author nicky
* @version 1.00.00
* <pre>
* 修改记录
* 修改后版本: 修改人: 修改日期: 2018.10.21 21:59 修改内容:
* </pre>
*/
@Service
public class SysRoleServiceImpl implements ISysRoleService {
@Autowired
SysRoleMapper sysRoleMapper;
/**
* 获取所有用户角色
*
* @param userId
* @return
*/
@Override
public List<SysRole> listUserRole(int userId) {
return sysRoleMapper.listUserRole(userId);
}
}
......@@ -3,6 +3,7 @@ package com.muses.taoshop.manager.service;
import com.muses.taoshop.manager.entity.Operation;
import com.muses.taoshop.manager.entity.Permission;
import com.muses.taoshop.manager.entity.SysRole;
import com.muses.taoshop.manager.mapper.SysRoleMapper;
import com.muses.taoshop.manager.mapper.SysUserMapper;
import com.muses.taoshop.manager.entity.SysUser;
import com.muses.taoshop.manager.service.ISysUserService;
......@@ -12,6 +13,7 @@ import org.springframework.stereotype.Service;
import javax.management.relation.Role;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
......@@ -31,6 +33,8 @@ public class SysUserServiceImpl implements ISysUserService{
@Autowired
SysUserMapper sysUserMapper;
@Autowired
SysRoleMapper sysRoleMapper;
@Override
public SysUser getSysUser(String username , String password) {
......@@ -53,6 +57,18 @@ public class SysUserServiceImpl implements ISysUserService{
return roleStrs;
}
/**
* 通过用户id获取用户角色集合
* @param userId
* @return
*/
@Override
public Set<SysRole> getUserRoles(int userId) {
List<SysRole> roleList = sysRoleMapper.listUserRole(userId);
Set<SysRole> roles = new HashSet<>(roleList);
return roles;
}
/**
* 获取用户权限
* @param username
......@@ -61,7 +77,7 @@ public class SysUserServiceImpl implements ISysUserService{
public Set<String> getPermissions(String username) {
SysUser user = this.getUserInfoByUsername(username);
Set<SysRole> roles = user.getRoles();
/** 创建一个HashSet来存放角色权限信息 **/
/* 创建一个HashSet来存放角色权限信息 */
Set<String> permissions = new HashSet<String>();
for(SysRole r : roles) {
for (Permission p : r.getPermissions()){
......
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.muses.taoshop.manager.mapper.SysRoleMapper">
<sql id="BaseColumn">
roleId, roleName, roleDesc, role
</sql>
<select id="listUserRole" parameterType="Integer" resultType="SysRole">
SELECT
r.*,
ur.userId
FROM
sys_user_role ur
LEFT JOIN sys_role r
ON r.roleId = ur.roleId
WHERE ur.userId IN (#{userId})
</select>
</mapper>
\ No newline at end of file
......@@ -41,15 +41,15 @@ public class ShiroRealm extends AuthorizingRealm {
SysUser user = userService.getUserInfoByUsername(username);
/**检测是否有此用户 **/
/* 检测是否有此用户 */
if(user == null){
throw new UnknownAccountException();//没有找到账号异常
}
/**检验账号是否被锁定 **/
/* 检验账号是否被锁定 */
if(Boolean.TRUE.equals(user.getLocked())){
throw new LockedAccountException();//抛出账号锁定异常
}
/**AuthenticatingRealm使用CredentialsMatcher进行密码匹配**/
/* AuthenticatingRealm使用CredentialsMatcher进行密码匹配 */
if(null != username && null != password){
return new SimpleAuthenticationInfo(username, password, getName());
}else{
......
......@@ -7,6 +7,7 @@ import com.muses.taoshop.manager.entity.Menu;
import com.muses.taoshop.manager.entity.Permission;
import com.muses.taoshop.manager.entity.SysRole;
import com.muses.taoshop.manager.entity.SysUser;
import com.muses.taoshop.manager.service.ISysRoleService;
import com.muses.taoshop.manager.service.ISysUserService;
import com.muses.taoshop.manager.util.MenuTreeUtil;
import org.apache.commons.lang3.StringUtils;
......@@ -56,6 +57,8 @@ public class LoginController extends BaseController {
@Autowired
ISysUserService iSysUserService;
@Autowired
ISysRoleService iSysRoleService;
@RequestMapping(value = "/toLogin")
@GetMapping
......@@ -137,13 +140,13 @@ public class LoginController extends BaseController {
@RequestMapping(value="/toIndex")
public ModelAndView toMain() throws AuthenticationException{
ModelAndView mv = this.getModelAndView();
/**获取Shiro管理的Session**/
/* 获取Shiro管理的Session */
Subject subject = SecurityUtils.getSubject();
Session session = subject.getSession();
SysUser user = (SysUser)session.getAttribute(Constants.SESSION_USER);
if(user != null){
Set<SysRole> roles = user.getRoles();
Set<SysRole> roles = iSysUserService.getUserRoles(user.getId());
Set<Permission> permissions = new HashSet<Permission>();
if(!CollectionUtils.isEmpty(roles)) {
for (SysRole r : roles) {
......@@ -151,7 +154,7 @@ public class LoginController extends BaseController {
}
}
/**获取用户可以查看的菜单**/
/* 获取用户可以查看的菜单 */
List<Menu> menuList = new ArrayList<Menu>();
for(Permission p : permissions){
menuList.add(p.getMenu());
......@@ -160,12 +163,12 @@ public class LoginController extends BaseController {
MenuTreeUtil treeUtil = new MenuTreeUtil();
List<Menu> treemenus= treeUtil.menuList(menuList);
String json = JSON.toJSONString(treemenus);
json = json.replaceAll("menuId","id").replaceAll("parentId","pId").
replaceAll("menuName","name").replaceAll("hasSubMenu","checked");
// String json = JSON.toJSONString(treemenus);
//
// json = json.replaceAll("menuId","id").replaceAll("parentId","pId").
// replaceAll("menuName","name").replaceAll("hasSubMenu","checked");
mv.addObject("menus",json);
mv.addObject("menus",treemenus);
mv.setViewName("admin/frame/index");
}else{
//会话失效,返回登录界面
......@@ -175,7 +178,25 @@ public class LoginController extends BaseController {
return mv;
}
/**
* 注销登录
* @return
*/
@RequestMapping(value="/logout")
public ModelAndView logout(){
ModelAndView mv = this.getModelAndView();
/* Shiro管理Session */
Subject sub = SecurityUtils.getSubject();
Session session = sub.getSession();
session.removeAttribute(Constants.SESSION_USER);
session.removeAttribute(Constants.SESSION_SECURITY_CODE);
/* Shiro销毁登录 */
Subject subject = SecurityUtils.getSubject();
subject.logout();
/* 返回后台系统登录界面 */
mv.setViewName("login");
return mv;
}
}
<div class="sider-bar-bk" xmlns:th="http://www.w3.org/1999/xhtml">
<div class="sider-bar-bk" xmlns:th="http://www.thymeleaf.org">
<div class="sider-bar-hd">
<i class="iconfont">&#xe616;</i>
<span>云服务</span>
</div>
<ul class="sider-nav">
<li class="sider-nav-item current">
<h3>
<a href="javascript:;">
<i class="iconfont">&#xe610;</i>
<span>订单管理</span>
<i class="iconfont arrow-right">&#xe611;</i>
<i class="iconfont arrow-down">&#xe615;</i>
</a>
</h3>
<ul class="sider-nav-s">
<li class="current"><a th:href="@{/admin/order/api/toOrder}">订单管理</a></li>
<li><a href="order-detail.html">订单详情</a></li>
<li><a href="order-handle.html">订单处理</a></li>
</ul>
</li>
<li class="sider-nav-item">
<h3>
<a href="javascript:;">
<i class="iconfont">&#xe613;</i>
<span>商品管理</span>
<i class="iconfont arrow-right">&#xe611;</i>
<i class="iconfont arrow-down">&#xe615;</i>
</a>
</h3>
<ul class="sider-nav-s">
<li><a href="#">品类管理</a></li>
<li><a href="#">商品管理</a></li>
</ul>
</li>
<li class="sider-nav-item">
<li class="sider-nav-item" th:each="menu : ${menus}">
<h3>
<a href="javascript:;">
<i class="iconfont">&#xe613;</i>
<span>系统管理</span>
<span th:text="${menu.menuName}">系统管理</span>
<i class="iconfont arrow-right">&#xe611;</i>
<i class="iconfont arrow-down">&#xe615;</i>
</a>
</h3>
<ul class="sider-nav-s">
<li><a href="#">系统用户管理</a></li>
<li><a href="#">系统角色管理</a></li>
</ul>
</li>
<li class="sider-nav-item">
<h3>
<a href="javascript:;">
<i class="iconfont">&#xe613;</i>
<span>云检测预警</span>
<i class="iconfont arrow-right">&#xe611;</i>
<i class="iconfont arrow-down">&#xe615;</i>
</a>
</h3>
<ul class="sider-nav-s">
<li><a href="#">WEB网站检测服务</a></li>
</ul>
</li>
</ul>
</div>
\ No newline at end of file
......@@ -47,7 +47,7 @@
<div class="form-kv clearfix">
<div class="form-kv-label">上次登录:</div>
<div class="form-content">
<span th:text="${{session.sessionUser.lastLogin}}">2018-08-08</span>
<span th:text="${session.sessionUser.lastLogin}">2018-08-08</span>
</div>
</div>
</div>
......
......@@ -373,6 +373,10 @@
$("body,html").stop().animate({scrollTop:$(".detail .detail-main").eq(index).offset().top},600);
});
});
/* 跳转到添加购物车 */
function toAddCart() {
}
</script>
<!--//tabs-->
</div>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册