jwt怎么获取当前登录用户_Springboot security oauth2 jwt实现权限控制,实现微服务获取当前用户信息...

为什么使用jwt?

在原先dubbo+zookeeper项目中,web模块只暴露Restful接口,各服务模块只暴露duboo接口,此时用户登录后由web项目进行token的鉴权和验证,并通过dubbo的隐式传参将sessionID传递给dubbo服务模块, 拦截器再根据sessionID从Redis中获取用户信息设置到当前线程

然鹅,在springcloud中,各个微服务直接暴露的是restful接口,此时如何让各个微服务获取到当前用户信息呢?最佳的方式就是token了,token作为BS之间的会话标识(一般是原生随机token),同时也可以作为信息的载体传递一些自定义信息(jwt, 即Json web token)。

为了能更清楚的了解本文,需要对spring-security-oauth 及 jwt有一定了解,本文只关注用户信息传递这一块

认证服务器

认证服务器配置AuthorizationServerConfigurerAdapter

@Configuration

@PropertySource({"classpath:application.yml"})

@EnableAuthorizationServer

public class AuthServerConfiguration extends AuthorizationServerConfigurerAdapter {

@Autowired

AuthenticationManager authenticationManager;

@Autowired

TokenStore tokenStore;

@Autowired

JwtAccessTokenConverter jwtAccessTokenConverter;

@Autowired

ApprovalStore approvalStore;

@Override

public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

clients.jdbc(dataSource());

}

@Override

public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {

endpoints.authenticationManager(authenticationManager)

.tokenStore(tokenStore)

.accessTokenConverter(jwtAccessTokenConverter)

.approvalStore(approvalStore)

.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST);

}

@Override

public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {

//允许表单认证

security

.tokenKeyAccess("permitAll()") //url:/oauth/token_key,exposes public key for token verification if using JWT tokens

.checkTokenAccess("permitAll()")

.allowFormAuthenticationForClients();

}

@Bean

@Primary

@ConfigurationProperties("ms-sql.datasource")

public DataSource dataSource() {

return new DriverManagerDataSource();

}

@Bean

public ApprovalStore approvalStore() {

return new JdbcApprovalStore(dataSource());

}

/**

* 使用 Jwt token

* @param accessTokenConverter

* @return

*/

@Bean

public TokenStore tokenStore(@Autowired JwtAccessTokenConverter accessTokenConverter) {

return new JwtTokenStore(accessTokenConverter);

}

/**

* token 转换器,加入对称秘钥,使用自

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值