1.Realm的实现

    1. package com.cedric.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.SimpleAuthenticationInfo;
    6. import org.apache.shiro.authz.AuthorizationInfo;
    7. import org.apache.shiro.authz.SimpleAuthorizationInfo;
    8. import org.apache.shiro.realm.AuthorizingRealm;
    9. import org.apache.shiro.subject.PrincipalCollection;
    10. import org.apache.shiro.util.ByteSource;
    11. /**
    12. *
    13. * 使用自定义realm 加上 MD5 + salt + hash
    14. */
    15. public class CustomerMd5Realm extends AuthorizingRealm {
    16. //授权
    17. @Override
    18. protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    19. String primaryPrincipal = (String) principalCollection.getPrimaryPrincipal();
    20. System.out.println("身份信息:" + primaryPrincipal);
    21. //根据身份信息 用户名 获取当前用户的角色信息,以及权限信息
    22. SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
    23. //将数据库中查询角色信息赋值给权限对象
    24. simpleAuthorizationInfo.addRole("admin");
    25. simpleAuthorizationInfo.addRole("user");
    26. //将数据库中查询权限信息赋值给权限对象
    27. simpleAuthorizationInfo.addStringPermission("user:*:01");
    28. simpleAuthorizationInfo.addStringPermission("product:create");
    29. return simpleAuthorizationInfo;
    30. }
    31. @Override
    32. protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
    33. //获取身份信息
    34. String principal = (String) authenticationToken.getPrincipal();
    35. //根据用户名查询数据库
    36. if ("Jack".equals(principal)){
    37. //参数1:数据库用户名 参数2:数据库md5+salt之后的密码 参数3:注册时的随机盐 参数4:realm的名字
    38. return new SimpleAuthenticationInfo(principal,
    39. "0c3fd70bf5939dccaad22d27bfa46298",
    40. ByteSource.Util.bytes("0X*P"),
    41. this.getName());
    42. }
    43. return null;
    44. }
    45. }

    2.授权

    1. package com.cedric;
    2. import com.cedric.realm.CustomerMd5Realm;
    3. import org.apache.shiro.SecurityUtils;
    4. import org.apache.shiro.authc.IncorrectCredentialsException;
    5. import org.apache.shiro.authc.UnknownAccountException;
    6. import org.apache.shiro.authc.UsernamePasswordToken;
    7. import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
    8. import org.apache.shiro.mgt.DefaultSecurityManager;
    9. import org.apache.shiro.subject.Subject;
    10. import java.util.Arrays;
    11. public class TestCustomerMd5RealmAuthenicator {
    12. public static void main(String[] args) {
    13. //创建安全管理器
    14. DefaultSecurityManager securityManager = new DefaultSecurityManager();
    15. //注入Realm
    16. CustomerMd5Realm realm = new CustomerMd5Realm();
    17. //设置realm使用hash凭证匹配器
    18. HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
    19. //使用算法
    20. credentialsMatcher.setHashAlgorithmName("md5");
    21. //散列次数
    22. credentialsMatcher.setHashIterations(1024);
    23. realm.setCredentialsMatcher(credentialsMatcher);
    24. securityManager.setRealm(realm);
    25. //将安全工具类注入安全工具
    26. SecurityUtils.setSecurityManager(securityManager);
    27. //通过安全工具获取subject
    28. Subject subject = SecurityUtils.getSubject();
    29. //认证
    30. UsernamePasswordToken token = new UsernamePasswordToken("Jack","123");
    31. try {
    32. subject.login(token);
    33. System.out.println("登录成功");
    34. } catch (UnknownAccountException e){
    35. e.printStackTrace();
    36. System.out.println("用户名错误");
    37. } catch (IncorrectCredentialsException e){
    38. e.printStackTrace();
    39. System.out.println("密码错误");
    40. }
    41. //授权
    42. if (subject.isAuthenticated()){
    43. //基于角色权限控制
    44. System.out.println(subject.hasRole("admin"));
    45. //基于多角色条件控制
    46. System.out.println(subject.hasAllRoles(Arrays.asList("admin", "user")));
    47. //是否具有其中一个角色
    48. boolean[] booleans = subject.hasRoles(Arrays.asList("admin", "super","user"));
    49. for (boolean b : booleans){
    50. System.out.println(b);
    51. }
    52. System.out.println("===================");
    53. //基于权限字符串的访问控制 资源标识符:操作:资源类型
    54. System.out.println("权限:" + subject.isPermitted("user:update:01"));
    55. System.out.println("权限:" + subject.isPermitted("product:create:02"));
    56. //分别具有哪些权限
    57. boolean[] permitted = subject.isPermitted("user:*:01", "order:*:10");
    58. for (boolean b : permitted){
    59. System.out.println(b);
    60. }
    61. //同时具有哪些权限
    62. boolean permittedAll = subject.isPermittedAll("user:*:01","product:create:*");
    63. System.out.println(permittedAll);
    64. }
    65. }
    66. }