【问题标题】:How do it set username to security context如何将用户名设置为安全上下文
【发布时间】:2020-04-18 22:12:16
【问题描述】:

我正在使用 Spring 启动。

我正在使用我自己的身份验证服务器来验证我的用户。

所以在调用我的身份验证服务器后,我的结果是 UserInfo 类的 json。

如何在安全上下文中设置它?

我将我的班级视为与org.springframework.security.core.userdetails.Userorg.springframework.security.core.userdetails.UserDetails 不同的班级类型。

这是我的JwtAuthenticationFilter 课程。

@Component
public class JwtAuthenticationFilter extends OncePerRequestFilter {

    @Autowired
    private AuthenticationService authenticationService;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {

        getJwtFromRequest(request, response, filterChain);
    }

    private void getJwtFromRequest(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        String bearerToken = request.getHeader("Authorization");

        if (!StringUtils.hasText(bearerToken) || !bearerToken.startsWith("Bearer ")) {

            throw new AccessTokenMissingException("No access token found in request headers");
        }

        // Call auth server to validate token
        try {
            ResponseEntity<String> result = authenticationService.getUserInfo(bearerToken.substring(7));

            UserInfo user = new ObjectMapper().readValue(result.getBody(), UserInfo.class);
            System.out.println(user.toString());

            // Invalid access token
            if (!result.getStatusCode().is2xxSuccessful()) {
                throw new InvalidAccessTokenException("Invalid access token");
            }


        } catch (HttpClientErrorException.Unauthorized | IOException e) {
            throw new InvalidAccessTokenException("Invalid access token");
        }

        //add to security context

        filterChain.doFilter(request, response);
    }
}

这是我的UserInfo 课程。

@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class UserInfo implements Serializable {
    private List<String> role = new ArrayList<>();
    private String username
    private String email;
}

【问题讨论】:

    标签: spring-boot authentication spring-security


    【解决方案1】:

    来自春天Security Reference documentation

    您需要做的就是编写一个过滤器(或等效的)来读取 来自某个位置的第三方用户信息,构建一个Spring Security-specific Authentication 对象,并将其放入 SecurityContextHolder。在这种情况下,您还需要考虑 通常由内置自动处理的事情 身份验证基础设施。例如,您可能需要 抢先创建一个 HTTP 会话以缓存它们之间的上下文 请求,在您向客户端写入响应之前

    还提供了一个创建简单身份验证对象的示例

    https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#what-is-authentication-in-spring-security

    SecurityContextHolder.getContext().setAuthentication(anAuthentication);
    

    这里的anAuthentication是要设置为SecurityContext的Authentication对象。

    更新:

    Authentication

    【讨论】:

    • 这没有意义,我的用户名和密码来自另一个身份验证服务器。所以提供用户名和密码是不对的。
    • 你需要提供一个Authentication对象
    猜你喜欢
    • 2022-09-30
    • 2013-11-07
    • 2012-11-14
    • 2021-01-01
    • 1970-01-01
    • 2012-08-13
    • 2010-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多