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

com.github.yingzhuo.spring.security.jwt.util.JwtSecurityContextUtils Maven / Gradle / Ivy

There is a newer version: 1.0.3
Show newest version
/*                 _                                            _ _                           _       _
 *  ___ _ __  _ __(_)_ __   __ _       ___  ___  ___ _   _ _ __(_) |_ _   _       _ __   __ _| |_ ___| |__
 * / __| '_ \| '__| | '_ \ / _` |_____/ __|/ _ \/ __| | | | '__| | __| | | |_____| '_ \ / _` | __/ __| '_ \
 * \__ \ |_) | |  | | | | | (_| |_____\__ \  __/ (__| |_| | |  | | |_| |_| |_____| |_) | (_| | || (__| | | |
 * |___/ .__/|_|  |_|_| |_|\__, |     |___/\___|\___|\__,_|_|  |_|\__|\__, |     | .__/ \__,_|\__\___|_| |_|
 *     |_|                 |___/                                      |___/      |_|
 *
 *  https://github.com/yingzhuo/spring-security-patch
 */
package com.github.yingzhuo.spring.security.jwt.util;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Optional;

/**
 * @author 应卓
 */
@SuppressWarnings("unchecked")
public final class JwtSecurityContextUtils {

    private JwtSecurityContextUtils() {
        super();
    }

    public static Object getPrincipal() {
        try {
            return SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        } catch (Exception e) {
            return null;
        }
    }

    public static  T getPrincipal(Class clazz) {
        return (T) getPrincipal();
    }

    public static UserDetails getUserDetails() {
        return getPrincipal(UserDetails.class);
    }

    public static Object getCredentials() {
        try {
            return SecurityContextHolder.getContext().getAuthentication().getCredentials();
        } catch (Exception e) {
            return null;
        }
    }

    public static String getCredentialsAsString() {
        return Optional.ofNullable(getCredentials()).map(Object::toString).orElse(null);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy