资讯专栏INFORMATION COLUMN

Apache Shiro 配置 LDAP 验证

zhiwei / 2571人阅读

摘要:通常在根据进行身份验证时一般进行以下三步利用一个用户的用户名和密码绑定到服务器。这里使用来简化操作用户名不存在抛出异常用户被管理员锁定抛出异常角色加入认证对象权限加入认证对象关键的代码如下,验证用户和获取用户信息的配置如下

通常在根据LDAP进行身份验证时一般进行以下三步:

利用一个LDAP用户的用户名和密码绑定到LDAP服务器。

在LDAP中检索一个用户的条目,然后将提供的密码和检索到的LDAP记录中进行验证。

根据LDAP提供的记录,再去本系统中查找授权信息。

Shiro 提供了DefaultLdapRealm,只做了第二步,根据用户的条目和密码来验证。并不能满足我们的需求,所以肯定是要定制化LdapRealm。

这里使用Spring Ldap 来简化Ldap操作

public class LdapRealm extends AuthorizingRealm {

    private static final Logger logger = LoggerFactory.getLogger(LdapRealm.class);

    private LdapTemplate ldapTemplate;

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String username = (String) token.getPrincipal();
        String password = new String((char[]) token.getCredentials());

        try {
            LdapQuery ldapQuery = LdapQueryBuilder.query().base("DC=example,DC=com").searchScope(SearchScope.SUBTREE)
                    .filter("(sAMAccountName={0})", username);

            boolean authResult = ldapTemplate.authenticate(ldapQuery.base(), ldapQuery.filter().encode(), password);

            if (!authResult) {
                logger.debug("ldap authentication for {} failed", username);
                return null;
            }

            User ldapUser = (User) ldapTemplate.searchForObject(ldapQuery, new LdapUserAttrMapper());

            User user = userService.selectUserById(ldapUser.getUserId());
            if (user == null) {
                // 用户名不存在抛出异常
                throw new UnknownAccountException();
            }
            if (user.getRemoveFlag()) {
                // 用户被管理员锁定抛出异常
                throw new LockedAccountException();
            }

            SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(user, token.getCredentials(),
                    "LdapRealm");
            return authenticationInfo;
        } catch (Exception e) {
            logger.error("ldap authentication failed", e.toString());
            return null;
        }

    }


    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        Long userId = ShiroUtils.getUserId();
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        // 角色加入AuthorizationInfo认证对象
        info.setRoles(roleService.selectRoleKeys(userId));
        // 权限加入AuthorizationInfo认证对象
        info.setStringPermissions(menuService.selectPermsByUserId(userId));
        return info;
    }

    public LdapTemplate getLdapTemplate() {
        return ldapTemplate;
    }

    public void setLdapTemplate(LdapTemplate ldapTemplate) {
        this.ldapTemplate = ldapTemplate;
    }
}

关键的代码如下,验证用户和获取LDAP用户信息

LdapQuery ldapQuery = LdapQueryBuilder.query().base("DC=example,DC=com").searchScope(SearchScope.SUBTREE)
                    .filter("(sAMAccountName={0})", username);
boolean authResult = ldapTemplate.authenticate(ldapQuery.base(), ldapQuery.filter().encode(), password);
User ldapUser = (User) ldapTemplate.searchForObject(ldapQuery, new LdapUserAttrMapper());

Spring 的 ldap 配置如下:


    
    
    



    



    

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/74274.html

相关文章

  • Apache Shiro 简介

    摘要:的很容易反映出常见的工作流程。权限检查是执行授权的另一种方式。在安全框架领域提供了一些独特的东西一致的会话,可用于任何应用程序和任何架构层。 Apache Shiro™是一个功能强大且易于使用的Java安全框架,可执行身份验证,授权,加密和会话管理。借助Shiro易于理解的API,可以快速轻松地保护任何应用程序 - 从最小的移动应用程序到最大的Web和企业应用程序。 1. Apache S...

    econi 评论0 收藏0
  • apache shiro框架

    摘要:框架提供的接口,是的核心,代表安全管理器对象。可以开发人员编写,框架也提供一些。在中作为应用程序和安全数据之间的桥梁或连接器。例如要求中必须同时含有和的权限才能执行方法。 apache shiro框架简介  Apache Shiro是一个强大而灵活的开源安全框架,它能够干净利落地处理身份认证,授权,企业会话管理和加密。现在,使用Apache Shiro的人越来越多,因为它相当简单,相比比Sp...

    Tecode 评论0 收藏0
  • 不用 Spring Security 可否?试试这个小而美的安全框架

    摘要:写在前面在一款应用的整个生命周期,我们都会谈及该应用的数据安全问题。用户的合法性与数据的可见性是数据安全中非常重要的一部分。 写在前面 在一款应用的整个生命周期,我们都会谈及该应用的数据安全问题。用户的合法性与数据的可见性是数据安全中非常重要的一部分。但是,一方面,不同的应用对于数据的合法性和可见性要求的维度与粒度都有所区别;另一方面,以当前微服务、多服务的架构方式,如何共享Sessi...

    toddmark 评论0 收藏0
  • web开发安全框架中的Apache Shiro的应用

    摘要:安全框架是目前为止作为登录注册最常用的框架,因为它十分的强大简单,提供了认证授权加密和会话管理等功能。本质上是一个特定安全的。当配置时,必须指定至少一个用来进行身份验证和或授权。提供了多种可用的来获取安全相关的数据。 web开发安全框架中的Apache Shiro的应用前阶段就hadoop的分享了一些内容,希望对新手入门的朋友有点帮助吧!对于hadoop新手入门的,还是比较推荐大快搜索...

    2json 评论0 收藏0
  • shiro入门笔记

    摘要:当前可以是身份,不需要经过认证或者在原先的中存在记录。当前必须拥有所有指定的角色时,才能访问被该注解标注的方法。 关于 Apache Shiro 概念基本都粘自官网 http://shiro.apache.org/详细中文博客 http://wiki.jikexueyuan.com/p...与SpringBoot整合 https://segmentfault.com/a/11... ...

    yagami 评论0 收藏0

发表评论

0条评论

zhiwei

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<