添加SimpleAuthorizingRealm

添加一个类net.wendal.nutzbook.shiro.realm.SimpleAuthorizingRealm

  1. package net.wendal.nutzbook.shiro.realm;
  2. import org.apache.shiro.authc.AuthenticationException;
  3. import org.apache.shiro.authc.AuthenticationInfo;
  4. import org.apache.shiro.authc.AuthenticationToken;
  5. import org.apache.shiro.authc.LockedAccountException;
  6. import org.apache.shiro.authc.SimpleAccount;
  7. import org.apache.shiro.authc.credential.CredentialsMatcher;
  8. import org.apache.shiro.authz.AuthorizationException;
  9. import org.apache.shiro.authz.AuthorizationInfo;
  10. import org.apache.shiro.authz.SimpleAuthorizationInfo;
  11. import org.apache.shiro.cache.CacheManager;
  12. import org.apache.shiro.realm.AuthorizingRealm;
  13. import org.apache.shiro.subject.PrincipalCollection;
  14. import org.nutz.dao.Dao;
  15. import org.nutz.integration.shiro.SimpleShiroToken;
  16. import org.nutz.mvc.Mvcs;
  17. import net.wendal.nutzbook.bean.Permission;
  18. import net.wendal.nutzbook.bean.Role;
  19. import net.wendal.nutzbook.bean.User;
  20. public class SimpleAuthorizingRealm extends AuthorizingRealm {
  21. protected Dao dao; // ShiroFilter先于NutFilter初始化化,所以无法使用注入功能
  22. protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
  23. // null usernames are invalid
  24. if (principals == null) {
  25. throw new AuthorizationException("PrincipalCollection method argument cannot be null.");
  26. }
  27. int userId = (Integer) principals.getPrimaryPrincipal();
  28. User user = dao().fetch(User.class, userId);
  29. if (user == null)
  30. return null;
  31. if (user.isLocked())
  32. throw new LockedAccountException("Account [" + user.getName() + "] is locked.");
  33. SimpleAuthorizationInfo auth = new SimpleAuthorizationInfo();
  34. user = dao().fetchLinks(user, null);
  35. if (user.getRoles() != null) {
  36. dao().fetchLinks(user.getRoles(), null);
  37. for (Role role : user.getRoles()) {
  38. auth.addRole(role.getName());
  39. if (role.getPermissions() != null) {
  40. for (Permission p : role.getPermissions()) {
  41. auth.addStringPermission(p.getName());
  42. }
  43. }
  44. }
  45. }
  46. if (user.getPermissions() != null) { // 特许/临时分配的权限
  47. for (Permission p : user.getPermissions()) {
  48. auth.addStringPermission(p.getName());
  49. }
  50. }
  51. return auth;
  52. }
  53. protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
  54. SimpleShiroToken upToken = (SimpleShiroToken) token;
  55. // upToken.getPrincipal() 的返回值就是SimpleShiroToken构造方法传入的值
  56. // 可以是int也可以是User类实例,或任何你希望的值,自行处理一下就好了
  57. User user = dao().fetch(User.class, ((Integer)upToken.getPrincipal()).longValue());
  58. if (user == null)
  59. return null;
  60. if (user.isLocked())
  61. throw new LockedAccountException("Account [" + user.getName() + "] is locked.");
  62. return new SimpleAccount(user.getId(), user.getPassword(), getName());
  63. }
  64. /**
  65. * 覆盖父类的验证,直接pass. 在shiro内做验证的话, 出错了都不知道哪里错
  66. */
  67. protected void assertCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) throws AuthenticationException {
  68. }
  69. public SimpleAuthorizingRealm() {
  70. this(null, null);
  71. }
  72. public SimpleAuthorizingRealm(CacheManager cacheManager, CredentialsMatcher matcher) {
  73. super(cacheManager, matcher);
  74. setAuthenticationTokenClass(SimpleShiroToken.class); // 非常非常重要,与SecurityUtils.getSubject().login是对应关系!!!
  75. }
  76. public SimpleAuthorizingRealm(CacheManager cacheManager) {
  77. this(cacheManager, null);
  78. }
  79. public SimpleAuthorizingRealm(CredentialsMatcher matcher) {
  80. this(null, matcher);
  81. }
  82. public Dao dao() {
  83. if (dao == null) {
  84. dao = Mvcs.ctx().getDefaultIoc().get(Dao.class, "dao");
  85. return dao;
  86. }
  87. return dao;
  88. }
  89. public void setDao(Dao dao) {
  90. this.dao = dao;
  91. }
  92. }

关键点

  • 这个类也存在于shiro插件中,但切勿直接引用, 因为这个类与实际的权限模型(User-Role-Permission)紧密相关,提供一个通用实现不现实.
    *