gpt4 book ai didi

spring-boot - 使用 Spring Security 的 gRPC 和 OAuth2 身份验证

转载 作者:行者123 更新时间:2023-12-04 02:52:17 25 4
gpt4 key购买 nike

我正在使用基于 Spring 的“后端”和 Android 的“前端”来试验 gRPC,其想法是我将通过 HTTP 使用密码授予类型请求访问 token (使用标准/oauth/token RESTful 端点)并使用通过 RPC 的所有后续请求中提供的访问 token (设置授权 header )。

我的 spring '后端' 上有一个 gRPC 服务器拦截器,它将从服务器调用中获取授权 header ,并针对 token 存储验证访问 token 。

我不知道下一步该做什么,或者即使到目前为止我所拥有的是“正确”的方法。

这是我的拦截器:

@Component
public class GrpcRequestInterceptor implements ServerInterceptor {
private AuthenticationManager authenticationManager;

public GrpcRequestInterceptor(AuthenticationManager authenticationManager, AuthorizationCodeServices authorizationCodeServices) {
this.authenticationManager = authenticationManager;
}

@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> serverCall, Metadata metadata, ServerCallHandler<ReqT, RespT> serverCallHandler) {
String authorizationHeader = metadata.get(Metadata.Key.of("Authorization", Metadata.ASCII_STRING_MARSHALLER));

PreAuthenticatedAuthenticationToken preAuthenticatedAuthenticationToken =
new PreAuthenticatedAuthenticationToken(authorizationHeader.substring("Bearer ".length()), "");

PreAuthenticatedAuthenticationProvider preAuthenticatedAuthenticationProvider = new PreAuthenticatedAuthenticationProvider();
preAuthenticatedAuthenticationProvider.setPreAuthenticatedUserDetailsService(new AuthenticationUserDetailsService<PreAuthenticatedAuthenticationToken>() {
@Override
public UserDetails loadUserDetails(PreAuthenticatedAuthenticationToken preAuthenticatedAuthenticationToken) throws UsernameNotFoundException {
????
}
});
Authentication authentication = preAuthenticatedAuthenticationProvider.authenticate(preAuthenticatedAuthenticationToken);

SecurityContextHolder.getContext().setAuthentication(authentication);

return serverCallHandler.startCall(serverCall, metadata);
}
}

如何获取 token 服务以及如何仅使用我自己提供的访问 token 对我的用户进行身份验证。

我的安全配置如下:
@Configuration
@EnableAuthorizationServer
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private AccountDetailsService accountDetailsService;

@Profile(value = "development")
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/h2-console/**");
}

@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(accountDetailsService)
.passwordEncoder(initPasswordEncoder());
}

@Bean
public PasswordEncoder initPasswordEncoder() {
return new BCryptPasswordEncoder(10);
}
}

我应该在这里手动设置 token 服务,然后将其注入(inject)我的拦截器还是???完成这项工作的“最灵活”的方法是什么?

任何帮助是极大的赞赏!

最佳答案

最好连接 ResourceServerTokenServices 而不是 TokenStore。 ResourceServerTokenServices 将对访问 token 执行 TokenStore 不做的额外检查,例如确保 token 没有过期。

@Autowired
private ResourceServerTokenServices tokenServices;

...

OAuth2Authentication authentication = tokenServices.loadAuthentication(token);
SecurityContextHolder.getContext().setAuthentication(authentication);

关于spring-boot - 使用 Spring Security 的 gRPC 和 OAuth2 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41919925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com