복붙노트

[SPRING] 사용자 권한 부여 된 권한은 항상 : ROLE_ANONYMOUS입니까?

SPRING

사용자 권한 부여 된 권한은 항상 : ROLE_ANONYMOUS입니까?

등록 후 프로그래밍 방식의 로그인을 만들기 위해 다음 방법을 사용하고 있습니다.

private void autoLogin(User user,
            HttpServletRequest request)
    {

GrantedAuthority[] grantedAuthorities = new GrantedAuthority[] { new GrantedAuthorityImpl(
                "ROLE_ADMIN") };

        UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
                user.getUsername(), user.getPassword(),grantedAuthorities);

        // generate session if one doesn't exist
        request.getSession();

        token.setDetails(new WebAuthenticationDetails(request));
        Authentication authenticatedUser = authenticationManager.authenticate(token);

        SecurityContextHolder.getContext().setAuthentication(authenticatedUser);
    }

사용자는 인증되지만 항상 ROLE_ANONYMOUS가 있습니다. 이유를 모르겠습니다. 어떤 아이디어?

해결법

  1. ==============================

    1.이 동작은 매우 이상하게 보입니다. Javi는 보안 컨텍스트를 수동으로 세션에 유지할 것을 제안하지만 Spring Security의 SecurityContextPersistenceFilter에 의해 자동으로 수행되어야합니다.

    이 동작은 매우 이상하게 보입니다. Javi는 보안 컨텍스트를 수동으로 세션에 유지할 것을 제안하지만 Spring Security의 SecurityContextPersistenceFilter에 의해 자동으로 수행되어야합니다.

    내가 상상할 수있는 한 가지 원인은 등록 처리 페이지의 에 filters = "none"입니다.

    filters = "none"은 지정된 URL에 대한 모든 보안 필터를 비활성화합니다. 보시다시피 Spring Security의 다른 기능을 방해 할 수 있습니다. 따라서 더 나은 접근 방법은 필터를 활성화 된 상태로 유지하면서 모든 사용자가 액세스 할 수 있도록 구성하는 것입니다. 몇 가지 옵션이 있습니다.

  2. ==============================

    2.비슷한 문제가있어서 인증을 한 후 수동으로 세션에 설정해야했습니다.

    비슷한 문제가있어서 인증을 한 후 수동으로 세션에 설정해야했습니다.

    request.getSession().setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, SecurityContextHolder.getContext());
    

    시도 해봐.

  3. from https://stackoverflow.com/questions/3923296/user-granted-authorities-are-always-role-anonymous by cc-by-sa and MIT license