简体   繁体   中英

jasig CAS: reference to RegistredService in AuthHandler Class

I'm developing a custom AuthHandler for our company. The idea is to allow access based on user and service. But i can't find a way to access the RegistredService.

Is there a way to pass the RegistredService to my 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;
    }

}

I'm using CAS 3.5.2.

I have used CAS for a few years and found that there are many ways to do everything. I don't know how (or if) you can pass the RegisteredService to your AuthHandler. I solved the same problem by using a custom AuthenticationFilter.

(backend) Create AuthenticationFilter.java in/near your CAS project like this:

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() {
    }
}

(frontend) Then add to your filter chain. If you have a web.xml with existing CAS filters, it is easy.

...
<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>
...

No there is no way to do this. If you want to implement authorization rules for CAS 3.5.2, you should review cas-addons: https://github.com/Unicon/cas-addons/wiki

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM