public class Permission {
private Integer id;
private Integer parent_id;
private String parent_ids;
private String permission;
private String resource_type;
private String url;
private String name;
private String available;
private Set roles = new HashSet<>();
public class Role {
private Integer id;
private String role;
private String description;
private String available;
private Set users = new HashSet<>();
private Set permissions = new HashSet<>();
public class User {
private Integer uid;
private String username;
private String password;
private String name;
private String id_card_num;
private String state;
private Set roles = new HashSet<>();
对象关系说明:
1、一个User对应多个Role
2、一个Permission对应多个Role
3、一个Role对应多个User和多个Permission
2.3、mapper接口创建:
UserMapper:通过用户名获取用户操作,用户添加操作,用户删除操作
RoleMapper:通过用户id获取用户所有角色操作
PermissionMapper:通过用户所有角色获取用户所有权限操作
@Mapper
public interface UserMapper {
User findByUserName(String userName);
int insert(User user);
int del(@Param("username") String username);
}
@Mapper
public interface RoleMapper {
Set findRolesByUserId(@Param("uid") Integer uid);
}
@Mapper
public interface PermissionMapper {
Set findPermissionsByRoleId(@Param("roles") Set roles);
}
获取用户资料操作
1、通过shiro登录成功
2、通过用户名称调用UserMapper获取用户信息
3、通过用户信息调用RoleMapper获取用户角色列表
4、通过用户角色列表调用PermissionMapper获取用户权限列表
2.4、mapper文件
mapper接口的具体实现
PermissionMapper.xml
RoleMapper.xml
UserMapper.xml
SELECT
LAST_INSERT_ID()
insert into user_info
uid,
username,
password,
`name`,
id_card_num,
state,
#{uid},
#{username},
#{password},
#{name},
#{id_card_num},
#{state},
DELETE FROM user_info WHERE username = #{username}
2.5、service层:UserService
通过调用mapper接口进行具体操作
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public int insert(User user) {
return userMapper.insert(user);
}
public int del(String username) {
return userMapper.del(username);
}
}
三、权限相关配置:ShiroRealm,ShiroConfig
ShiroRealm:shiro中通过Realm来获取用户的信息
ShiroConfig:定义访问规则
package com.shiro.realm;
import com.shiro.bean.Permission;
import com.shiro.bean.Role;
import com.shiro.bean.User;
import com.shiro.mapper.PermissionMapper;
import com.shiro.mapper.RoleMapper;
import com.shiro.mapper.UserMapper;
import org.apache.shiro.SecurityUtils;
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.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Set;
/**
* @author brusion
* @date 2018/10/18
* @description: Realm用于获取用户数据,组装后给shiro使用
*/
public class ShiroRealm extends AuthorizingRealm {
@Autowired
private UserMapper userMapper;
@Autowired
private RoleMapper roleMapper;
@Autowired
private PermissionMapper permissionMapper;
//验证当前登录的账号是否可用
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
UsernamePasswordToken tokenUser = (UsernamePasswordToken) token;
String username = tokenUser.getUsername();
String password = new String(tokenUser.getPassword());
User user = userMapper.getByName(username);
if (user == null || !password.equals(user.getPassword())) {
throw new UnknownAccountException("用户或密码为空");
}
if ("1".equals(user.getState())) {
throw new LockedAccountException("账号被锁定");
}
return new SimpleAuthenticationInfo(user, user.getPassword(), getName());
}
//获取当账号对应的权限,角色信息
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
User user = (User) SecurityUtils.getSubject().getPrincipal();
Integer uid = user.getUid();
Set roles = roleMapper.getRoleById(uid);
for (Role role : roles) {
info.addRole(role.getRole());
}
Set permissions = permissionMapper.getPermissionByRole(roles);
for (Permission per : permissions) {
info.addStringPermission(per.getPermission());
}
return info;
}
}
如果在使用JAXB把xml文件unmarshal成vo(XSD自动生成的vo)时碰到如下错误:
org.xml.sax.saxparseexception : premature end of file
很有可能时你直接读取文件为inputstream,然后将inputstream作为构建unmarshal需要的source参数。InputSource inputSource = new In
servlet 搞java web开发的人一定不会陌生,而且大家还会时常用到它。
下面是java官方网站上对servlet的介绍: java官网对于servlet的解释 写道
Java Servlet Technology Overview Servlets are the Java platform technology of choice for extending and enha
这两天学到事务管理这一块,结合到之前的terasoluna框架,觉得书本上讲的还是简单阿。我就把我从书本上学到的再结合实际的项目以及网上看到的一些内容,对声明式事务管理做个整理吧。我看得Spring in Action第二版中只提到了用TransactionProxyFactoryBean和<tx:advice/>,定义注释驱动这三种,我承认后两种的内容很好,很强大。但是实际的项目当中
1)nosql数据库主要由以下特点:非关系型的、分布式的、开源的、水平可扩展的。
1,处理超大量的数据
2,运行在便宜的PC服务器集群上,
3,击碎了性能瓶颈。
1)对数据高并发读写。
2)对海量数据的高效率存储和访问。
3)对数据的高扩展性和高可用性。
redis支持的类型:
Sring 类型
set name lijie
get name lijie
set na
在多节点的系统中,如何实现分布式锁机制,其中用redis来实现是很好的方法之一,我们先来看一下jedis包中,有个类名BinaryJedis,它有个方法如下:
public Long setnx(final byte[] key, final byte[] value) {
checkIsInMulti();
client.setnx(key, value);
ret