# 权限注解
# 权限概述
系统框架集成了 Shiro权限控制
实现了颗粒度权限管理,在用户登录获取权限授权成功后即可获得系统所给予的访问权限,权限方法的控制需要在控制器中给具体的方法上加上权限控制,如 @RequiresPermissions("sys:level:edit")
此时如果拥有编辑的权限即可正常访问该方法,否则无法进行有效访问,会返回错误给前端;
说明
鉴于Shiro的权限认证机制,用户登录授权成功并获得用户信息,当认证成功访问需要权限验证的方法时(访问带权限验证的注解@RequiresPermissions("xxx")
)会自动进入授权方法中;
# 身份认证
/**
* 身份认证
*
* @param authenticationToken
* @return
* @throws AuthenticationException
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
System.out.println("进行身份认证中...");
// 获取用户名
UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
String username = token.getUsername();
String password = "";
if (token.getPassword() != null) {
password = new String(token.getPassword());
}
User user = null;
try {
user = loginService.login(username, password);
} catch (CaptchaException e) {
throw new AuthenticationException(e.getMessage(), e);
} catch (UserNotExistsException e) {
throw new UnknownAccountException(e.getMessage(), e);
} catch (IncorrectCredentialsException e) {
throw new IncorrectCredentialsException(e.getMessage(), e);
} catch (LockedAccountException e) {
throw new LockedAccountException(e.getMessage(), e);
} catch (ExcessiveAttemptsException e) {
throw new ExcessiveAttemptsException(e.getMessage(), e);
} catch (Exception e) {
log.info("对用户[" + username + "]进行登录验证..验证未通过{}", e.getMessage());
throw new AuthenticationException(e.getMessage(), e);
}
//进行验证
SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
user, //用户名
user.getPassword(), //密码
ByteSource.Util.bytes(""), //设置盐值
getName()
);
return authenticationInfo;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 授权权限
/**
* 授权权限
*
* @param principalCollection
* @return
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
System.out.println("认证成功进行授权中...");
User user = (User) principalCollection.getPrimaryPrincipal();
//添加角色和权限
SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
Integer userId = user.getId();
if (userId.equals(1)) {
//管理员拥有所有权限
simpleAuthorizationInfo.addStringPermission("*:*");
} else {
List<Menu> menuList = menuService.getAuthPermissionList(userId);
if (!menuList.isEmpty()) {
for (Menu menu : menuList) {
if (StringUtils.isEmpty(menu.getPermission())) {
continue;
}
simpleAuthorizationInfo.addStringPermission(menu.getPermission());
}
}
}
return simpleAuthorizationInfo;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
特别说明
上述 授权权限
方法中可以看出 ID=1的管理员
拥有所有方法的访问权限,其他用户则根据系统赋予的权限进行统一的授予权限