SpringSecurity스프링시큐리티 로그인 수동처리 Manually Login

less than 1 minute read

1. 간단하다

콘텍스트 홀더를 불러서 userDetails를 저장해준다. 어차피 시큐리티의 로그인 프로세스는 UserDetails에 필요한 정보를 디비나 기타 등등에서 가져와서 UserDetails구현체를 생성해가지고 시큐리티에서 관리하는 세션에 저장해주는 것이기 때문에… 이걸 간략하게 처리하는 것과 같다.

//UserDetails생성하는 코드를 사용해서 디테일즈 받아오기 UserDetails userDetails = userDetailsService.loadUserByUsername(account.getEmail()); //시큐리티 콘텍스트 홀더에 저장 SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities()));

2. 스텍오버플로우에서 발견했던건데… 잘 안된다.

AuthenticationMAnager에서 인증이 실패한다. 정상적인 로그인 프로세스를 타는 방법이긴 하지만 암호화 되어있는 password를 입력해야하는데 여기서 틀린 것 같다. 맞는 패스워드를 넣는다면 성공할 것 같다. 맞는 것 같으니 테스트는 생략한다.

		UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken("username", "password", "authorities");

		// Authenticate the user
		Authentication authentication = authenticationManager.authenticate(authRequest);
		SecurityContext securityContext = SecurityContextHolder.getContext();
		securityContext.setAuthentication(authentication);

		// Create a new session and add the security context.
		HttpSession session = request.getSession(true);
		session.setAttribute("SPRING\_SECURITY\_CONTEXT", securityContext);