rzd.vivc.documentexamination.service.AuthenticationInfoSpringSecurity.java Source code

Java tutorial

Introduction

Here is the source code for rzd.vivc.documentexamination.service.AuthenticationInfoSpringSecurity.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package rzd.vivc.documentexamination.service;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

/**
 *    ? ? ? Spring
 * Security
 *
 * @author VVolgina
 */
@Component
public class AuthenticationInfoSpringSecurity implements AuthenticationInfoService {

    @Override
    public String getLogin() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object principal = authentication.getPrincipal();
        String username;
        if (principal instanceof UserDetails) {
            username = ((UserDetails) principal).getUsername();
        } else {
            username = principal.toString();
        }
        return username;
    }

}