i通过使用Spring Boot,Spring Security,Spring Oauth2,Eureka Server和Zuul代理创建微服务。我创建了下面给出的三个项目。
- register-eureka:在Eureka服务器中注册我的服务。
-
gatway-zuul:我实施了Zuul代理服务器,Spring Security和Spring-Security-Oauth2.IT工作正常,代币正在生成,并且它也成功地将请求转发给其他微服务。
-
MICOREST:第三个微服务用于创建静止的服务。
问题:我需要在Micorest Project Controller中获取用户详细信息,但我不知道如何获得它。
我试图使用主要类。
@RequestMapping(value = "/user", method = RequestMethod.GET)
public User getUser(Principal principal)
{
try
{
System.out.println("fine here mueraza"+principal.getName());
}
catch(Exception e) {
System.out.println("fine here mueraza===========principal is null==============");
}
return new User();
}
我使用委托人获取该值,但它总是cumming null
@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class GatewayZullApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayZullApplication.class, args);
}
}
aouth2 wrigration
@Configuration
@EnableOAuth2Client
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
private static String REALM="microclient";
private static final int ONE_DAY = 60 * 60 * 24;
private static final int THIRTY_DAYS = 60 * 60 * 24 * 30;
@Autowired
private TokenStore tokenStore;
@Autowired
private UserApprovalHandler userApprovalHandler;
@Autowired
@Qualifier("authenticationManagerBean")
private AuthenticationManager authenticationManager;
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("microclient")
.secret("{noop}secret")
.authorizedGrantTypes("password", "refresh_token")
.authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT")
.scopes("read", "write", "trust")
//.accessTokenValiditySeconds(ONE_DAY)
.accessTokenValiditySeconds(6000)
.refreshTokenValiditySeconds(THIRTY_DAYS);
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.tokenStore(tokenStore).userApprovalHandler(userApprovalHandler)
.authenticationManager(authenticationManager);
}
@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
oauthServer.realm(REALM);
}
}
您可以从JWT令牌获得校长。将用户实体加载到JWT令牌索赔。
如果您需要从任何服务访问用户实体详细信息:
- 从请求标题中获取令牌。
- 用正确的签名密钥解析令牌并获取用户详细信息。
供您参考: https://medium.com/@baimurzin/how-to-to-to-te-per-the-current-user-user-in-spring-cloud-cloud-microservices-c876e1c6fc65