第二节,自定义Realm

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_32534855/article/details/83785182

自定义Realm

/**
 * @anthor DencyCheng
 * @date 2018/11/4 0004
 */
public class CustormRealm extends AuthorizingRealm {

    Map<String,String> userMap = new HashMap<String, String>(10);

    {
        userMap.put("dency","123456");
        userMap.put("admin","123456");
    }
    /**
     * 授权(查看是否有权限)
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

        //1.获取用户名
        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;
    }

    private Set<String> getPermissionsByUserName(String userName) {
        Set<String> permissions = new HashSet<>();
        permissions.add("user:add");
        permissions.add("user:remove");
        return  permissions;
    }

    private Set<String> getRolesByUserName(String userName) {
        Set<String> roles = new HashSet<>();
        roles.add("admin");
        roles.add("user");
        return roles;
    }

    /**
     * 认证(登录)
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        //1.获取用户名
        String userName = (String) authenticationToken.getPrincipal();

        //2.查询用户是否存在
        String password = getPasswordByUserName(userName);
        if(password == null){
            return  null;
        }

        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(userName,password,userName);

         return authenticationInfo;
    }

    /**
     * 模拟数据库
     * @param userName
     * @return
     */
    private String getPasswordByUserName(String userName) {
        return userMap.get(userName);
    }
}

测试

	        //1. 构建SecurityManager 环境
		DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
		//自定义Realm
                CustormRealm custormRealm = new CustormRealm();
		defaultSecurityManager.setRealm(custormRealm);

		//2. 获得主体
		SecurityUtils.setSecurityManager(defaultSecurityManager);
		Subject subject = SecurityUtils.getSubject();

		//3. 认证(登录)
		UsernamePasswordToken token = new UsernamePasswordToken("dency","123456");
		subject.login(token);
		System.out.println("isAuthenticated:"+subject.isAuthenticated());

		//4. 登出
		subject.logout();
		System.out.println("isAuthenticated:"+subject.isAuthenticated());

猜你喜欢

转载自blog.csdn.net/qq_32534855/article/details/83785182