Custom Realm Profile
Generally Custom Realm is now inherited AuthorizingRealm.
Realm of inheritance: AuthorizingRealm-> AuthenticatingRealm-> CachingRealm-> Realm.
step
- Create a class that inherits AuthorizingRealm.
- Rewrite authorization method doGetAuthorizationInfo.
- Rewrite authentication method doGetAuthenticationInfo.
Override the method described
- When the user logs of calls doGetAuthenticationInfo.
- When checking the permissions will be called: doGetAuthorizationInfo.
Entity Object Description
- UsernamePasswordToken: correspondence there is token shiro's Principal and Credential, inheritance UsernamePasswordToken- "HostAuthenticationToken-" AuthenticationToken
- SimpleAuthorizationInfo: information on behalf of the user role permissions
- SimpleAuthenticationInfo: on behalf of the user authentication information
Code combat
Custom CustomRealm
/** * @ClassName: CustomRealm * 自定义Realm * @Description: * @Author: Coding_wxb * @Date 2019.08.02 2:24 */ public class CustomRealm extends AuthorizingRealm { /** *模拟用户 **/ private final Map<String,String> userInfoMap = new HashMap<>(); { userInfoMap.put("woxbwo","123"); userInfoMap.put("zbbiex","456"); } /** *模拟role -> permission **/ private final Map<String, Set<String>> permissionMap = new HashMap<>(); { Set<String> set1 = new HashSet<>(); Set<String> set2 = new HashSet<>(); set1.add("video:find"); set1.add("video:buy"); set2.add("video:add"); set2.add("video:delete"); permissionMap.put("woxbwo",set1); permissionMap.put("zbbiex",set2); } /** *模拟user -> role **/ private final Map<String,Set<String>> roleMap = new HashMap<>();{ Set<String> set1 = new HashSet<>(); Set<String> set2 = new HashSet<>(); set1.add("role1"); set1.add("role2"); set2.add("root"); roleMap.put("woxbwo",set1); roleMap.put("zbbiex",set2); * This method is invoked in check permissions/ ** } *@description: *@params: [principals] *@return: org.apache.shiro.authz.AuthorizationInfo **/ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { System.out.println("权限 doGetAuthorizationInfo"); String name = (String)principals.getPrimaryPrincipal(); Set<String> permissions = getPermissionsByNameFromDB(name); Set<String> roles = getRolesByNameFromDB(name); SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo(); simpleAuthorizationInfo.setRoles (Roles); simpleAuthorizationInfo.setStringPermissions (Permissions); return simpleAuthorizationInfo; } / ** * @ Description: * This method is invoked when the user authentication * @ the params: [token] * @return : the org.apache. shiro.authc.AuthenticationInfo * * / @Override protected AuthenticationInfo doGetAuthenticationInfo (AuthenticationToken token) throws of AuthenticationException { System.out.println ( "authentication doGetAuthenticationInfo" ); // Get status information from the token, token representing the user information input String name =(String) token.getPrincipal (); // simulation taken from the database password String pwd = getPwdByUserNameFromDB (name); IF (StringUtils.isEmpty (pwd)) { return null ; } SimpleAuthenticationInfo simpleAuthenticationInfo = new new SimpleAuthenticationInfo (name, pwd, the this .getName ()); return simpleAuthenticationInfo; } / ** * analog set from the database to obtain the user roles * @param name * @return * / Private the set <String>getRolesByNameFromDB (String name) { return roleMap.get (name); } / ** * analog fetched from the database a set of permissions * @param name * @return * / Private the Set <String> getPermissionsByNameFromDB (String name) { return permissionMap.get ( name); } / ** * analog obtain the password from the database * @param name * @return * / Private String getPwdByUserNameFromDB (String name) { return userInfoMap.get (name); } }
Create a test class
/** * @ClassName: CustomRealmTest * @Description: * @Author: Coding_wxb * @Date 2019.08.02 2:50 */ public class CustomRealmTest { private CustomRealm customRealm = new CustomRealm(); private DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager(); @Before public void init(){ //构建环境 defaultSecurityManager.setRealm(customRealm); SecurityUtils.setSecurityManager(defaultSecurityManager); } @Test public voidtestAuthentication () { // Get the current operation of the body the Subject Subject = SecurityUtils.getSubject (); // user account password entered UsernamePasswordToken usernamePasswordToken = new new UsernamePasswordToken ( "woxbwo", "123" ); subject.login (usernamePasswordToken); / / log System.out.println ( "authentication result:" + subject.isAuthenticated ()); // get the properties marked body System.out.println ( "getPrincipal =" + subject.getPrincipal ()); subject.checkRole ( "role1" ); System.out.println ( "is there a corresponding role:" + subject.hasRole ( "role1"role1")); System.out.println ( "if there is a corresponding authority:" + subject.isPermitted ( "Video: the Add" )); } }
Test Results
Certification doGetAuthenticationInfo authentication result: to true getPrincipal = woxbwo rights doGetAuthorizationInfo rights doGetAuthorizationInfo whether there is a corresponding role: to true authority doGetAuthorizationInfo whether there is a corresponding authority: false