gateway oauth2 对称加密_SpringCloud oauth2 jwt gateway demo

@Configuration

@EnableAuthorizationServer

@AllArgsConstructor

public class OAuth2Config extends AuthorizationServerConfigurerAdapter {

@Override

public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

clients.inMemory()

.withClient("user-service")

.secret("123456")

.scopes("service")

.autoApprove(true)

.authorizedGrantTypes("implicit", "refresh_token", "password", "authorization_code")

.accessTokenValiditySeconds(12 * 300);//5min过期

}

@Override

public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {

endpoints.tokenStore(tokenStore()).tokenEnhancer(jwtTokenEnhancer()).authenticationManager(authenticationManager);

}

@Override

public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {

oauthServer

.tokenKeyAccess("permitAll()")

.checkTokenAccess("isAuthenticated()").allowFormAuthenticationForClients().passwordEncoder(NoOpPasswordEncoder.getInstance());

/**

* 必须设置allowFormAuthenticationForClients 否则没有办法用postman获取token

* 也需要指定密码加密方式BCryptPasswordEncoder

*/

}

@Autowired

@Qualifier("authenticationManagerBean")

private AuthenticationManager authenticationManager;

@Bean

public TokenStore tokenStore() {

return new JwtTokenStore(jwtTokenEnhancer());

}

@Bean

protected JwtAccessTokenConverter jwtTokenEnhancer() {

JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();

jwtAccessTokenConverter.setKeyPair(keyPair());

return jwtAccessTokenConverter;

}

@Bean

public KeyPair keyPair() {

KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(new ClassPathResource("demojwt.jks"), "keystorepass".toCharArray());

return keyStoreKeyFactory.getKeyPair("jwt", "keypairpass".toCharArray());

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值