[code][size=15]Subject subject = SecurityUtils.getSubject();[/code]
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
//提交认证
subject.login(token);
//Shiro进行认证
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
UsernamePasswordToken token = (UsernamePasswordToken)authcToken;
//获取用户信息
SysUserEntity user = new SysUserEntity();
user.setUsername(token.getUsername());
user = sysUserDao.selectOne(user);
//账号不存在
if(user == null) {
throw new UnknownAccountException("账号或密码不正确");
}
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), ByteSource.Util.bytes(user.getSalt()), getName());
return info;
}
[/size]
[code][size=15]//Shiro进行授权[/code]
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
//获取认证时候添加到SimpleAuthenticationInfo中的实例
SysUserEntity user = (SysUserEntity)principals.getPrimaryPrincipal();
Long userId = user.getUserId();
//查询用户所有权限
Set<String> permsSet = new HashSet<String>();
List<String> permsList = sysUserDao.queryAllPerms(userId);
for(String perms : permsList){
if(StringUtils.isBlank(perms)){
continue;
}
permsSet.addAll(Arrays.asList(perms.trim().split(",")));
}
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
info.setStringPermissions(permsSet);
return info;
}
[/size]
[code][size=15]<#if shiro.hasPermission("sys:add")>[/code]
<a class="btn btn-primary" @click="add">新增</a>
</#if>
<#if shiro.hasPermission("sys:del")>
<a class="btn btn-primary" @click="del">删除</a>
</#if>
[/size]
[code][size=15]@RequestMapping("/delete")[/code]
@RequiresPermissions("sys:del")
public R delete(long deptId){
//判断是否有子部门
List<Long> deptList = sysDeptService.queryDetpIdList(deptId);
if(deptList.size() > 0){
return R.error("请先删除子部门");
}
sysDeptService.deleteById(deptId);
return R.ok();
}
[/size]
作者:佚名来源: 网络
本文为 @ 21CTO 创作并授权 21CTO 发布,未经许可,请勿转载。
内容授权事宜请您联系 webmaster@21cto.com或关注 21CTO 公众号。
该文观点仅代表作者本人,21CTO 平台仅提供信息存储空间服务。