All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.smartcosmos.security.user.SmartCosmosUserArgumentResolver Maven / Gradle / Ivy

Go to download

Provides Spring Configuration and Annotations making it easier to develop SMART COSMOS Extensions

The newest version!
package net.smartcosmos.security.user;

import java.nio.file.attribute.UserPrincipalNotFoundException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.core.MethodParameter;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * @author voor
 */
@Component
public class SmartCosmosUserArgumentResolver implements HandlerMethodArgumentResolver {

    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.getParameterType().equals(SmartCosmosUser.class);
    }

    @Override
    public Object resolveArgument(MethodParameter parameter,
            ModelAndViewContainer mavContainer, NativeWebRequest webRequest,
            WebDataBinderFactory binderFactory) throws Exception {

        HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();

        if (null != SecurityContextHolder.getContext()) {
            Object principal = SecurityContextHolder.getContext().getAuthentication()
                    .getPrincipal();

            if (principal instanceof OAuth2Authentication) {
                principal = ((OAuth2Authentication) principal).getPrincipal();
            }

            if (principal instanceof SmartCosmosUser) {
                return principal;
            }
        }

        throw new UserPrincipalNotFoundException(
                "Could not find Smart Cosmos User in current request.");
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy