jasig CAS:引用AuthHandler类中的RegistredService

时间:2015-05-11 13:10:22

标签: java cas

我正在为我们公司开发一个自定义AuthHandler。 我们的想法是允许基于用户和服务进行访问。 但我无法找到访问RegistredService的方法。

有没有办法将RegistredService传递给我的AuthHandler?

/**
 *  Mbox Auth Handler
 */

package lu.ion.cas.adaptors.mbox;

import org.jasig.cas.authentication.handler.support.AbstractPreAndPostProcessingAuthenticationHandler;
import org.jasig.cas.authentication.handler.AuthenticationException;
import org.jasig.cas.authentication.principal.Credentials;
import org.jasig.cas.authentication.principal.UsernamePasswordCredentials;

import lu.ion.cas.MboxAuthHelper;

import javax.validation.constraints.NotNull;

public class AuthHandler
    extends AbstractPreAndPostProcessingAuthenticationHandler {

    private MboxAuthHelper mboxAuthHelper;
    private RequestContext context;

    protected boolean doAuthentication(final Credentials credentials)
        throws AuthenticationException {

        return authenticateUsernamePasswordInternal((UsernamePasswordCredentials) credentials);
    }

    protected boolean authenticateUsernamePasswordInternal(
        final UsernamePasswordCredentials credentials)
        throws AuthenticationException {

        return mboxAuthHelper.load(credentials.getUsername(), credentials.getPassword(), "/auth/check") != null;
    }

    public boolean supports(Credentials credentials) {
        return true;
    }

    public final void setMboxAuthHelper(final MboxAuthHelper mboxAuthHelper) {
        this.mboxAuthHelper = mboxAuthHelper;
    }

}

我正在使用CAS 3.5.2。

2 个答案:

答案 0 :(得分:0)

我已经使用CAS几年了,发现有很多方法可以做任何事情。我不知道如何(或者如果)您可以将RegisteredService传递给AuthHandler。我使用自定义AuthenticationFilter解决了同样的问题。

(后端) 在您的CAS项目中/附近创建AuthenticationFilter.java,如下所示:

public class AuthenticationFilter implements Filter {

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        HttpSession session = request.getSession();

        String loginName = request.getRemoteUser();
        String contextPath = request.getContextPath();

        System.err.println("loginName is: " + loginName);
        System.err.println("contextPath is: " + contextPath);

        boolean isAuthorized = false;

        // do work/query to find out if they are authorized

        if (isAuthorized) {
            chain.doFilter(request, response);
        } else {
            session.invalidate();
            // print error page
        }

    }

    @Override
    public void init(FilterConfig config) throws ServletException {
    }

    @Override
    public void destroy() {
    }
}

(前端)然后添加到您的过滤器链。如果你有一个带有现有CAS过滤器的web.xml,那很简单。

...
<filter>
    <filter-name>Custom Filter</filter-name>
    <filter-class>
        com.yoursite.filter.AuthenticationFilter
    </filter-class>
</filter>

<filter-mapping>
    <filter-name>Custom Filter</filter-name>
    <url-pattern>/index.jsp</url-pattern>
</filter-mapping>
...

答案 1 :(得分:0)

没有办法做到这一点。如果要实现CAS 3.5.2的授权规则,则应查看cas-addons: https://github.com/Unicon/cas-addons/wiki