自定义Realm
新建一个MyRealm类,继承Shiro框架的AuthorizingRealm类,并实现默认的两个方法:
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| package com.ledao.realm;
import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.AuthenticationInfo; import org.apache.shiro.authc.AuthenticationToken; import org.apache.shiro.authc.SimpleAuthenticationInfo; 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 java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set;
public class MyRealm extends AuthorizingRealm {
@Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { String userName = (String) principalCollection.getPrimaryPrincipal(); Set<String> roles = getRolesByUserName(userName); Set<String> permissions = getPermissionsByUserName(userName);
SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo(); simpleAuthorizationInfo.setRoles(roles); simpleAuthorizationInfo.setStringPermissions(permissions); return simpleAuthorizationInfo; }
@Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { String userName = (String) authenticationToken.getPrincipal(); String password = getPasswordByUserName(userName); if (password == null) { return null; } SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(userName, password, "myRealm"); return simpleAuthenticationInfo; }
Map<String, String> userMap = new HashMap<>(16);
{ userMap.put("ledao", "123456"); super.setName("myRealm"); }
private String getPasswordByUserName(String userName) { return userMap.get(userName); }
private Set<String> getRolesByUserName(String userName) { Set<String> roles = new HashSet<>(); roles.add("admin"); roles.add("teacher"); return roles; }
private Set<String> getPermissionsByUserName(String userName) { Set<String> permissions = new HashSet<>(); permissions.add("添加学生"); permissions.add("添加教师"); return permissions; } }
|
测试类
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 45 46 47 48 49 50 51 52 53
| package com.ledao;
import com.ledao.realm.MyRealm; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.authz.AuthorizationException; import org.apache.shiro.mgt.DefaultSecurityManager; import org.apache.shiro.subject.Subject;
public class Test {
public static void main(String[] args) { MyRealm myRealm = new MyRealm(); DefaultSecurityManager securityManager = new DefaultSecurityManager(); securityManager.setRealm(myRealm); SecurityUtils.setSecurityManager(securityManager); Subject subject = SecurityUtils.getSubject(); UsernamePasswordToken token = new UsernamePasswordToken("ledao", "123456"); try { subject.login(token); System.out.println(subject.isAuthenticated() ? "登录成功" : "登录失败"); try { subject.checkRoles("admin", "teacher"); try { subject.checkPermissions("添加学生", "添加教师"); } catch (AuthorizationException e) { e.printStackTrace(); System.out.println("登录的用户与检查的权限不相符!!"); } } catch (AuthorizationException e) { e.printStackTrace(); System.out.println("登录的用户与检查的角色不相符!!"); } } catch (AuthenticationException e) { e.printStackTrace(); System.out.println("用户名或密码错误!!"); } } }
|