【问题标题】:Custom authentication自定义身份验证
【发布时间】:2010-12-17 18:35:15
【问题描述】:

我的系统有 2 个子系统。每个子系统都有不同的用户集。每个用户都有一个额外的字段“SystemName”,可以用来知道这个用户属于哪个系统。

在登录表单(每个子系统 1 个表单)中,我添加了一个隐藏字段,指定表单的类型(包含 SystemName 值)。

一般来说,检查比较简单:

if (user.systemName == params.systemName) {
    proceed with regular login
} else {
    throw standard login error
}

我尝试将该检查放入我的自定义 DaoAuthenticationProvider,但它无法访问“params.systemName”。

我应该将该代码放在哪里以使 Acegi 使用此检查对我的用户进行身份验证?

提前致谢。

【问题讨论】:

    标签: security authentication grails spring-security


    【解决方案1】:

    这就是我在 Java 中的做法。扩展WebAuthenticationDetails

    import javax.servlet.http.HttpServletRequest;
    import org.acegisecurity.ui.WebAuthenticationDetails;
    
    public class SystemNameWebAuthenticationDetails extends WebAuthenticationDetails {
    
        public SystemNameWebAuthenticationDetails() {
            super();
        }
    
        public SystemNameWebAuthenticationDetails(HttpServletRequest request) {
            super(request);
            this.systemName = request.getParameter("systemName");
        }
    
        public String getSystemName() {
            return systemName;
        }
    
        private String systemName;
    }
    

    在认证过滤器中设置:

    <bean id="authenticationProcessingFilter"
          class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilter">
          ...
          <property name="authenticationDetailsSource">
            <bean class="org.acegisecurity.ui.AuthenticationDetailsSourceImpl">
                <property name="clazz" value="SystemNameWebAuthenticationDetails"/>
            </bean>
          </property>
    </bean>
    

    稍后您可以在身份验证过程中访问该属性,向身份验证对象询问详细信息。或者这样做:

    SecurityContextHolder.getContext().getAuthentication().getDetails()
    

    【讨论】:

      猜你喜欢
      • 2020-04-01
      • 2022-12-10
      • 2018-09-26
      • 2016-07-09
      • 2016-08-04
      • 2011-10-13
      • 2016-11-07
      • 2016-07-03
      • 1970-01-01
      相关资源
      最近更新 更多