Spring Security login problem

0

Witam, mam problem ze spring security. Używam spring data, mvc i security chciałbym zrobić proste logowanie użytkowników których mam w bazie odnaleźć ich role (np. wyświetlić w konsoli) i zabezpieczyć dane url dla użytkowników którzy nie mają przykładowo dostępu np. ROLE_ADMIN.

Mój problem : podczas logowania użytkowników (manualnie) po wprowadzeniu do securityContextHolder authentication od razu po wyjściu z metody trace securityContextHolder.getContext().getAuthentication() które ustawia się jako null.

Wątpie, że błąd jest spowodowany w innej części konfiguracji, ale jestem początkujący jeżeli uważacie, że błąd jest gdzieś indziej wkleje pozostały kod źródłowy.

//zalozmy ze znajduje mi admina ktory ma role ROLE_ADMIN
 	@RequestMapping("login")
	public String start() throws ServletException {
		User admin = CustomUserDetailsService.usersRepository.findByLogin("admin");
		Iterable<UserRole> found = CustomUserDetailsService.userRolesRepository.findAll();
		UserRole ROLEPLAYING = null;
		   
		   for(UserRole x: found)
			for (Iterator<User> iterator = x.getUser().iterator(); iterator.hasNext();) {
				User a = iterator.next();
				if(a.getId() == admin.getId()) 
					ROLEPLAYING = x;
			}
		   
			List<String> userRoles = new ArrayList<>();
			userRoles.add(ROLEPLAYING.getRole());
			
			CustomUserDetails customUserDetails = new CustomUserDetails(admin, userRoles);
						UserDetails userDetails = customUserDetails;
			Authentication authentication = new UsernamePasswordAuthenticationToken(userDetails, userDetails.getPassword(), userDetails.getAuthorities());
			securityContextHolder.getContext().setAuthentication(authentication);

			System.out.println(securityContextHolder.getContext().getAuthentication());
//niestety po odwolaniu sie tutaj dostaje caly czas null
 	@RequestMapping("test")
	public void test() {
		if(SecurityContextHolder.getContext().getAuthentication() == null)
			System.out.println("null");
		else
			System.out.println(securityContextHolder.getContext().getAuthentication());
0

Sprawa zamknięta, chodziło o CustomUserDetailsService (które wstrzykuje się w konfiguracji). Miałem źle napisaną metode loadUserByUsername przy wyszukiwania roli dla usera zwracała nulle i wszystko się sypało :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1