Unicon / cas-client-autoconfig-support

Annotation-based configuration support for Apereo CAS Java clients
Apache License 2.0
165 stars 73 forks source link

How do you retrieve the user principal and attributes from the cas client #2

Closed dbollaer closed 9 years ago

dbollaer commented 9 years ago

Hello,

I have successfully configured the cas client, but I fail to retrieve the user principal and attributes. The following code always show "No user"

    public static String getCurrentLogin() {
        SecurityContext securityContext = SecurityContextHolder.getContext();
        Authentication authentication = securityContext.getAuthentication();
        String userName = "No user";

        if (authentication != null) {
            if (authentication.getPrincipal() instanceof UserDetails) {
                UserDetails springSecurityUser = (UserDetails) authentication.getPrincipal();
                userName = springSecurityUser.getUsername();
            } else if (authentication.getPrincipal() instanceof String) {
                userName = (String) authentication.getPrincipal();
            }
            userName = "No username";
        }

        return userName;
    }

Thank you in advance.

dima767 commented 9 years ago

There is no Spring Security involved here at all. This configuration simply uses straight up CAS Java client library (filters, etc.) and exposes its AttributePrincipal as a standard remote user in http servlet request. Here's a sample of the client boot app using this auto config library, and how you get the authenticated principal from http servlet request: https://github.com/UniconLabs/bootiful-cas-client/blob/master/src/main/groovy/net/unicon/cas/client/demo/MainController.groovy#L26

Cheers, D.