Skip to content

Instantly share code, notes, and snippets.

@PawelSzymanski89
Created May 22, 2020 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PawelSzymanski89/ad03cc28cd9d4caf0d4a3dcc8196585c to your computer and use it in GitHub Desktop.
Save PawelSzymanski89/ad03cc28cd9d4caf0d4a3dcc8196585c to your computer and use it in GitHub Desktop.
Globalne dane uzytkownika w kazdym requescie
package pl.szymanskip.openapi.autoryzacja;
import lombok.AllArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import pl.szymanskip.openapi.encje.Uzytkownik;
import pl.szymanskip.openapi.obiekty.UzytkownikFront;
import pl.szymanskip.openapi.serwisy.SerwisUzytkownik;
import javax.servlet.http.HttpServletRequest;
import java.util.Optional;
@ControllerAdvice
@AllArgsConstructor
public class AutoryzacjaKontroler {
private SerwisUzytkownik serwisUzytkownik;
@ModelAttribute("aut")
public UzytkownikFront zbudujUzytkownikaNaFront() {
UzytkownikFront uzytkownikFront = new UzytkownikFront();
Authentication tozsamosc = SecurityContextHolder.getContext().getAuthentication();
String nazwa = tozsamosc.getPrincipal().toString();
Optional<Uzytkownik> uzytkownik = this.serwisUzytkownik.znajdzUzytkownikaPoNazwie(nazwa);
uzytkownikFront.setZalogowany(!nazwa.equals("anonymousUser") && uzytkownik.isPresent());
if (uzytkownikFront.jestZalogowany()) {
Uzytkownik uzytkownikZBazy = uzytkownik.get();
uzytkownikFront.setNazwaUzytkownika(nazwa);
uzytkownikFront.setIdUzytkownika(uzytkownikZBazy.getIdUzytkownika());
uzytkownikFront.setEmailUzytkownika(uzytkownikZBazy.getEmail());
uzytkownikFront.setPakietyUprawnien(uzytkownikZBazy.getPakiety());
}
uzytkownikFront.setIpUzytkownika(getIpUzytkownika());
uzytkownikFront.setIdSesji(getIdSesji());
return uzytkownikFront;
}
private Optional<HttpServletRequest> getBezacyRequest() {
final ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder
.currentRequestAttributes();
return Optional.of(attr.getRequest());
}
private String getIpUzytkownika(){
Optional<HttpServletRequest> request = getBezacyRequest();
if (request.isEmpty()) return "";
String remoteAddr = "";
remoteAddr = request.get().getHeader("X-FORWARDED-FOR");
if (remoteAddr == null || "".equals(remoteAddr)) {
remoteAddr = request.get().getRemoteAddr();
}
return remoteAddr;
}
private String getIdSesji(){
Optional<HttpServletRequest> request = getBezacyRequest();
if (request.isEmpty()) return "";
return request.get().getSession().getId();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment